]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/allocator/89510.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / allocator / 89510.cc
CommitLineData
a945c346 1// Copyright (C) 2020-2024 Free Software Foundation, Inc.
b1983f45
JW
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 compile { target c++11 } }
7cc9022f 19// { dg-require-effective-target hosted }
b1983f45
JW
20
21#include <memory>
22
23using AT = std::allocator_traits<std::allocator<int>>;
24
25template<typename...> using void_t = void;
26
27template<typename T, typename U, typename = void>
28struct has_construct
29: std::false_type
30{ };
31
32template<typename T, typename U>
33struct has_construct<T, U,
34 void_t<decltype(std::declval<T&>().construct(std::declval<U*>()))>>
35: std::true_type
36{ };
37
38template<typename T, typename U, typename = void>
39struct has_destroy
40: std::false_type
41{ };
42
43template<typename T, typename U>
44struct has_destroy<T, U,
45 void_t<decltype(std::declval<T&>().destroy(std::declval<U*>()))>>
46: std::true_type
47{ };
48
49template<typename T, typename U, typename = void>
50struct has_traits_construct
51: std::false_type
52{ };
53
54template<typename T, typename U>
55struct has_traits_construct<T, U,
56 void_t<decltype(AT::construct(std::declval<T&>(), std::declval<U*>()))>>
57: std::true_type
58{ };
59
60template<typename T, typename U, typename = void>
61struct has_traits_destroy
62: std::false_type
63{ };
64
65template<typename T, typename U>
66struct has_traits_destroy<T, U,
67 void_t<decltype(AT::destroy(std::declval<T&>(), std::declval<U*>()))>>
68: std::true_type
69{ };
70
71struct NoDefault { NoDefault(int); };
72struct NoDest { private: ~NoDest(); };
73
74// Whether true or false, this should not give an error:
75constexpr bool c = has_construct<std::allocator<NoDefault>, NoDefault>::value;
76constexpr bool cv = has_construct<std::allocator<void>, NoDefault>::value;
77constexpr bool c2 = has_traits_construct<std::allocator<int>, NoDefault>::value;
78constexpr bool d = has_destroy<std::allocator<NoDest>, NoDest>::value;
79constexpr bool d2 = has_traits_destroy<std::allocator<int>, NoDest>::value;
80
81std::allocator<int> a;
82
83long* lp;
84#if __cplusplus <= 201703L
85static_assert( noexcept(a.construct(lp)), "" );
86static_assert( noexcept(a.construct(lp, 1L)), "" );
87static_assert( noexcept(a.construct(lp, 2)), "" );
88static_assert( noexcept(a.construct(lp, 2U)), "" );
89static_assert( noexcept(a.destroy(lp)), "" );
90#endif
91static_assert( noexcept(AT::construct(a, lp)), "" );
92static_assert( noexcept(AT::construct(a, lp, 1L)), "" );
93static_assert( noexcept(AT::construct(a, lp, 2)), "" );
94static_assert( noexcept(AT::construct(a, lp, 2U)), "" );
95static_assert( noexcept(AT::destroy(a, lp)), "" );
96
97struct X
98{
99 X() noexcept;
100 X(int) noexcept;
101 ~X() noexcept;
102};
103
104X* xp;
105#if __cplusplus <= 201703L
106static_assert( noexcept(a.construct(xp)), "" );
107static_assert( noexcept(a.construct(xp, 1)), "" );
108static_assert( noexcept(a.destroy(xp)), "" );
109#endif
110static_assert( noexcept(AT::construct(a, xp)), "" );
111static_assert( noexcept(AT::construct(a, xp, 1)), "" );
112static_assert( noexcept(AT::destroy(a, xp)), "" );
113
114struct Y
115{
116 Y() noexcept;
117 Y(int) noexcept(false);
118 ~Y() noexcept;
119};
120
121Y* yp;
122#if __cplusplus <= 201703L
123static_assert( noexcept(a.construct(yp)), "" );
124static_assert( ! noexcept(a.construct(yp, 1)), "" );
125static_assert( noexcept(a.destroy(yp)), "" );
126#endif
127static_assert( noexcept(AT::construct(a, yp)), "" );
128static_assert( ! noexcept(AT::construct(a, yp, 1)), "" );
129static_assert( noexcept(AT::destroy(a, yp)), "" );
130
131struct Z
132{
133 Z() noexcept;
134 Z(int) noexcept;
135 ~Z() noexcept(false);
136};
137
138Z* zp;
139// These construct calls should be noexcept, but they are false because
140// they use is_nothrow_constructible which depends on is_nothrow_destructible.
141#if __cplusplus <= 201703L
142static_assert( ! noexcept(a.construct(zp)), "wrong" );
143static_assert( ! noexcept(a.construct(zp, 1)), "wrong" );
144static_assert( ! noexcept(a.destroy(zp)), "" );
145#endif
146static_assert( ! noexcept(AT::construct(a, zp)), "" );
147static_assert( ! noexcept(AT::construct(a, zp, 1)), "" );
148static_assert( ! noexcept(AT::destroy(a, zp)), "" );