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 //===----------------------------------------------------------------------===//
11 // template <class T, class... Args>
12 // struct is_constructible;
14 #include <type_traits>
15 #include "test_macros.h"
22 #if TEST_STD_VER >= 11
29 struct Derived
: public Base
{};
33 virtual void foo() = 0;
36 class AbstractDestructor
38 virtual ~AbstractDestructor() = 0;
49 #if TEST_STD_VER >= 11
60 #if TEST_STD_VER >= 11
63 explicit operator To ();
69 void test_is_constructible()
71 static_assert( (std::is_constructible
<T
>::value
), "");
73 static_assert( std::is_constructible_v
<T
>, "");
77 template <class T
, class A0
>
78 void test_is_constructible()
80 static_assert(( std::is_constructible
<T
, A0
>::value
), "");
82 static_assert(( std::is_constructible_v
<T
, A0
>), "");
86 template <class T
, class A0
, class A1
>
87 void test_is_constructible()
89 static_assert(( std::is_constructible
<T
, A0
, A1
>::value
), "");
91 static_assert(( std::is_constructible_v
<T
, A0
, A1
>), "");
95 template <class T
, class A0
, class A1
, class A2
>
96 void test_is_constructible()
98 static_assert(( std::is_constructible
<T
, A0
, A1
, A2
>::value
), "");
100 static_assert(( std::is_constructible_v
<T
, A0
, A1
, A2
>), "");
105 void test_is_not_constructible()
107 static_assert((!std::is_constructible
<T
>::value
), "");
108 #if TEST_STD_VER > 14
109 static_assert((!std::is_constructible_v
<T
>), "");
113 template <class T
, class A0
>
114 void test_is_not_constructible()
116 static_assert((!std::is_constructible
<T
, A0
>::value
), "");
117 #if TEST_STD_VER > 14
118 static_assert((!std::is_constructible_v
<T
, A0
>), "");
122 int main(int, char**)
127 test_is_constructible
<int> ();
128 test_is_constructible
<int, const int> ();
129 test_is_constructible
<A
, int> ();
130 test_is_constructible
<A
, int, double> ();
131 test_is_constructible
<A
, int, long, double> ();
132 test_is_constructible
<int&, int&> ();
134 test_is_not_constructible
<A
> ();
135 #if TEST_STD_VER >= 11
136 test_is_not_constructible
<A
, char> ();
138 test_is_constructible
<A
, char> ();
140 test_is_not_constructible
<A
, void> ();
141 test_is_not_constructible
<int, void()>();
142 test_is_not_constructible
<int, void(&)()>();
143 test_is_not_constructible
<int, void() const>();
144 test_is_not_constructible
<int&, void>();
145 test_is_not_constructible
<int&, void()>();
146 test_is_not_constructible
<int&, void() const>();
147 test_is_not_constructible
<int&, void(&)()>();
149 test_is_not_constructible
<void> ();
150 test_is_not_constructible
<const void> (); // LWG 2738
151 test_is_not_constructible
<volatile void> ();
152 test_is_not_constructible
<const volatile void> ();
153 test_is_not_constructible
<int&> ();
154 test_is_not_constructible
<Abstract
> ();
155 test_is_not_constructible
<AbstractDestructor
> ();
156 test_is_constructible
<int, S
>();
157 test_is_not_constructible
<int&, S
>();
159 test_is_constructible
<void(&)(), void(&)()>();
160 test_is_constructible
<void(&)(), void()>();
161 #if TEST_STD_VER >= 11
162 test_is_constructible
<void(&&)(), void(&&)()>();
163 test_is_constructible
<void(&&)(), void()>();
164 test_is_constructible
<void(&&)(), void(&)()>();
167 #if TEST_STD_VER >= 11
168 test_is_constructible
<int const&, int>();
169 test_is_constructible
<int const&, int&&>();
171 test_is_constructible
<int&&, double&>();
172 test_is_constructible
<void(&)(), void(&&)()>();
174 test_is_not_constructible
<int&, int>();
175 test_is_not_constructible
<int&, int const&>();
176 test_is_not_constructible
<int&, int&&>();
178 test_is_constructible
<int&&, int>();
179 test_is_constructible
<int&&, int&&>();
180 test_is_not_constructible
<int&&, int&>();
181 test_is_not_constructible
<int&&, int const&&>();
183 test_is_constructible
<Base
, Derived
>();
184 test_is_constructible
<Base
&, Derived
&>();
185 test_is_not_constructible
<Derived
&, Base
&>();
186 test_is_constructible
<Base
const&, Derived
const&>();
187 #ifndef TEST_COMPILER_GCC
188 test_is_not_constructible
<Derived
const&, Base
const&>();
189 test_is_not_constructible
<Derived
const&, Base
>();
192 test_is_constructible
<Base
&&, Derived
>();
193 test_is_constructible
<Base
&&, Derived
&&>();
194 #ifndef TEST_COMPILER_GCC
195 test_is_not_constructible
<Derived
&&, Base
&&>();
196 test_is_not_constructible
<Derived
&&, Base
>();
199 // test that T must also be destructible
200 test_is_constructible
<PrivateDtor
&, PrivateDtor
&>();
201 test_is_not_constructible
<PrivateDtor
, int>();
203 test_is_not_constructible
<void() const, void() const>();
204 test_is_not_constructible
<void() const, void*>();
206 test_is_constructible
<int&, ImplicitTo
<int&>>();
207 test_is_constructible
<const int&, ImplicitTo
<int&&>>();
208 test_is_constructible
<int&&, ImplicitTo
<int&&>>();
209 test_is_constructible
<const int&, ImplicitTo
<int>>();
211 test_is_not_constructible
<B
&&, B
&>();
212 test_is_not_constructible
<B
&&, D
&>();
213 test_is_constructible
<B
&&, ImplicitTo
<D
&&>>();
214 test_is_constructible
<B
&&, ImplicitTo
<D
&&>&>();
215 test_is_constructible
<int&&, double&>();
216 test_is_constructible
<const int&, ImplicitTo
<int&>&>();
217 test_is_constructible
<const int&, ImplicitTo
<int&>>();
218 test_is_constructible
<const int&, ExplicitTo
<int&>&>();
219 test_is_constructible
<const int&, ExplicitTo
<int&>>();
221 test_is_constructible
<const int&, ExplicitTo
<int&>&>();
222 test_is_constructible
<const int&, ExplicitTo
<int&>>();
225 // Binding through reference-compatible type is required to perform
226 // direct-initialization as described in [over.match.ref] p. 1 b. 1:
228 // But the rvalue to lvalue reference binding isn't allowed according to
229 // [over.match.ref] despite Clang accepting it.
230 test_is_constructible
<int&, ExplicitTo
<int&>>();
231 #ifndef TEST_COMPILER_GCC
232 test_is_constructible
<const int&, ExplicitTo
<int&&>>();
235 static_assert(std::is_constructible
<int&&, ExplicitTo
<int&&>>::value
, "");
238 // FIXME Clang and GCC disagree on the validity of this expression.
239 test_is_constructible
<const int&, ExplicitTo
<int>>();
240 static_assert(std::is_constructible
<int&&, ExplicitTo
<int>>::value
, "");
242 test_is_not_constructible
<const int&, ExplicitTo
<int>>();
243 test_is_not_constructible
<int&&, ExplicitTo
<int>>();
246 // Binding through temporary behaves like copy-initialization,
247 // see [dcl.init.ref] p. 5, very last sub-bullet:
248 test_is_not_constructible
<const int&, ExplicitTo
<double&&>>();
249 test_is_not_constructible
<int&&, ExplicitTo
<double&&>>();
251 test_is_not_constructible
<void()>();
252 test_is_not_constructible
<void() const> ();
253 test_is_not_constructible
<void() volatile> ();
254 test_is_not_constructible
<void() &> ();
255 test_is_not_constructible
<void() &&> ();
256 #endif // TEST_STD_VER >= 11