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 27 template <class T> 28 struct A 29 { 30 typedef T value_type; 31 32 }; 33 34 int b_construct = 0; 35 36 template <class T> 37 struct B 38 { 39 typedef T value_type; 40 41 #if TEST_STD_VER >= 11 42 template <class U, class ...Args> 43 void construct(U* p, Args&& ...args) 44 { 45 ++b_construct; 46 ::new ((void*)p) U(std::forward<Args>(args)...); 47 } 48 #endif 49 }; 50 51 struct A0 52 { 53 static int count; 54 A0() {++count;} 55 }; 56 57 int A0::count = 0; 58 59 struct A1 60 { 61 static int count; 62 A1(char c) 63 { 64 assert(c == 'c'); 65 ++count; 66 } 67 }; 68 69 int A1::count = 0; 70 71 struct A2 72 { 73 static int count; 74 A2(char c, int i) 75 { 76 assert(c == 'd'); 77 assert(i == 5); 78 ++count; 79 } 80 }; 81 82 int A2::count = 0; 83 84 int main() 85 { 86 { 87 A0::count = 0; 88 A<int> a; 89 std::aligned_storage<sizeof(A0)>::type a0; 90 assert(A0::count == 0); 91 std::allocator_traits<A<int> >::construct(a, (A0*)&a0); 92 assert(A0::count == 1); 93 } 94 { 95 A1::count = 0; 96 A<int> a; 97 std::aligned_storage<sizeof(A1)>::type a1; 98 assert(A1::count == 0); 99 std::allocator_traits<A<int> >::construct(a, (A1*)&a1, 'c'); 100 assert(A1::count == 1); 101 } 102 { 103 A2::count = 0; 104 A<int> a; 105 std::aligned_storage<sizeof(A2)>::type a2; 106 assert(A2::count == 0); 107 std::allocator_traits<A<int> >::construct(a, (A2*)&a2, 'd', 5); 108 assert(A2::count == 1); 109 } 110 #if TEST_STD_VER >= 11 111 { 112 A0::count = 0; 113 b_construct = 0; 114 B<int> b; 115 std::aligned_storage<sizeof(A0)>::type a0; 116 assert(A0::count == 0); 117 assert(b_construct == 0); 118 std::allocator_traits<B<int> >::construct(b, (A0*)&a0); 119 assert(A0::count == 1); 120 assert(b_construct == 1); 121 } 122 { 123 A1::count = 0; 124 b_construct = 0; 125 B<int> b; 126 std::aligned_storage<sizeof(A1)>::type a1; 127 assert(A1::count == 0); 128 assert(b_construct == 0); 129 std::allocator_traits<B<int> >::construct(b, (A1*)&a1, 'c'); 130 assert(A1::count == 1); 131 assert(b_construct == 1); 132 } 133 { 134 A2::count = 0; 135 b_construct = 0; 136 B<int> b; 137 std::aligned_storage<sizeof(A2)>::type a2; 138 assert(A2::count == 0); 139 assert(b_construct == 0); 140 std::allocator_traits<B<int> >::construct(b, (A2*)&a2, 'd', 5); 141 assert(A2::count == 1); 142 assert(b_construct == 1); 143 } 144 #endif 145 } 146