]> gcc.gnu.org Git - gcc.git/blob - gcc/testsuite/g++.dg/ext/is_nothrow_convertible1.C
c++: Implement __is_{nothrow_,}convertible [PR106784]
[gcc.git] / gcc / testsuite / g++.dg / ext / is_nothrow_convertible1.C
1 // PR c++/106784
2 // { dg-do compile { target c++11 } }
3 // Like is_convertible1.C, but conversion functions are made noexcept.
4
5 #define SA(X) static_assert((X),#X)
6
7 template<typename From, typename To>
8 struct is_nothrow_convertible {
9 static const bool value = __is_nothrow_convertible(From, To);
10 };
11
12 struct from_int {
13 from_int(int) noexcept;
14 };
15
16 struct from_charp {
17 from_charp(const char *) noexcept;
18 };
19
20 struct to_int {
21 operator int() noexcept;
22 };
23
24 typedef int Fn(int);
25 typedef char Arr[10];
26 enum E { XYZZY };
27
28 SA(!__is_nothrow_convertible(int, void));
29 SA(__is_nothrow_convertible(int, int));
30 SA(__is_nothrow_convertible(int, from_int));
31 SA(__is_nothrow_convertible(long, from_int));
32 SA(__is_nothrow_convertible(double, from_int));
33 SA(__is_nothrow_convertible(const int, from_int));
34 SA(__is_nothrow_convertible(const int&, from_int));
35 SA(__is_nothrow_convertible(to_int, int));
36 SA(__is_nothrow_convertible(to_int, const int&));
37 SA(__is_nothrow_convertible(to_int, long));
38 SA(!__is_nothrow_convertible(to_int, int&));
39 SA(!__is_nothrow_convertible(to_int, from_int));
40 SA(!__is_nothrow_convertible(int, Fn));
41 SA(!__is_nothrow_convertible(int, Fn*));
42 SA(!__is_nothrow_convertible(int, Fn&));
43 SA(!__is_nothrow_convertible(int, Arr));
44 SA(!__is_nothrow_convertible(int, Arr&));
45 SA(!__is_nothrow_convertible(int, int&));
46 SA(__is_nothrow_convertible(int, const int&));
47 SA(!__is_nothrow_convertible(const int, int&));
48 SA(__is_nothrow_convertible(const int, const int&));
49 SA(!__is_nothrow_convertible(int, int*));
50
51 SA(!__is_nothrow_convertible(int, E));
52 SA(__is_nothrow_convertible(E, int));
53
54 SA(__is_nothrow_convertible(int&, int));
55 SA(__is_nothrow_convertible(int&, int&));
56 SA(__is_nothrow_convertible(int&, const int&));
57 SA(!__is_nothrow_convertible(const int&, int&));
58 SA(__is_nothrow_convertible(const int&, const int&));
59 SA(!__is_nothrow_convertible(int&, int*));
60 SA(!__is_nothrow_convertible(int&, void));
61 SA(!__is_nothrow_convertible(int&, Fn));
62 SA(!__is_nothrow_convertible(int&, Fn*));
63 SA(!__is_nothrow_convertible(int&, Fn&));
64 SA(!__is_nothrow_convertible(int&, Arr));
65 SA(!__is_nothrow_convertible(int&, Arr&));
66
67 SA(!__is_nothrow_convertible(int*, int));
68 SA(!__is_nothrow_convertible(int*, int&));
69 SA(!__is_nothrow_convertible(int*, void));
70 SA(__is_nothrow_convertible(int*, int*));
71 SA(__is_nothrow_convertible(int*, const int*));
72 SA(!__is_nothrow_convertible(const int*, int*));
73 SA(__is_nothrow_convertible(const int*, const int*));
74 SA(!__is_nothrow_convertible(int*, Fn));
75 SA(!__is_nothrow_convertible(int*, Fn*));
76 SA(!__is_nothrow_convertible(int*, Fn&));
77 SA(!__is_nothrow_convertible(int*, Arr));
78 SA(!__is_nothrow_convertible(int*, Arr&));
79 SA(!__is_nothrow_convertible(int*, float*));
80
81 SA(__is_nothrow_convertible(void, void));
82 SA(!__is_nothrow_convertible(void, char));
83 SA(!__is_nothrow_convertible(void, char&));
84 SA(!__is_nothrow_convertible(void, char*));
85 SA(!__is_nothrow_convertible(char, void));
86 SA(__is_nothrow_convertible(const void, void));
87 SA(__is_nothrow_convertible(void, const void));
88 SA(__is_nothrow_convertible(const void, const void));
89 SA(!__is_nothrow_convertible(void, Fn));
90 SA(!__is_nothrow_convertible(void, Fn&));
91 SA(!__is_nothrow_convertible(void, Fn*));
92 SA(!__is_nothrow_convertible(void, Arr));
93 SA(!__is_nothrow_convertible(void, Arr&));
94
95 SA(!__is_nothrow_convertible(Fn, void));
96 SA(!__is_nothrow_convertible(Fn, Fn));
97 SA(__is_nothrow_convertible(Fn, Fn*));
98 SA(__is_nothrow_convertible(Fn, Fn&));
99 SA(!__is_nothrow_convertible(int(int), int(int)));
100 SA(__is_nothrow_convertible(int(int), int(&)(int)));
101 SA(__is_nothrow_convertible(int(int), int(&&)(int)));
102 SA(__is_nothrow_convertible(int(int), int(*)(int)));
103 SA(__is_nothrow_convertible(int(int), int(*const)(int)));
104 SA(!__is_nothrow_convertible(int(int), char));
105 SA(!__is_nothrow_convertible(int(int), char*));
106 SA(!__is_nothrow_convertible(int(int), char&));
107
108 SA(!__is_nothrow_convertible(Fn&, void));
109 SA(!__is_nothrow_convertible(Fn&, Fn));
110 SA(__is_nothrow_convertible(Fn&, Fn&));
111 SA(__is_nothrow_convertible(Fn&, Fn*));
112 SA(!__is_nothrow_convertible(Fn&, Arr));
113 SA(!__is_nothrow_convertible(Fn&, Arr&));
114 SA(!__is_nothrow_convertible(Fn&, char));
115 SA(!__is_nothrow_convertible(Fn&, char&));
116 SA(!__is_nothrow_convertible(Fn&, char*));
117
118 SA(!__is_nothrow_convertible(Fn*, void));
119 SA(!__is_nothrow_convertible(Fn*, Fn));
120 SA(!__is_nothrow_convertible(Fn*, Fn&));
121 SA(__is_nothrow_convertible(Fn*, Fn*));
122 SA(!__is_nothrow_convertible(Fn*, Arr));
123 SA(!__is_nothrow_convertible(Fn*, Arr&));
124 SA(!__is_nothrow_convertible(Fn*, char));
125 SA(!__is_nothrow_convertible(Fn*, char&));
126 SA(!__is_nothrow_convertible(Fn*, char*));
127
128 SA(!__is_nothrow_convertible(Arr, void));
129 SA(!__is_nothrow_convertible(Arr, Fn));
130 SA(!__is_nothrow_convertible(Arr, Fn*));
131 SA(!__is_nothrow_convertible(Arr, Fn&));
132 SA(!__is_nothrow_convertible(Arr, Arr));
133 SA(!__is_nothrow_convertible(Arr, Arr&));
134 SA(__is_nothrow_convertible(Arr, const Arr&));
135 SA(!__is_nothrow_convertible(Arr, volatile Arr&));
136 SA(!__is_nothrow_convertible(Arr, const volatile Arr&));
137 SA(!__is_nothrow_convertible(const Arr, Arr&));
138 SA(__is_nothrow_convertible(const Arr, const Arr&));
139 SA(__is_nothrow_convertible(Arr, Arr&&));
140 SA(__is_nothrow_convertible(Arr, const Arr&&));
141 SA(__is_nothrow_convertible(Arr, volatile Arr&&));
142 SA(__is_nothrow_convertible(Arr, const volatile Arr&&));
143 SA(__is_nothrow_convertible(const Arr, const Arr&&));
144 SA(!__is_nothrow_convertible(Arr&, Arr&&));
145 SA(!__is_nothrow_convertible(Arr&&, Arr&));
146 SA(!__is_nothrow_convertible(Arr, char));
147 SA(__is_nothrow_convertible(Arr, char*));
148 SA(__is_nothrow_convertible(Arr, const char*));
149 SA(!__is_nothrow_convertible(Arr, char&));
150 SA(!__is_nothrow_convertible(const Arr, char*));
151 SA(__is_nothrow_convertible(const Arr, const char*));
152 SA(!__is_nothrow_convertible(int, int[1]));
153 SA(!__is_nothrow_convertible(int[1], int[1]));
154 SA(!__is_nothrow_convertible(int[1], int(&)[1]));
155 SA(__is_nothrow_convertible(int(&)[1], int(&)[1]));
156 SA(__is_nothrow_convertible(int(&)[1], const int(&)[1]));
157 SA(!__is_nothrow_convertible(const int(&)[1], int(&)[1]));
158 SA(!__is_nothrow_convertible(int[1][1], int*));
159 SA(!__is_nothrow_convertible(int[][1], int*));
160
161 SA(!__is_nothrow_convertible(Arr&, void));
162 SA(!__is_nothrow_convertible(Arr&, Fn));
163 SA(!__is_nothrow_convertible(Arr&, Fn*));
164 SA(!__is_nothrow_convertible(Arr&, Fn&));
165 SA(!__is_nothrow_convertible(Arr&, Arr));
166 SA(__is_nothrow_convertible(Arr&, Arr&));
167 SA(__is_nothrow_convertible(Arr&, const Arr&));
168 SA(!__is_nothrow_convertible(const Arr&, Arr&));
169 SA(__is_nothrow_convertible(const Arr&, const Arr&));
170 SA(!__is_nothrow_convertible(Arr&, char));
171 SA(__is_nothrow_convertible(Arr&, char*));
172 SA(__is_nothrow_convertible(Arr&, const char*));
173 SA(!__is_nothrow_convertible(Arr&, char&));
174 SA(!__is_nothrow_convertible(const Arr&, char*));
175 SA(__is_nothrow_convertible(const Arr&, const char*));
176 SA(__is_nothrow_convertible(Arr, from_charp));
177 SA(__is_nothrow_convertible(Arr&, from_charp));
178
179 struct B { };
180 struct D : B { };
181
182 SA(__is_nothrow_convertible(D, B));
183 SA(__is_nothrow_convertible(D*, B*));
184 SA(__is_nothrow_convertible(D&, B&));
185 SA(!__is_nothrow_convertible(B, D));
186 SA(!__is_nothrow_convertible(B*, D*));
187 SA(!__is_nothrow_convertible(B&, D&));
188
189 /* These are taken from LLVM's test/SemaCXX/type-traits.cpp. */
190
191 struct I {
192 int i;
193 I(int _i) noexcept : i(_i) { }
194 operator int() const noexcept {
195 return i;
196 }
197 };
198
199 struct F
200 {
201 float f;
202 F(float _f) noexcept : f(_f) {}
203 F(const I& obj) noexcept
204 : f(static_cast<float>(obj.i)) {}
205 operator float() const noexcept {
206 return f;
207 }
208 operator I() const noexcept {
209 return I(static_cast<int>(f));
210 }
211 };
212
213 SA(__is_nothrow_convertible(I, I));
214 SA(__is_nothrow_convertible(I, const I));
215 SA(__is_nothrow_convertible(I, int));
216 SA(__is_nothrow_convertible(int, I));
217 SA(__is_nothrow_convertible(I, F));
218 SA(__is_nothrow_convertible(F, I));
219 SA(__is_nothrow_convertible(F, float));
220 SA(__is_nothrow_convertible(float, F));
221
222 template<typename>
223 struct X {
224 template<typename U> X(const X<U>&) noexcept;
225 };
226
227 SA(__is_nothrow_convertible(X<int>, X<float>));
228 SA(__is_nothrow_convertible(X<float>, X<int>));
229
230 struct Abstract {
231 virtual void f() = 0;
232 };
233
234 SA(!__is_nothrow_convertible(Abstract, Abstract));
235
236 class hidden {
237 hidden(const hidden&);
238 friend void test ();
239 };
240
241 SA(__is_nothrow_convertible(hidden&, hidden&));
242 SA(__is_nothrow_convertible(hidden&, const hidden&));
243 SA(__is_nothrow_convertible(hidden&, volatile hidden&));
244 SA(__is_nothrow_convertible(hidden&, const volatile hidden&));
245 SA(__is_nothrow_convertible(const hidden&, const hidden&));
246 SA(__is_nothrow_convertible(const hidden&, const volatile hidden&));
247 SA(__is_nothrow_convertible(volatile hidden&, const volatile hidden&));
248 SA(__is_nothrow_convertible(const volatile hidden&, const volatile hidden&));
249 SA(!__is_nothrow_convertible(const hidden&, hidden&));
250
251 void
252 test ()
253 {
254 /* __is_nothrow_convertible(hidden, hidden) should be false despite the
255 friend declaration above, because "Access checks are performed
256 as if from a context unrelated to either type", but we don't
257 implement that for the built-in (std::is_convertible works as
258 expected). This is the case for __is_assignable as well. */
259 //SA(!__is_nothrow_convertible(hidden, hidden));
260 }
261
262 void
263 test2 ()
264 {
265 struct X { };
266 struct Y {
267 explicit Y(X); // not viable for implicit conversions
268 };
269 SA(!__is_nothrow_convertible(X, Y));
270 }
This page took 0.051452 seconds and 5 git commands to generate.