• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static_assert((!std::is_convertible<ConstFunction, Function>::value), "");
124     static_assert((!std::is_convertible<Function*, ConstFunction>::value), "");
125     static_assert((!std::is_convertible<Function&, ConstFunction>::value), "");
126     static_assert((!std::is_convertible<ConstFunction, ConstFunction>::value), "");
127     static_assert((!std::is_convertible<ConstFunction, void>::value), "");
128 
129     // Array
130     test_is_not_convertible<Array, void> ();
131     test_is_not_convertible<Array, Function> ();
132     test_is_not_convertible<Array, Function&> ();
133     test_is_not_convertible<Array, Function*> ();
134     test_is_not_convertible<Array, Array> ();
135 
136     static_assert((!std::is_convertible<Array, Array&>::value), "");
137     static_assert(( std::is_convertible<Array, const Array&>::value), "");
138     static_assert((!std::is_convertible<Array, const volatile Array&>::value), "");
139 
140     static_assert((!std::is_convertible<const Array, Array&>::value), "");
141     static_assert(( std::is_convertible<const Array, const Array&>::value), "");
142     static_assert((!std::is_convertible<Array, volatile Array&>::value), "");
143     static_assert((!std::is_convertible<Array, const volatile Array&>::value), "");
144 
145 #if TEST_STD_VER >= 11
146     static_assert(( std::is_convertible<Array, Array&&>::value), "");
147     static_assert(( std::is_convertible<Array, const Array&&>::value), "");
148     static_assert(( std::is_convertible<Array, volatile Array&&>::value), "");
149     static_assert(( std::is_convertible<Array, const volatile Array&&>::value), "");
150     static_assert(( std::is_convertible<const Array, const Array&&>::value), "");
151     static_assert((!std::is_convertible<Array&, Array&&>::value), "");
152     static_assert((!std::is_convertible<Array&&, Array&>::value), "");
153 #endif
154 
155     test_is_not_convertible<Array, char> ();
156     test_is_not_convertible<Array, char&> ();
157 
158     static_assert(( std::is_convertible<Array, char*>::value), "");
159     static_assert(( std::is_convertible<Array, const char*>::value), "");
160     static_assert(( std::is_convertible<Array, char* const>::value), "");
161     static_assert(( std::is_convertible<Array, char* const volatile>::value), "");
162 
163     static_assert((!std::is_convertible<const Array, char*>::value), "");
164     static_assert(( std::is_convertible<const Array, const char*>::value), "");
165 
166     static_assert((!std::is_convertible<char[42][42], char*>::value), "");
167     static_assert((!std::is_convertible<char[][1], char*>::value), "");
168 
169     // Array&
170     test_is_not_convertible<Array&, void> ();
171     test_is_not_convertible<Array&, Function> ();
172     test_is_not_convertible<Array&, Function&> ();
173     test_is_not_convertible<Array&, Function*> ();
174     test_is_not_convertible<Array&, Array> ();
175 
176     static_assert(( std::is_convertible<Array&, Array&>::value), "");
177     static_assert(( std::is_convertible<Array&, const Array&>::value), "");
178     static_assert((!std::is_convertible<const Array&, Array&>::value), "");
179     static_assert(( std::is_convertible<const Array&, const Array&>::value), "");
180 
181     test_is_not_convertible<Array&, char> ();
182     test_is_not_convertible<Array&, char&> ();
183 
184     static_assert(( std::is_convertible<Array&, char*>::value), "");
185     static_assert(( std::is_convertible<Array&, const char*>::value), "");
186     static_assert((!std::is_convertible<const Array&, char*>::value), "");
187     static_assert(( std::is_convertible<const Array&, const char*>::value), "");
188 
189     static_assert((std::is_convertible<Array, StringType>::value), "");
190     static_assert((std::is_convertible<char(&)[], StringType>::value), "");
191 
192     // char
193     test_is_not_convertible<char, void> ();
194     test_is_not_convertible<char, Function> ();
195     test_is_not_convertible<char, Function&> ();
196     test_is_not_convertible<char, Function*> ();
197     test_is_not_convertible<char, Array> ();
198     test_is_not_convertible<char, Array&> ();
199 
200     test_is_convertible<char, char> ();
201 
202     static_assert((!std::is_convertible<char, char&>::value), "");
203     static_assert(( std::is_convertible<char, const char&>::value), "");
204     static_assert((!std::is_convertible<const char, char&>::value), "");
205     static_assert(( std::is_convertible<const char, const char&>::value), "");
206 
207     test_is_not_convertible<char, char*> ();
208 
209     // char&
210     test_is_not_convertible<char&, void> ();
211     test_is_not_convertible<char&, Function> ();
212     test_is_not_convertible<char&, Function&> ();
213     test_is_not_convertible<char&, Function*> ();
214     test_is_not_convertible<char&, Array> ();
215     test_is_not_convertible<char&, Array&> ();
216 
217     test_is_convertible<char&, char> ();
218 
219     static_assert(( std::is_convertible<char&, char&>::value), "");
220     static_assert(( std::is_convertible<char&, const char&>::value), "");
221     static_assert((!std::is_convertible<const char&, char&>::value), "");
222     static_assert(( std::is_convertible<const char&, const char&>::value), "");
223 
224     test_is_not_convertible<char&, char*> ();
225 
226     // char*
227     test_is_not_convertible<char*, void> ();
228     test_is_not_convertible<char*, Function> ();
229     test_is_not_convertible<char*, Function&> ();
230     test_is_not_convertible<char*, Function*> ();
231     test_is_not_convertible<char*, Array> ();
232     test_is_not_convertible<char*, Array&> ();
233 
234     test_is_not_convertible<char*, char> ();
235     test_is_not_convertible<char*, char&> ();
236 
237     static_assert(( std::is_convertible<char*, char*>::value), "");
238     static_assert(( std::is_convertible<char*, const char*>::value), "");
239     static_assert((!std::is_convertible<const char*, char*>::value), "");
240     static_assert(( std::is_convertible<const char*, const char*>::value), "");
241 
242     // NonCopyable
243     static_assert((std::is_convertible<NonCopyable&, NonCopyable&>::value), "");
244     static_assert((std::is_convertible<NonCopyable&, const NonCopyable&>::value), "");
245     static_assert((std::is_convertible<NonCopyable&, const volatile NonCopyable&>::value), "");
246     static_assert((std::is_convertible<NonCopyable&, volatile NonCopyable&>::value), "");
247     static_assert((std::is_convertible<const NonCopyable&, const NonCopyable&>::value), "");
248     static_assert((std::is_convertible<const NonCopyable&, const volatile NonCopyable&>::value), "");
249     static_assert((std::is_convertible<volatile NonCopyable&, const volatile NonCopyable&>::value), "");
250     static_assert((std::is_convertible<const volatile NonCopyable&, const volatile NonCopyable&>::value), "");
251     static_assert((!std::is_convertible<const NonCopyable&, NonCopyable&>::value), "");
252 // This test requires Access control SFINAE which we only have in C++11 or when
253 // we are using the compiler builtin for is_convertible.
254 #if TEST_STD_VER >= 11 || !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK)
255     test_is_not_convertible<NonCopyable&, NonCopyable>();
256 #endif
257 
258     // Ensure that CannotInstantiate is not instantiated by is_convertible when it is not needed.
259     // For example CannotInstantiate is instatiated as a part of ADL lookup for arguments of type CannotInstantiate*.
260     static_assert((std::is_convertible<CannotInstantiate<int>*, CannotInstantiate<int>*>::value), "");
261 }
262