Fix std::vector's use of temporary objects
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / allocator / construction.cc
blob8040949c04c559c018f06a58df338d8b007d881b
1 // Copyright (C) 2016 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.
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.
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/>.
18 // { dg-options "-std=gnu++11" }
19 // { dg-do compile }
21 #include <vector>
23 struct Tag { };
25 template<typename T>
26 struct TaggingAllocator
28 using value_type = T;
30 TaggingAllocator() = default;
32 template<typename U>
33 TaggingAllocator(const TaggingAllocator<U>&) { }
36 allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
38 void
39 deallocate(T* p, std::size_t n) { std::allocator<T>{}.deallocate(p, n); }
41 template<typename U, typename... Args>
42 void
43 construct(U* p, Args&&... args)
44 { ::new((void*)p) U(Tag{}, std::forward<Args>(args)...); }
46 template<typename U, typename... Args>
47 void
48 destroy(U* p)
49 { p->~U(); }
52 template<typename T, typename U>
53 bool
54 operator==(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
55 { return true; }
57 template<typename T, typename U>
58 bool
59 operator!=(const TaggingAllocator<T>&, const TaggingAllocator<U>&)
60 { return false; }
62 struct X
64 // All constructors must be passed the Tag type.
66 // DefaultInsertable into vector<X, TaggingAllocator<X>>,
67 X(Tag) { }
68 // CopyInsertable into vector<X, TaggingAllocator<X>>,
69 X(Tag, const X&) { }
70 // MoveInsertable into vector<X, TaggingAllocator<X>>, and
71 X(Tag, X&&) { }
73 // EmplaceConstructible into vector<X, TaggingAllocator<X>> from args.
74 template<typename... Args>
75 X(Tag, Args&&...) { }
77 // not DefaultConstructible, CopyConstructible or MoveConstructible.
78 X() = delete;
79 X(const X&) = delete;
80 X(X&&) = delete;
82 // CopyAssignable.
83 X& operator=(const X&) { return *this; }
85 // MoveAssignable.
86 X& operator=(X&&) { return *this; }
88 private:
89 // Not Destructible.
90 ~X() { }
92 // Erasable from vector<X, TaggingAllocator<X>>.
93 friend class TaggingAllocator<X>;
96 template class std::vector<X, TaggingAllocator<X>>;
98 void test01()
100 std::vector<X, TaggingAllocator<X>> v;
101 v.reserve(3);
102 v.emplace_back();
103 v.emplace(v.begin());
104 v.emplace(v.begin(), 1, 2, 3);