| 1 | // Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do compile { target c++11 } } |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | struct Tag { }; |
| 23 | |
| 24 | template<typename T> |
| 25 | struct TaggingAllocator |
| 26 | { |
| 27 | using value_type = T; |
| 28 | |
| 29 | TaggingAllocator() = default; |
| 30 | |
| 31 | template<typename U> |
| 32 | TaggingAllocator(const TaggingAllocator<U>&) { } |
| 33 | |
| 34 | T* |
| 35 | allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); } |
| 36 | |
| 37 | void |
| 38 | deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); } |
| 39 | |
| 40 | template<typename U, typename... Args> |
| 41 | void |
| 42 | construct(U* p, Args&&... args) |
| 43 | { ::new((void*)p) U(Tag{}, std::forward<Args>(args)...); } |
| 44 | |
| 45 | template<typename U, typename... Args> |
| 46 | void |
| 47 | destroy(U* p) |
| 48 | { p->~U(); } |
| 49 | }; |
| 50 | |
| 51 | template<typename T, typename U> |
| 52 | bool |
| 53 | operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 54 | { return true; } |
| 55 | |
| 56 | template<typename T, typename U> |
| 57 | bool |
| 58 | operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&) |
| 59 | { return false; } |
| 60 | |
| 61 | struct X |
| 62 | { |
| 63 | // All constructors must be passed the Tag type. |
| 64 | |
| 65 | // DefaultInsertable into vector<X, TaggingAllocator<X>>, |
| 66 | X(Tag) { } |
| 67 | // CopyInsertable into vector<X, TaggingAllocator<X>>, |
| 68 | X(Tag, const X&) { } |
| 69 | // MoveInsertable into vector<X, TaggingAllocator<X>>, and |
| 70 | X(Tag, X&&) { } |
| 71 | |
| 72 | // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args. |
| 73 | template<typename... Args> |
| 74 | X(Tag, Args&&...) { } |
| 75 | |
| 76 | // not DefaultConstructible, CopyConstructible or MoveConstructible. |
| 77 | X() = delete; |
| 78 | X(const X&) = delete; |
| 79 | X(X&&) = delete; |
| 80 | |
| 81 | // CopyAssignable. |
| 82 | X& operator=(const X&) { return *this; } |
| 83 | |
| 84 | // MoveAssignable. |
| 85 | X& operator=(X&&) { return *this; } |
| 86 | |
| 87 | private: |
| 88 | // Not Destructible. |
| 89 | ~X() { } |
| 90 | |
| 91 | // Erasable from vector<X, TaggingAllocator<X>>. |
| 92 | friend class TaggingAllocator<X>; |
| 93 | }; |
| 94 | |
| 95 | template class std::vector<X, TaggingAllocator<X>>; |
| 96 | |
| 97 | void test01() |
| 98 | { |
| 99 | std::vector<X, TaggingAllocator<X>> v; |
| 100 | v.reserve(3); |
| 101 | v.emplace_back(); |
| 102 | v.emplace(v.begin()); |
| 103 | v.emplace(v.begin(), 1, 2, 3); |
| 104 | } |