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