1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // template <class Alloc>
12 // struct allocator_traits
14 // template <class Ptr, class... Args>
15 // static constexpr void construct(allocator_type& a, Ptr p, Args&&... args);
21 #include <type_traits>
25 #include "test_macros.h"
26 #include "incomplete_type_helper.h"
40 TEST_CONSTEXPR_CXX20
B(int& count
) : count_(count
) {}
42 #if TEST_STD_VER >= 11
43 template <class U
, class ...Args
>
44 TEST_CONSTEXPR_CXX20
void construct(U
* p
, Args
&& ...args
)
48 std::construct_at(p
, std::forward
<Args
>(args
)...);
50 ::new ((void*)p
) U(std::forward
<Args
>(args
)...);
60 TEST_CONSTEXPR_CXX20
A0(int* count
) {++*count
;}
65 TEST_CONSTEXPR_CXX20
A1(int* count
, char c
)
74 TEST_CONSTEXPR_CXX20
A2(int* count
, char c
, int i
)
82 TEST_CONSTEXPR_CXX20
bool test()
87 std::allocator
<A0
> alloc
;
88 A0
* a0
= alloc
.allocate(1);
89 assert(A0_count
== 0);
90 std::allocator_traits
<A
<A0
> >::construct(a
, a0
, &A0_count
);
91 assert(A0_count
== 1);
92 alloc
.deallocate(a0
, 1);
97 std::allocator
<A1
> alloc
;
98 A1
* a1
= alloc
.allocate(1);
99 assert(A1_count
== 0);
100 std::allocator_traits
<A
<A1
> >::construct(a
, a1
, &A1_count
, 'c');
101 assert(A1_count
== 1);
102 alloc
.deallocate(a1
, 1);
107 std::allocator
<A2
> alloc
;
108 A2
* a2
= alloc
.allocate(1);
109 assert(A2_count
== 0);
110 std::allocator_traits
<A
<A2
> >::construct(a
, a2
, &A2_count
, 'd', 5);
111 assert(A2_count
== 1);
112 alloc
.deallocate(a2
, 1);
115 typedef IncompleteHolder
* VT
;
118 std::allocator
<VT
> alloc
;
119 VT
* vt
= alloc
.allocate(1);
120 std::allocator_traits
<Alloc
>::construct(a
, vt
, nullptr);
121 alloc
.deallocate(vt
, 1);
124 #if TEST_STD_VER >= 11
128 B
<A0
> b(b_construct
);
129 std::allocator
<A0
> alloc
;
130 A0
* a0
= alloc
.allocate(1);
131 assert(A0_count
== 0);
132 assert(b_construct
== 0);
133 std::allocator_traits
<B
<A0
> >::construct(b
, a0
, &A0_count
);
134 assert(A0_count
== 1);
135 assert(b_construct
== 1);
136 alloc
.deallocate(a0
, 1);
141 B
<A1
> b(b_construct
);
142 std::allocator
<A1
> alloc
;
143 A1
* a1
= alloc
.allocate(1);
144 assert(A1_count
== 0);
145 assert(b_construct
== 0);
146 std::allocator_traits
<B
<A1
> >::construct(b
, a1
, &A1_count
, 'c');
147 assert(A1_count
== 1);
148 assert(b_construct
== 1);
149 alloc
.deallocate(a1
, 1);
154 B
<A2
> b(b_construct
);
155 std::allocator
<A2
> alloc
;
156 A2
* a2
= alloc
.allocate(1);
157 assert(A2_count
== 0);
158 assert(b_construct
== 0);
159 std::allocator_traits
<B
<A2
> >::construct(b
, a2
, &A2_count
, 'd', 5);
160 assert(A2_count
== 1);
161 assert(b_construct
== 1);
162 alloc
.deallocate(a2
, 1);
169 int main(int, char**)
173 #if TEST_STD_VER > 17
174 static_assert(test());