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 // type_traits
10
11 // is_convertible
12
13 #include <type_traits>
14 #include "test_macros.h"
15
16 template <class T, class U>
test_is_convertible()17 void test_is_convertible()
18 {
19 static_assert((std::is_convertible<T, U>::value), "");
20 static_assert((std::is_convertible<const T, U>::value), "");
21 static_assert((std::is_convertible<T, const U>::value), "");
22 static_assert((std::is_convertible<const T, const U>::value), "");
23 #if TEST_STD_VER > 14
24 static_assert((std::is_convertible_v<T, U>), "");
25 static_assert((std::is_convertible_v<const T, U>), "");
26 static_assert((std::is_convertible_v<T, const U>), "");
27 static_assert((std::is_convertible_v<const T, const U>), "");
28 #endif
29 }
30
31 template <class T, class U>
test_is_not_convertible()32 void test_is_not_convertible()
33 {
34 static_assert((!std::is_convertible<T, U>::value), "");
35 static_assert((!std::is_convertible<const T, U>::value), "");
36 static_assert((!std::is_convertible<T, const U>::value), "");
37 static_assert((!std::is_convertible<const T, const U>::value), "");
38 #if TEST_STD_VER > 14
39 static_assert((!std::is_convertible_v<T, U>), "");
40 static_assert((!std::is_convertible_v<const T, U>), "");
41 static_assert((!std::is_convertible_v<T, const U>), "");
42 static_assert((!std::is_convertible_v<const T, const U>), "");
43 #endif
44 }
45
46 typedef void Function();
47 typedef void ConstFunction() const;
48 typedef char Array[1];
49
50 struct StringType {
StringTypeStringType51 StringType(const char*) {}
52 };
53
54 class NonCopyable {
55 NonCopyable(NonCopyable&);
56 };
57
58 template <typename T>
59 class CannotInstantiate {
60 enum { X = T::ThisExpressionWillBlowUp };
61 };
62
63 struct abstract { virtual int f() = 0; };
64
main(int,char **)65 int main(int, char**)
66 {
67 // void
68 test_is_convertible<void,void> ();
69 test_is_not_convertible<void,Function> ();
70 test_is_not_convertible<void,Function&> ();
71 test_is_not_convertible<void,Function*> ();
72 test_is_not_convertible<void,Array> ();
73 test_is_not_convertible<void,Array&> ();
74 test_is_not_convertible<void,char> ();
75 test_is_not_convertible<void,char&> ();
76 test_is_not_convertible<void,char*> ();
77 test_is_not_convertible<char, void>();
78
79 // Function
80 test_is_not_convertible<Function, void> ();
81 test_is_not_convertible<Function, Function> ();
82 test_is_convertible<Function, Function&> ();
83 test_is_convertible<Function, Function*> ();
84 test_is_convertible<Function, Function*const> ();
85
86 static_assert(( std::is_convertible<Function, Function&&>::value), "");
87
88 test_is_not_convertible<Function, Array> ();
89 test_is_not_convertible<Function, Array&> ();
90 test_is_not_convertible<Function, char> ();
91 test_is_not_convertible<Function, char&> ();
92 test_is_not_convertible<Function, char*> ();
93
94 // Function&
95 test_is_not_convertible<Function&, void> ();
96 test_is_not_convertible<Function&, Function> ();
97 test_is_convertible<Function&, Function&> ();
98
99 test_is_convertible<Function&, Function*> ();
100 test_is_not_convertible<Function&, Array> ();
101 test_is_not_convertible<Function&, Array&> ();
102 test_is_not_convertible<Function&, char> ();
103 test_is_not_convertible<Function&, char&> ();
104 test_is_not_convertible<Function&, char*> ();
105
106 // Function*
107 test_is_not_convertible<Function*, void> ();
108 test_is_not_convertible<Function*, Function> ();
109 test_is_not_convertible<Function*, Function&> ();
110 test_is_convertible<Function*, Function*> ();
111
112 test_is_not_convertible<Function*, Array> ();
113 test_is_not_convertible<Function*, Array&> ();
114 test_is_not_convertible<Function*, char> ();
115 test_is_not_convertible<Function*, char&> ();
116 test_is_not_convertible<Function*, char*> ();
117
118 // Non-referencable function type
119 static_assert((!std::is_convertible<ConstFunction, Function>::value), "");
120 static_assert((!std::is_convertible<ConstFunction, Function*>::value), "");
121 static_assert((!std::is_convertible<ConstFunction, Function&>::value), "");
122 static_assert((!std::is_convertible<ConstFunction, Function&&>::value), "");
123 static_assert((!std::is_convertible<Function*, ConstFunction>::value), "");
124 static_assert((!std::is_convertible<Function&, ConstFunction>::value), "");
125 static_assert((!std::is_convertible<ConstFunction, ConstFunction>::value), "");
126 static_assert((!std::is_convertible<ConstFunction, void>::value), "");
127
128 // Array
129 test_is_not_convertible<Array, void> ();
130 test_is_not_convertible<Array, Function> ();
131 test_is_not_convertible<Array, Function&> ();
132 test_is_not_convertible<Array, Function*> ();
133 test_is_not_convertible<Array, Array> ();
134
135 static_assert((!std::is_convertible<Array, Array&>::value), "");
136 static_assert(( std::is_convertible<Array, const Array&>::value), "");
137 static_assert((!std::is_convertible<Array, const volatile Array&>::value), "");
138
139 static_assert((!std::is_convertible<const Array, Array&>::value), "");
140 static_assert(( std::is_convertible<const Array, const Array&>::value), "");
141 static_assert((!std::is_convertible<Array, volatile Array&>::value), "");
142 static_assert((!std::is_convertible<Array, const volatile Array&>::value), "");
143
144 static_assert(( std::is_convertible<Array, Array&&>::value), "");
145 static_assert(( std::is_convertible<Array, const Array&&>::value), "");
146 static_assert(( std::is_convertible<Array, volatile Array&&>::value), "");
147 static_assert(( std::is_convertible<Array, const volatile Array&&>::value), "");
148 static_assert(( std::is_convertible<const Array, const Array&&>::value), "");
149 static_assert((!std::is_convertible<Array&, Array&&>::value), "");
150 static_assert((!std::is_convertible<Array&&, Array&>::value), "");
151
152 test_is_not_convertible<Array, char> ();
153 test_is_not_convertible<Array, char&> ();
154
155 static_assert(( std::is_convertible<Array, char*>::value), "");
156 static_assert(( std::is_convertible<Array, const char*>::value), "");
157 static_assert(( std::is_convertible<Array, char* const>::value), "");
158 static_assert(( std::is_convertible<Array, char* const volatile>::value), "");
159
160 static_assert((!std::is_convertible<const Array, char*>::value), "");
161 static_assert(( std::is_convertible<const Array, const char*>::value), "");
162
163 static_assert((!std::is_convertible<char[42][42], char*>::value), "");
164 static_assert((!std::is_convertible<char[][1], char*>::value), "");
165
166 // Array&
167 test_is_not_convertible<Array&, void> ();
168 test_is_not_convertible<Array&, Function> ();
169 test_is_not_convertible<Array&, Function&> ();
170 test_is_not_convertible<Array&, Function*> ();
171 test_is_not_convertible<Array&, Array> ();
172
173 static_assert(( std::is_convertible<Array&, Array&>::value), "");
174 static_assert(( std::is_convertible<Array&, const Array&>::value), "");
175 static_assert((!std::is_convertible<const Array&, Array&>::value), "");
176 static_assert(( std::is_convertible<const Array&, const Array&>::value), "");
177
178 test_is_not_convertible<Array&, char> ();
179 test_is_not_convertible<Array&, char&> ();
180
181 static_assert(( std::is_convertible<Array&, char*>::value), "");
182 static_assert(( std::is_convertible<Array&, const char*>::value), "");
183 static_assert((!std::is_convertible<const Array&, char*>::value), "");
184 static_assert(( std::is_convertible<const Array&, const char*>::value), "");
185
186 static_assert((std::is_convertible<Array, StringType>::value), "");
187 static_assert((std::is_convertible<char(&)[], StringType>::value), "");
188
189 // char
190 test_is_not_convertible<char, void> ();
191 test_is_not_convertible<char, Function> ();
192 test_is_not_convertible<char, Function&> ();
193 test_is_not_convertible<char, Function*> ();
194 test_is_not_convertible<char, Array> ();
195 test_is_not_convertible<char, Array&> ();
196
197 test_is_convertible<char, char> ();
198
199 static_assert((!std::is_convertible<char, char&>::value), "");
200 static_assert(( std::is_convertible<char, const char&>::value), "");
201 static_assert((!std::is_convertible<const char, char&>::value), "");
202 static_assert(( std::is_convertible<const char, const char&>::value), "");
203
204 test_is_not_convertible<char, char*> ();
205
206 // char&
207 test_is_not_convertible<char&, void> ();
208 test_is_not_convertible<char&, Function> ();
209 test_is_not_convertible<char&, Function&> ();
210 test_is_not_convertible<char&, Function*> ();
211 test_is_not_convertible<char&, Array> ();
212 test_is_not_convertible<char&, Array&> ();
213
214 test_is_convertible<char&, char> ();
215
216 static_assert(( std::is_convertible<char&, char&>::value), "");
217 static_assert(( std::is_convertible<char&, const char&>::value), "");
218 static_assert((!std::is_convertible<const char&, char&>::value), "");
219 static_assert(( std::is_convertible<const char&, const char&>::value), "");
220
221 test_is_not_convertible<char&, char*> ();
222
223 // char*
224 test_is_not_convertible<char*, void> ();
225 test_is_not_convertible<char*, Function> ();
226 test_is_not_convertible<char*, Function&> ();
227 test_is_not_convertible<char*, Function*> ();
228 test_is_not_convertible<char*, Array> ();
229 test_is_not_convertible<char*, Array&> ();
230
231 test_is_not_convertible<char*, char> ();
232 test_is_not_convertible<char*, char&> ();
233
234 static_assert(( std::is_convertible<char*, char*>::value), "");
235 static_assert(( std::is_convertible<char*, const char*>::value), "");
236 static_assert((!std::is_convertible<const char*, char*>::value), "");
237 static_assert(( std::is_convertible<const char*, const char*>::value), "");
238
239 // NonCopyable
240 static_assert((std::is_convertible<NonCopyable&, NonCopyable&>::value), "");
241 static_assert((std::is_convertible<NonCopyable&, const NonCopyable&>::value), "");
242 static_assert((std::is_convertible<NonCopyable&, const volatile NonCopyable&>::value), "");
243 static_assert((std::is_convertible<NonCopyable&, volatile NonCopyable&>::value), "");
244 static_assert((std::is_convertible<const NonCopyable&, const NonCopyable&>::value), "");
245 static_assert((std::is_convertible<const NonCopyable&, const volatile NonCopyable&>::value), "");
246 static_assert((std::is_convertible<volatile NonCopyable&, const volatile NonCopyable&>::value), "");
247 static_assert((std::is_convertible<const volatile NonCopyable&, const volatile NonCopyable&>::value), "");
248 static_assert((!std::is_convertible<const NonCopyable&, NonCopyable&>::value), "");
249
250 // This test requires Access control SFINAE which we only have in C++11 or when
251 // we are using the compiler builtin for is_convertible.
252 test_is_not_convertible<NonCopyable&, NonCopyable>();
253
254
255 // Ensure that CannotInstantiate is not instantiated by is_convertible when it is not needed.
256 // For example CannotInstantiate is instantiated as a part of ADL lookup for arguments of type CannotInstantiate*.
257 static_assert((std::is_convertible<CannotInstantiate<int>*, CannotInstantiate<int>*>::value), "");
258
259 // Test for PR13592
260 static_assert(!std::is_convertible<abstract, abstract>::value, "");
261
262 return 0;
263 }
264