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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template <class T, class ...Args>
14 // constexpr T* construct_at(T* location, Args&& ...args);
21 #include "test_iterators.h"
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_
;
39 constexpr Counted(int& count
) : count_(count
) { ++count
; }
40 constexpr Counted(Counted
const& that
) : count_(that
.count_
) { ++count_
; }
41 constexpr ~Counted() { --count_
; }
48 int* res
= std::construct_at(&i
);
55 int* res
= std::construct_at(&i
, 42);
63 Foo
* res
= std::construct_at(&foo
, 42, 'x', 123.89, &count
);
65 assert(*res
== Foo(42, 'x', 123.89));
70 std::allocator
<Counted
> a
;
71 Counted
* p
= a
.allocate(2);
73 std::construct_at(p
, count
);
75 std::construct_at(p
+1, count
);
87 template <class ...Args
>
88 constexpr bool can_construct_at
= requires
{
89 std::construct_at(std::declval
<Args
>()...);
92 // Check that SFINAE works.
93 static_assert( can_construct_at
<int*, int>);
94 static_assert( can_construct_at
<Foo
*, int, char, double>);
95 static_assert(!can_construct_at
<Foo
*, int, char>);
96 static_assert(!can_construct_at
<Foo
*, int, char, double, int>);
97 static_assert(!can_construct_at
<std::nullptr_t
, int, char, double>);
98 static_assert(!can_construct_at
<int*, int, char, double>);
99 static_assert(!can_construct_at
<contiguous_iterator
<Foo
*>, int, char, double>);
100 // Can't construct function pointers.
101 static_assert(!can_construct_at
<int(*)()>);
102 static_assert(!can_construct_at
<int(*)(), std::nullptr_t
>);
104 int main(int, char**) {
106 static_assert(test());