|
21 | 21 | #include "test_iterators.h" |
22 | 22 |
|
23 | 23 | struct Foo { |
24 | | - constexpr Foo() { } |
25 | | - constexpr Foo(int a, char b, double c) : a_(a), b_(b), c_(c) { } |
26 | | - constexpr Foo(int a, char b, double c, int* count) : Foo(a, b, c) { *count += 1; } |
27 | | - constexpr bool operator==(Foo const& other) const { |
28 | | - return a_ == other.a_ && b_ == other.b_ && c_ == other.c_; |
29 | | - } |
| 24 | + constexpr Foo() {} |
| 25 | + constexpr Foo(int a, char b, double c) : a_(a), b_(b), c_(c) {} |
| 26 | + constexpr Foo(int a, char b, double c, int* count) : Foo(a, b, c) { *count += 1; } |
| 27 | + constexpr bool operator==(Foo const& other) const { return a_ == other.a_ && b_ == other.b_ && c_ == other.c_; } |
30 | 28 |
|
31 | 29 | private: |
32 | | - int a_; |
33 | | - char b_; |
34 | | - double c_; |
| 30 | + int a_; |
| 31 | + char b_; |
| 32 | + double c_; |
35 | 33 | }; |
36 | 34 |
|
37 | 35 | struct Counted { |
38 | | - int& count_; |
39 | | - constexpr Counted(int& count) : count_(count) { ++count; } |
40 | | - constexpr Counted(Counted const& that) : count_(that.count_) { ++count_; } |
41 | | - constexpr ~Counted() { --count_; } |
| 36 | + int& count_; |
| 37 | + constexpr Counted(int& count) : count_(count) { ++count; } |
| 38 | + constexpr Counted(Counted const& that) : count_(that.count_) { ++count_; } |
| 39 | + constexpr ~Counted() { --count_; } |
42 | 40 | }; |
43 | 41 |
|
44 | | -constexpr bool test() |
45 | | -{ |
46 | | - { |
47 | | - int i = 99; |
48 | | - int* res = std::construct_at(&i); |
49 | | - assert(res == &i); |
50 | | - assert(*res == 0); |
51 | | - } |
52 | | - |
53 | | - { |
54 | | - int i = 0; |
55 | | - int* res = std::construct_at(&i, 42); |
56 | | - assert(res == &i); |
57 | | - assert(*res == 42); |
58 | | - } |
59 | | - |
60 | | - { |
61 | | - Foo foo = {}; |
62 | | - int count = 0; |
63 | | - Foo* res = std::construct_at(&foo, 42, 'x', 123.89, &count); |
64 | | - assert(res == &foo); |
65 | | - assert(*res == Foo(42, 'x', 123.89)); |
66 | | - assert(count == 1); |
67 | | - } |
68 | | - |
69 | | - { |
70 | | - std::allocator<Counted> a; |
71 | | - Counted* p = a.allocate(2); |
72 | | - int count = 0; |
73 | | - std::construct_at(p, count); |
74 | | - assert(count == 1); |
75 | | - std::construct_at(p+1, count); |
76 | | - assert(count == 2); |
77 | | - (p+1)->~Counted(); |
78 | | - assert(count == 1); |
79 | | - p->~Counted(); |
80 | | - assert(count == 0); |
81 | | - a.deallocate(p, 2); |
82 | | - } |
83 | | - |
84 | | - return true; |
| 42 | +constexpr bool test() { |
| 43 | + { |
| 44 | + int i = 99; |
| 45 | + int* res = std::construct_at(&i); |
| 46 | + assert(res == &i); |
| 47 | + assert(*res == 0); |
| 48 | + } |
| 49 | + |
| 50 | + { |
| 51 | + int i = 0; |
| 52 | + int* res = std::construct_at(&i, 42); |
| 53 | + assert(res == &i); |
| 54 | + assert(*res == 42); |
| 55 | + } |
| 56 | + |
| 57 | + { |
| 58 | + Foo foo = {}; |
| 59 | + int count = 0; |
| 60 | + Foo* res = std::construct_at(&foo, 42, 'x', 123.89, &count); |
| 61 | + assert(res == &foo); |
| 62 | + assert(*res == Foo(42, 'x', 123.89)); |
| 63 | + assert(count == 1); |
| 64 | + } |
| 65 | + |
| 66 | + { |
| 67 | + std::allocator<Counted> a; |
| 68 | + Counted* p = a.allocate(2); |
| 69 | + int count = 0; |
| 70 | + std::construct_at(p, count); |
| 71 | + assert(count == 1); |
| 72 | + std::construct_at(p + 1, count); |
| 73 | + assert(count == 2); |
| 74 | + (p + 1)->~Counted(); |
| 75 | + assert(count == 1); |
| 76 | + p->~Counted(); |
| 77 | + assert(count == 0); |
| 78 | + a.deallocate(p, 2); |
| 79 | + } |
| 80 | + |
| 81 | + return true; |
85 | 82 | } |
86 | 83 |
|
87 | | -template <class ...Args> |
88 | | -constexpr bool can_construct_at = requires { |
89 | | - std::construct_at(std::declval<Args>()...); |
90 | | -}; |
| 84 | +template <class... Args> |
| 85 | +constexpr bool can_construct_at = requires { std::construct_at(std::declval<Args>()...); }; |
91 | 86 |
|
92 | 87 | // Check that SFINAE works. |
93 | | -static_assert( can_construct_at<int*, int>); |
94 | | -static_assert( can_construct_at<Foo*, int, char, double>); |
| 88 | +static_assert(can_construct_at<int*, int>); |
| 89 | +static_assert(can_construct_at<Foo*, int, char, double>); |
95 | 90 | static_assert(!can_construct_at<Foo*, int, char>); |
96 | 91 | static_assert(!can_construct_at<Foo*, int, char, double, int>); |
97 | 92 | static_assert(!can_construct_at<std::nullptr_t, int, char, double>); |
98 | 93 | static_assert(!can_construct_at<int*, int, char, double>); |
99 | 94 | static_assert(!can_construct_at<contiguous_iterator<Foo*>, int, char, double>); |
100 | 95 | // Can't construct function pointers. |
101 | | -static_assert(!can_construct_at<int(*)()>); |
102 | | -static_assert(!can_construct_at<int(*)(), std::nullptr_t>); |
| 96 | +static_assert(!can_construct_at<int (*)()>); |
| 97 | +static_assert(!can_construct_at<int (*)(), std::nullptr_t>); |
103 | 98 |
|
104 | 99 | int main(int, char**) { |
105 | | - test(); |
106 | | - static_assert(test()); |
107 | | - return 0; |
| 100 | + test(); |
| 101 | + static_assert(test()); |
| 102 | + return 0; |
108 | 103 | } |
0 commit comments