construct.pass.cpp (3369B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <memory> 11 12 // template <class Alloc> 13 // struct allocator_traits 14 // { 15 // template <class Ptr, class... Args> 16 // static void construct(allocator_type& a, Ptr p, Args&&... args); 17 // ... 18 // }; 19 20 #include <memory> 21 #include <new> 22 #include <type_traits> 23 #include <cassert> 24 25 #include "test_macros.h" 26 #include "incomplete_type_helper.h" 27 28 template <class T> 29 struct A 30 { 31 typedef T value_type; 32 33 }; 34 35 int b_construct = 0; 36 37 template <class T> 38 struct B 39 { 40 typedef T value_type; 41 42 #if TEST_STD_VER >= 11 43 template <class U, class ...Args> 44 void construct(U* p, Args&& ...args) 45 { 46 ++b_construct; 47 ::new ((void*)p) U(std::forward<Args>(args)...); 48 } 49 #endif 50 }; 51 52 struct A0 53 { 54 static int count; 55 A0() {++count;} 56 }; 57 58 int A0::count = 0; 59 60 struct A1 61 { 62 static int count; 63 A1(char c) 64 { 65 assert(c == 'c'); 66 ++count; 67 } 68 }; 69 70 int A1::count = 0; 71 72 struct A2 73 { 74 static int count; 75 A2(char c, int i) 76 { 77 assert(c == 'd'); 78 assert(i == 5); 79 ++count; 80 } 81 }; 82 83 int A2::count = 0; 84 85 int main() 86 { 87 { 88 A0::count = 0; 89 A<int> a; 90 std::aligned_storage<sizeof(A0)>::type a0; 91 assert(A0::count == 0); 92 std::allocator_traits<A<int> >::construct(a, (A0*)&a0); 93 assert(A0::count == 1); 94 } 95 { 96 A1::count = 0; 97 A<int> a; 98 std::aligned_storage<sizeof(A1)>::type a1; 99 assert(A1::count == 0); 100 std::allocator_traits<A<int> >::construct(a, (A1*)&a1, 'c'); 101 assert(A1::count == 1); 102 } 103 { 104 A2::count = 0; 105 A<int> a; 106 std::aligned_storage<sizeof(A2)>::type a2; 107 assert(A2::count == 0); 108 std::allocator_traits<A<int> >::construct(a, (A2*)&a2, 'd', 5); 109 assert(A2::count == 1); 110 } 111 { 112 typedef IncompleteHolder* VT; 113 typedef A<VT> Alloc; 114 Alloc a; 115 std::aligned_storage<sizeof(VT)>::type store; 116 std::allocator_traits<Alloc>::construct(a, (VT*)&store, nullptr); 117 } 118 #if TEST_STD_VER >= 11 119 { 120 A0::count = 0; 121 b_construct = 0; 122 B<int> b; 123 std::aligned_storage<sizeof(A0)>::type a0; 124 assert(A0::count == 0); 125 assert(b_construct == 0); 126 std::allocator_traits<B<int> >::construct(b, (A0*)&a0); 127 assert(A0::count == 1); 128 assert(b_construct == 1); 129 } 130 { 131 A1::count = 0; 132 b_construct = 0; 133 B<int> b; 134 std::aligned_storage<sizeof(A1)>::type a1; 135 assert(A1::count == 0); 136 assert(b_construct == 0); 137 std::allocator_traits<B<int> >::construct(b, (A1*)&a1, 'c'); 138 assert(A1::count == 1); 139 assert(b_construct == 1); 140 } 141 { 142 A2::count = 0; 143 b_construct = 0; 144 B<int> b; 145 std::aligned_storage<sizeof(A2)>::type a2; 146 assert(A2::count == 0); 147 assert(b_construct == 0); 148 std::allocator_traits<B<int> >::construct(b, (A2*)&a2, 'd', 5); 149 assert(A2::count == 1); 150 assert(b_construct == 1); 151 } 152 #endif 153 }