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