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>
15 // static constexpr void destroy(allocator_type& a, Ptr p);
23 #include "test_macros.h"
24 #include "incomplete_type_helper.h"
31 TEST_CONSTEXPR_CXX20 T
* allocate(std::size_t n
)
33 return std::allocator
<T
>().allocate(n
);
36 TEST_CONSTEXPR_CXX20
void deallocate(T
* p
, std::size_t n
)
38 return std::allocator
<T
>().deallocate(p
, n
);
45 TEST_CONSTEXPR
explicit CountDestroy(int* counter
)
51 TEST_CONSTEXPR_CXX20 T
* allocate(std::size_t n
)
53 return std::allocator
<T
>().allocate(n
);
56 TEST_CONSTEXPR_CXX20
void deallocate(T
* p
, std::size_t n
)
58 return std::allocator
<T
>().deallocate(p
, n
);
62 TEST_CONSTEXPR_CXX20
void destroy(U
* p
)
71 struct CountDestructor
73 TEST_CONSTEXPR
explicit CountDestructor(int* counter
)
77 TEST_CONSTEXPR_CXX20
~CountDestructor() { ++*counter_
; }
82 TEST_CONSTEXPR_CXX20
bool test()
85 typedef NoDestroy
<CountDestructor
> Alloc
;
88 CountDestructor
* pool
= std::allocator_traits
<Alloc
>::allocate(alloc
, 1);
90 std::allocator_traits
<Alloc
>::construct(alloc
, pool
, &destructors
);
91 assert(destructors
== 0);
93 std::allocator_traits
<Alloc
>::destroy(alloc
, pool
);
94 assert(destructors
== 1);
96 std::allocator_traits
<Alloc
>::deallocate(alloc
, pool
, 1);
99 typedef IncompleteHolder
* T
;
100 typedef NoDestroy
<T
> Alloc
;
102 T
* pool
= std::allocator_traits
<Alloc
>::allocate(alloc
, 1);
103 std::allocator_traits
<Alloc
>::construct(alloc
, pool
, nullptr);
104 std::allocator_traits
<Alloc
>::destroy(alloc
, pool
);
105 std::allocator_traits
<Alloc
>::deallocate(alloc
, pool
, 1);
108 typedef CountDestroy
<CountDestructor
> Alloc
;
109 int destroys_called
= 0;
110 int destructors_called
= 0;
111 Alloc
alloc(&destroys_called
);
113 CountDestructor
* pool
= std::allocator_traits
<Alloc
>::allocate(alloc
, 1);
114 std::allocator_traits
<Alloc
>::construct(alloc
, pool
, &destructors_called
);
115 assert(destroys_called
== 0);
116 assert(destructors_called
== 0);
118 std::allocator_traits
<Alloc
>::destroy(alloc
, pool
);
119 assert(destroys_called
== 1);
120 assert(destructors_called
== 1);
122 std::allocator_traits
<Alloc
>::deallocate(alloc
, pool
, 1);
127 int main(int, char**)
130 #if TEST_STD_VER > 17
131 static_assert(test());