| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <memory> |
| 10 |
|
| 11 |
// template <class Alloc> |
| 12 |
// struct allocator_traits |
| 13 |
// { |
| 14 |
// static constexpr pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); |
| 15 |
// ... |
| 16 |
// }; |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
#include <cstdint> |
| 20 |
#include <cassert> |
| 21 |
|
| 22 |
#include "test_macros.h" |
| 23 |
#include "incomplete_type_helper.h" |
| 24 |
|
| 25 |
template <class T> |
| 26 |
struct A |
| 27 |
{ |
| 28 |
typedef T value_type; |
| 29 |
|
| 30 |
TEST_CONSTEXPR_CXX20 A() {} |
| 31 |
|
| 32 |
TEST_CONSTEXPR_CXX20 value_type* allocate(std::size_t n) |
| 33 |
{ |
| 34 |
assert(n == 10); |
| 35 |
return &storage; |
| 36 |
} |
| 37 |
|
| 38 |
value_type storage; |
| 39 |
}; |
| 40 |
|
| 41 |
template <class T> |
| 42 |
struct B |
| 43 |
{ |
| 44 |
typedef T value_type; |
| 45 |
|
| 46 |
TEST_CONSTEXPR_CXX20 value_type* allocate(std::size_t n, const void* p) |
| 47 |
{ |
| 48 |
assert(n == 11); |
| 49 |
assert(p == nullptr); |
| 50 |
return &storage; |
| 51 |
} |
| 52 |
|
| 53 |
value_type storage; |
| 54 |
}; |
| 55 |
|
| 56 |
TEST_CONSTEXPR_CXX20 bool test() |
| 57 |
{ |
| 58 |
#if TEST_STD_VER >= 11 |
| 59 |
{ |
| 60 |
A<int> a; |
| 61 |
assert(std::allocator_traits<A<int> >::allocate(a, 10, nullptr) == &a.storage); |
| 62 |
} |
| 63 |
{ |
| 64 |
typedef A<IncompleteHolder*> Alloc; |
| 65 |
Alloc a; |
| 66 |
assert(std::allocator_traits<Alloc>::allocate(a, 10, nullptr) == &a.storage); |
| 67 |
} |
| 68 |
#endif |
| 69 |
{ |
| 70 |
B<int> b; |
| 71 |
assert(std::allocator_traits<B<int> >::allocate(b, 11, nullptr) == &b.storage); |
| 72 |
} |
| 73 |
{ |
| 74 |
typedef B<IncompleteHolder*> Alloc; |
| 75 |
Alloc b; |
| 76 |
assert(std::allocator_traits<Alloc>::allocate(b, 11, nullptr) == &b.storage); |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
int main(int, char**) |
| 84 |
{ |
| 85 |
test(); |
| 86 |
#if TEST_STD_VER > 17 |
| 87 |
static_assert(test()); |
| 88 |
#endif |
| 89 |
return 0; |
| 90 |
} |