1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: libcpp-no-concepts
11
12 // template<class From, class To>
13 // concept convertible_to;
14
15 #include <concepts>
16 #include <type_traits>
17
18 namespace {
19 enum ClassicEnum { a, b };
20 enum class ScopedEnum { x, y };
21 struct Empty {};
22 using nullptr_t = decltype(nullptr);
23
24 template <class T, class U>
CheckConvertibleTo()25 void CheckConvertibleTo() {
26 static_assert(std::convertible_to<T, U>);
27 static_assert(std::convertible_to<const T, U>);
28 static_assert(std::convertible_to<T, const U>);
29 static_assert(std::convertible_to<const T, const U>);
30 }
31
32 template <class T, class U>
CheckNotConvertibleTo()33 void CheckNotConvertibleTo() {
34 static_assert(!std::convertible_to<T, U>);
35 static_assert(!std::convertible_to<const T, U>);
36 static_assert(!std::convertible_to<T, const U>);
37 static_assert(!std::convertible_to<const T, const U>);
38 }
39
40 template <class T, class U>
CheckIsConvertibleButNotConvertibleTo()41 void CheckIsConvertibleButNotConvertibleTo() {
42 // Sanity check T is either implicitly xor explicitly convertible to U.
43 static_assert(std::is_convertible_v<T, U>);
44 static_assert(std::is_convertible_v<const T, U>);
45 static_assert(std::is_convertible_v<T, const U>);
46 static_assert(std::is_convertible_v<const T, const U>);
47 CheckNotConvertibleTo<T, U>();
48 }
49
50 // Tests that should objectively return false (except for bool and nullptr_t)
51 template <class T>
CommonlyNotConvertibleTo()52 constexpr void CommonlyNotConvertibleTo() {
53 CheckNotConvertibleTo<T, void>();
54 CheckNotConvertibleTo<T, nullptr_t>();
55 CheckNotConvertibleTo<T, T*>();
56 CheckNotConvertibleTo<T, T Empty::*>();
57 CheckNotConvertibleTo<T, T (Empty::*)()>();
58 CheckNotConvertibleTo<T, T[sizeof(T)]>();
59 CheckNotConvertibleTo<T, T (*)()>();
60 CheckNotConvertibleTo<T, T (&)()>();
61 CheckNotConvertibleTo<T, T(&&)()>();
62 }
63
64 template <std::same_as<bool> >
CommonlyNotConvertibleTo()65 constexpr void CommonlyNotConvertibleTo() {
66 CheckNotConvertibleTo<bool, void>();
67 CheckNotConvertibleTo<bool, nullptr_t>();
68 CheckConvertibleTo<bool Empty::*, bool>();
69 CheckConvertibleTo<bool (Empty::*)(), bool>();
70 CheckConvertibleTo<bool[2], bool>();
71 CheckConvertibleTo<bool (*)(), bool>();
72 CheckConvertibleTo<bool (&)(), bool>();
73 CheckConvertibleTo<bool(&&)(), bool>();
74 }
75
76 template <std::same_as<nullptr_t> >
CommonlyNotConvertibleTo()77 constexpr void CommonlyNotConvertibleTo() {
78 CheckNotConvertibleTo<nullptr_t, void>();
79 CheckConvertibleTo<nullptr_t, nullptr_t>();
80 CheckConvertibleTo<nullptr_t, void*>();
81 CheckConvertibleTo<nullptr_t, int Empty::*>();
82 CheckConvertibleTo<nullptr_t, void (Empty::*)()>();
83 CheckNotConvertibleTo<nullptr_t, int[2]>();
84 CheckConvertibleTo<nullptr_t, void (*)()>();
85 CheckNotConvertibleTo<nullptr_t, void (&)()>();
86 CheckNotConvertibleTo<nullptr_t, void(&&)()>();
87 }
88 } // namespace
89
90 using Function = void();
91 using NoexceptFunction = void() noexcept;
92 using ConstFunction = void() const;
93 using Array = char[1];
94
95 struct StringType {
StringTypeStringType96 StringType(const char*) {}
97 };
98
99 class NonCopyable {
100 NonCopyable(NonCopyable&);
101 };
102
103 template <typename T>
104 class CannotInstantiate {
105 enum { X = T::ThisExpressionWillBlowUp };
106 };
107
108 struct abstract {
109 virtual int f() = 0;
110 };
111
112 struct ExplicitlyConvertible;
113 struct ImplicitlyConvertible;
114
115 struct ExplicitlyConstructible {
116 explicit ExplicitlyConstructible(int);
117 explicit ExplicitlyConstructible(ExplicitlyConvertible);
118 explicit ExplicitlyConstructible(ImplicitlyConvertible) = delete;
119 };
120
121 struct ExplicitlyConvertible {
operator ExplicitlyConstructibleExplicitlyConvertible122 explicit operator ExplicitlyConstructible() const {
123 return ExplicitlyConstructible(0);
124 }
125 };
126
127 struct ImplicitlyConstructible;
128
129 struct ImplicitlyConvertible {
130 operator ExplicitlyConstructible() const;
131 operator ImplicitlyConstructible() const = delete;
132 };
133
134 struct ImplicitlyConstructible {
135 ImplicitlyConstructible(ImplicitlyConvertible);
136 };
137
main(int,char **)138 int main(int, char**) {
139 // void
140 CheckConvertibleTo<void, void>();
141 CheckNotConvertibleTo<void, Function>();
142 CheckNotConvertibleTo<void, Function&>();
143 CheckNotConvertibleTo<void, Function*>();
144 CheckNotConvertibleTo<void, NoexceptFunction>();
145 CheckNotConvertibleTo<void, NoexceptFunction&>();
146 CheckNotConvertibleTo<void, NoexceptFunction*>();
147 CheckNotConvertibleTo<void, Array>();
148 CheckNotConvertibleTo<void, Array&>();
149 CheckNotConvertibleTo<void, char>();
150 CheckNotConvertibleTo<void, char&>();
151 CheckNotConvertibleTo<void, char*>();
152 CheckNotConvertibleTo<char, void>();
153
154 // Function
155 CheckNotConvertibleTo<Function, void>();
156 CheckNotConvertibleTo<Function, Function>();
157 CheckNotConvertibleTo<Function, NoexceptFunction>();
158 CheckNotConvertibleTo<Function, NoexceptFunction&>();
159 CheckNotConvertibleTo<Function, NoexceptFunction*>();
160 CheckNotConvertibleTo<Function, NoexceptFunction* const>();
161 CheckConvertibleTo<Function, Function&>();
162 CheckConvertibleTo<Function, Function*>();
163 CheckConvertibleTo<Function, Function* const>();
164
165 static_assert(std::convertible_to<Function, Function&&>);
166 static_assert(!std::convertible_to<Function, NoexceptFunction&&>);
167
168 CheckNotConvertibleTo<Function, Array>();
169 CheckNotConvertibleTo<Function, Array&>();
170 CheckNotConvertibleTo<Function, char>();
171 CheckNotConvertibleTo<Function, char&>();
172 CheckNotConvertibleTo<Function, char*>();
173
174 // Function&
175 CheckNotConvertibleTo<Function&, void>();
176 CheckNotConvertibleTo<Function&, Function>();
177 CheckConvertibleTo<Function&, Function&>();
178
179 CheckConvertibleTo<Function&, Function*>();
180 CheckNotConvertibleTo<Function&, Array>();
181 CheckNotConvertibleTo<Function&, Array&>();
182 CheckNotConvertibleTo<Function&, char>();
183 CheckNotConvertibleTo<Function&, char&>();
184 CheckNotConvertibleTo<Function&, char*>();
185
186 // Function*
187 CheckNotConvertibleTo<Function*, void>();
188 CheckNotConvertibleTo<Function*, Function>();
189 CheckNotConvertibleTo<Function*, Function&>();
190 CheckConvertibleTo<Function*, Function*>();
191
192 CheckNotConvertibleTo<Function*, Array>();
193 CheckNotConvertibleTo<Function*, Array&>();
194 CheckNotConvertibleTo<Function*, char>();
195 CheckNotConvertibleTo<Function*, char&>();
196 CheckNotConvertibleTo<Function*, char*>();
197
198 // Non-referencable function type
199 static_assert(!std::convertible_to<ConstFunction, Function>);
200 static_assert(!std::convertible_to<ConstFunction, Function*>);
201 static_assert(!std::convertible_to<ConstFunction, Function&>);
202 static_assert(!std::convertible_to<ConstFunction, Function&&>);
203 static_assert(!std::convertible_to<Function*, ConstFunction>);
204 static_assert(!std::convertible_to<Function&, ConstFunction>);
205 static_assert(!std::convertible_to<ConstFunction, ConstFunction>);
206 static_assert(!std::convertible_to<ConstFunction, void>);
207
208 // NoexceptFunction
209 CheckNotConvertibleTo<NoexceptFunction, void>();
210 CheckNotConvertibleTo<NoexceptFunction, Function>();
211 CheckNotConvertibleTo<NoexceptFunction, NoexceptFunction>();
212 CheckConvertibleTo<NoexceptFunction, NoexceptFunction&>();
213 CheckConvertibleTo<NoexceptFunction, NoexceptFunction*>();
214 CheckConvertibleTo<NoexceptFunction, NoexceptFunction* const>();
215 CheckConvertibleTo<NoexceptFunction, Function&>();
216 CheckConvertibleTo<NoexceptFunction, Function*>();
217 CheckConvertibleTo<NoexceptFunction, Function* const>();
218
219 static_assert(std::convertible_to<NoexceptFunction, Function&&>);
220 static_assert(std::convertible_to<NoexceptFunction, NoexceptFunction&&>);
221
222 CheckNotConvertibleTo<NoexceptFunction, Array>();
223 CheckNotConvertibleTo<NoexceptFunction, Array&>();
224 CheckNotConvertibleTo<NoexceptFunction, char>();
225 CheckNotConvertibleTo<NoexceptFunction, char&>();
226 CheckNotConvertibleTo<NoexceptFunction, char*>();
227
228 // NoexceptFunction&
229 CheckNotConvertibleTo<NoexceptFunction&, void>();
230 CheckNotConvertibleTo<NoexceptFunction&, Function>();
231 CheckNotConvertibleTo<NoexceptFunction&, NoexceptFunction>();
232 CheckConvertibleTo<NoexceptFunction&, Function&>();
233 CheckConvertibleTo<NoexceptFunction&, NoexceptFunction&>();
234
235 CheckConvertibleTo<NoexceptFunction&, Function*>();
236 CheckConvertibleTo<NoexceptFunction&, NoexceptFunction*>();
237 CheckNotConvertibleTo<NoexceptFunction&, Array>();
238 CheckNotConvertibleTo<NoexceptFunction&, Array&>();
239 CheckNotConvertibleTo<NoexceptFunction&, char>();
240 CheckNotConvertibleTo<NoexceptFunction&, char&>();
241 CheckNotConvertibleTo<NoexceptFunction&, char*>();
242
243 // NoexceptFunction*
244 CheckNotConvertibleTo<NoexceptFunction*, void>();
245 CheckNotConvertibleTo<NoexceptFunction*, Function>();
246 CheckNotConvertibleTo<NoexceptFunction*, Function&>();
247 CheckNotConvertibleTo<NoexceptFunction*, NoexceptFunction>();
248 CheckNotConvertibleTo<NoexceptFunction*, NoexceptFunction&>();
249 CheckConvertibleTo<NoexceptFunction*, Function*>();
250 CheckConvertibleTo<NoexceptFunction*, NoexceptFunction*>();
251
252 CheckNotConvertibleTo<NoexceptFunction*, Array>();
253 CheckNotConvertibleTo<NoexceptFunction*, Array&>();
254 CheckNotConvertibleTo<NoexceptFunction*, char>();
255 CheckNotConvertibleTo<NoexceptFunction*, char&>();
256 CheckNotConvertibleTo<NoexceptFunction*, char*>();
257
258 // Array
259 CheckNotConvertibleTo<Array, void>();
260 CheckNotConvertibleTo<Array, Function>();
261 CheckNotConvertibleTo<Array, Function&>();
262 CheckNotConvertibleTo<Array, Function*>();
263 CheckNotConvertibleTo<Array, NoexceptFunction>();
264 CheckNotConvertibleTo<Array, NoexceptFunction&>();
265 CheckNotConvertibleTo<Array, NoexceptFunction*>();
266 CheckNotConvertibleTo<Array, Array>();
267
268 static_assert(!std::convertible_to<Array, Array&>);
269 static_assert(std::convertible_to<Array, const Array&>);
270 static_assert(!std::convertible_to<Array, const volatile Array&>);
271
272 static_assert(!std::convertible_to<const Array, Array&>);
273 static_assert(std::convertible_to<const Array, const Array&>);
274 static_assert(!std::convertible_to<Array, volatile Array&>);
275 static_assert(!std::convertible_to<Array, const volatile Array&>);
276
277 static_assert(std::convertible_to<Array, Array&&>);
278 static_assert(std::convertible_to<Array, const Array&&>);
279 static_assert(std::convertible_to<Array, volatile Array&&>);
280 static_assert(std::convertible_to<Array, const volatile Array&&>);
281 static_assert(std::convertible_to<const Array, const Array&&>);
282 static_assert(!std::convertible_to<Array&, Array&&>);
283 static_assert(!std::convertible_to<Array&&, Array&>);
284
285 CheckNotConvertibleTo<Array, char>();
286 CheckNotConvertibleTo<Array, char&>();
287
288 static_assert(std::convertible_to<Array, char*>);
289 static_assert(std::convertible_to<Array, const char*>);
290 static_assert(std::convertible_to<Array, char* const>);
291 static_assert(std::convertible_to<Array, char* const volatile>);
292
293 static_assert(!std::convertible_to<const Array, char*>);
294 static_assert(std::convertible_to<const Array, const char*>);
295
296 static_assert(!std::convertible_to<char[42][42], char*>);
297 static_assert(!std::convertible_to<char[][1], char*>);
298
299 // Array&
300 CheckNotConvertibleTo<Array&, void>();
301 CheckNotConvertibleTo<Array&, Function>();
302 CheckNotConvertibleTo<Array&, Function&>();
303 CheckNotConvertibleTo<Array&, Function*>();
304 CheckNotConvertibleTo<Array&, NoexceptFunction>();
305 CheckNotConvertibleTo<Array&, NoexceptFunction&>();
306 CheckNotConvertibleTo<Array&, NoexceptFunction*>();
307 CheckNotConvertibleTo<Array&, Array>();
308
309 static_assert(std::convertible_to<Array&, Array&>);
310 static_assert(std::convertible_to<Array&, const Array&>);
311 static_assert(!std::convertible_to<const Array&, Array&>);
312 static_assert(std::convertible_to<const Array&, const Array&>);
313
314 CheckNotConvertibleTo<Array&, char>();
315 CheckNotConvertibleTo<Array&, char&>();
316
317 static_assert(std::convertible_to<Array&, char*>);
318 static_assert(std::convertible_to<Array&, const char*>);
319 static_assert(!std::convertible_to<const Array&, char*>);
320 static_assert(std::convertible_to<const Array&, const char*>);
321
322 static_assert(std::convertible_to<Array, StringType>);
323 static_assert(std::convertible_to<char(&)[], StringType>);
324
325 // char
326 CheckNotConvertibleTo<char, void>();
327 CheckNotConvertibleTo<char, Function>();
328 CheckNotConvertibleTo<char, Function&>();
329 CheckNotConvertibleTo<char, Function*>();
330 CheckNotConvertibleTo<char, NoexceptFunction>();
331 CheckNotConvertibleTo<char, NoexceptFunction&>();
332 CheckNotConvertibleTo<char, NoexceptFunction*>();
333 CheckNotConvertibleTo<char, Array>();
334 CheckNotConvertibleTo<char, Array&>();
335
336 CheckConvertibleTo<char, char>();
337
338 static_assert(!std::convertible_to<char, char&>);
339 static_assert(std::convertible_to<char, const char&>);
340 static_assert(!std::convertible_to<const char, char&>);
341 static_assert(std::convertible_to<const char, const char&>);
342
343 CheckNotConvertibleTo<char, char*>();
344
345 // char&
346 CheckNotConvertibleTo<char&, void>();
347 CheckNotConvertibleTo<char&, Function>();
348 CheckNotConvertibleTo<char&, Function&>();
349 CheckNotConvertibleTo<char&, Function*>();
350 CheckNotConvertibleTo<char&, NoexceptFunction>();
351 CheckNotConvertibleTo<char&, NoexceptFunction&>();
352 CheckNotConvertibleTo<char&, NoexceptFunction*>();
353 CheckNotConvertibleTo<char&, Array>();
354 CheckNotConvertibleTo<char&, Array&>();
355
356 CheckConvertibleTo<char&, char>();
357
358 static_assert(std::convertible_to<char&, char&>);
359 static_assert(std::convertible_to<char&, const char&>);
360 static_assert(!std::convertible_to<const char&, char&>);
361 static_assert(std::convertible_to<const char&, const char&>);
362
363 CheckNotConvertibleTo<char&, char*>();
364
365 // char*
366 CheckNotConvertibleTo<char*, void>();
367 CheckNotConvertibleTo<char*, Function>();
368 CheckNotConvertibleTo<char*, Function&>();
369 CheckNotConvertibleTo<char*, Function*>();
370 CheckNotConvertibleTo<char*, NoexceptFunction>();
371 CheckNotConvertibleTo<char*, NoexceptFunction&>();
372 CheckNotConvertibleTo<char*, NoexceptFunction*>();
373 CheckNotConvertibleTo<char*, Array>();
374 CheckNotConvertibleTo<char*, Array&>();
375
376 CheckNotConvertibleTo<char*, char>();
377 CheckNotConvertibleTo<char*, char&>();
378
379 static_assert(std::convertible_to<char*, char*>);
380 static_assert(std::convertible_to<char*, const char*>);
381 static_assert(!std::convertible_to<const char*, char*>);
382 static_assert(std::convertible_to<const char*, const char*>);
383
384 // NonCopyable
385 static_assert(std::convertible_to<NonCopyable&, NonCopyable&>);
386 static_assert(std::convertible_to<NonCopyable&, const NonCopyable&>);
387 static_assert(std::convertible_to<NonCopyable&, const volatile NonCopyable&>);
388 static_assert(std::convertible_to<NonCopyable&, volatile NonCopyable&>);
389 static_assert(std::convertible_to<const NonCopyable&, const NonCopyable&>);
390 static_assert(
391 std::convertible_to<const NonCopyable&, const volatile NonCopyable&>);
392 static_assert(
393 std::convertible_to<volatile NonCopyable&, const volatile NonCopyable&>);
394 static_assert(std::convertible_to<const volatile NonCopyable&,
395 const volatile NonCopyable&>);
396 static_assert(!std::convertible_to<const NonCopyable&, NonCopyable&>);
397
398 // This test requires Access control SFINAE which we only have in C++11 or when
399 // we are using the compiler builtin for convertible_to.
400 CheckNotConvertibleTo<NonCopyable&, NonCopyable>();
401
402 // Ensure that CannotInstantiate is not instantiated by convertible_to when it is not needed.
403 // For example CannotInstantiate is instantiated as a part of ADL lookup for arguments of type CannotInstantiate*.
404 static_assert(
405 std::convertible_to<CannotInstantiate<int>*, CannotInstantiate<int>*>);
406
407 // Test for PR13592
408 static_assert(!std::convertible_to<abstract, abstract>);
409
410 CommonlyNotConvertibleTo<int>();
411 CommonlyNotConvertibleTo<bool>();
412 CommonlyNotConvertibleTo<nullptr_t>();
413
414 CheckNotConvertibleTo<int, ExplicitlyConstructible>();
415 CheckNotConvertibleTo<ExplicitlyConvertible, ExplicitlyConstructible>();
416 CheckNotConvertibleTo<ExplicitlyConstructible, ExplicitlyConvertible>();
417 CheckIsConvertibleButNotConvertibleTo<ImplicitlyConvertible,
418 ExplicitlyConstructible>();
419 CheckNotConvertibleTo<ImplicitlyConstructible, ImplicitlyConvertible>();
420
421 return 0;
422 }
423