Debian Codegraph @

libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/1.cc

raw
1 // Copyright (C) 2019-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++20 } }
19
20 #include <memory>
21 #include <testsuite_hooks.h>
22
23 template<typename T, typename... Args>
24 concept can_construct_at = requires(T* p, Args&&... args)
25 {
26 p = std::construct_at(p, std::forward<Args>(args)...);
27 };
28
29 static_assert( can_construct_at<int> );
30 static_assert( can_construct_at<int, int> );
31 static_assert( !can_construct_at<int, int, int> );
32
33 // Not required by C++20:
34 static_assert( noexcept(std::construct_at(std::declval<int*>(), 1)) );
35
36 void
37 test01()
38 {
39 int i = -1;
40 auto p = std::construct_at(&i);
41 VERIFY( p == &i );
42 VERIFY( i == 0 );
43 p = std::construct_at(&i, 42);
44 VERIFY( p == &i );
45 VERIFY( i == 42 );
46 }
47
48 struct X {
49 X(char&, void*) { }
50 };
51
52 static_assert( can_construct_at<X, char&, void*> );
53 static_assert( !can_construct_at<X> );
54 static_assert( !can_construct_at<X, char> );
55 static_assert( !can_construct_at<X, char&, const void*> );
56
57 static_assert( !noexcept(std::construct_at(std::declval<X*>(), std::declval<char&>(), std::declval<void*>())) );
58
59 int
60 main()
61 {
62 test01();
63 }