372c1a58378aeadc2c2ab7156230a0812d9f7c02
1 // { dg-do compile { target c++11 } }
3 // Copyright (C) 2012-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <type_traits>
21 #include <initializer_list>
26 template<typename T
, typename
= typename
T::type
>
27 static std::true_type
test(int);
29 static std::false_type
test(...);
33 struct has_type
: public decltype(has_type_impl::test
<T
>(0))
36 template<typename T
, typename Expected
>
37 struct is_expected_type
: public std::is_same
<typename
T::type
, Expected
>
40 template<typename P1
, typename P2
>
41 struct and_
: public std::conditional
<P1::value
, P2
, std::false_type
>::type
44 template<typename T
, typename Expected
>
45 struct is_type
: public and_
<has_type
<T
>, is_expected_type
<T
, Expected
>>
55 struct F1
{ operator void*(); };
56 struct F2
{ operator void*(); };
58 struct G1
{ operator const void*(); };
59 struct G2
{ operator volatile void*(); };
70 explicit operator T();
74 struct PrivateImplicitTo
80 auto lmd1
= [](int, double) {};
81 auto lmd2
= [](int, double) {};
85 virtual ~Abstract() = 0;
106 operator Abstract
*();
111 operator Abstract
*();
126 struct common_type
<X1
, X2
>
132 struct common_type
<X2
, X1
>
138 struct common_type
<RX12
, X1
>
144 struct common_type
<X1
, RX12
>
150 struct common_type
<RX21
, X1
>
156 struct common_type
<X1
, RX21
>
162 static_assert(is_type
<std::common_type
<int, int>, int>(), "");
163 static_assert(is_type
<std::common_type
<ScEn
, ScEn
>, ScEn
>(), "");
164 static_assert(is_type
<std::common_type
<UnscEn
, UnscEn
>, UnscEn
>(), "");
165 static_assert(is_type
<std::common_type
<UnscEn
, int>, int>(), "");
166 static_assert(is_type
<std::common_type
<int, int, int>, int>(), "");
167 static_assert(is_type
<std::common_type
<int, int, int, int>, int>(), "");
168 static_assert(is_type
<std::common_type
<int, int, int, int, int>, int>(), "");
169 static_assert(is_type
<std::common_type
<S
, S
>, S
>(), "");
170 static_assert(is_type
<std::common_type
<const S
, const S
>, S
>(), "");
171 static_assert(is_type
<std::common_type
<std::initializer_list
<int>,
172 std::initializer_list
<int>>, std::initializer_list
<int>>(), "");
173 static_assert(is_type
<std::common_type
<B
, D
>, B
>(), "");
174 static_assert(is_type
<std::common_type
<D
, B
>, B
>(), "");
175 static_assert(is_type
<std::common_type
<F1
, F2
>, void*>(), "");
176 static_assert(is_type
<std::common_type
<F2
, F1
>, void*>(), "");
177 static_assert(is_type
<std::common_type
<G1
, G2
>, const volatile void*>(), "");
178 static_assert(is_type
<std::common_type
<G2
, G1
>, const volatile void*>(), "");
179 static_assert(is_type
<std::common_type
<int*, const volatile int*>,
180 const volatile int*>(), "");
181 static_assert(is_type
<std::common_type
<void*, const volatile int*>,
182 const volatile void*>(), "");
183 static_assert(is_type
<std::common_type
<void, void>, void>(), "");
184 static_assert(is_type
<std::common_type
<const void, const void>, void>(), "");
185 static_assert(is_type
<std::common_type
<int&, int&&>, int>(), "");
186 static_assert(is_type
<std::common_type
<int&, int&>, int>(), "");
187 static_assert(is_type
<std::common_type
<int&&, int&&>, int>(), "");
188 static_assert(is_type
<std::common_type
<int&&, const int&&>, int>(), "");
189 static_assert(is_type
<std::common_type
<U
&, const U
&&>, U
>(), "");
190 static_assert(is_type
<std::common_type
<U
&, U
&>, U
>(), "");
191 static_assert(is_type
<std::common_type
<U
&&, U
&&>, U
>(), "");
192 static_assert(is_type
<std::common_type
<int B::*, int D::*>, int D::*>(), "");
193 static_assert(is_type
<std::common_type
<int D::*, int B::*>, int D::*>(), "");
194 static_assert(is_type
<std::common_type
<const int B::*, volatile int D::*>,
195 const volatile int D::*>(), "");
196 static_assert(is_type
<std::common_type
<int (B::*)(), int (D::*)()>,
197 int (D::*)()>(), "");
198 static_assert(is_type
<std::common_type
<int (B::*)() const, int (D::*)() const>,
199 int (D::*)() const>(), "");
200 static_assert(is_type
<std::common_type
<int[3], int[3]>, int*>(), "");
201 static_assert(is_type
<std::common_type
<int[1], const int[3]>,
203 static_assert(is_type
<std::common_type
<void(), void()>, void(*)()>(), "");
204 static_assert(is_type
<std::common_type
<void(&)(), void(&)()>, void(*)()>(), "");
205 static_assert(is_type
<std::common_type
<void(&)(), void(&&)()>,
207 static_assert(is_type
<std::common_type
<void(&&)(), void(&)()>,
209 static_assert(is_type
<std::common_type
<void(&&)(), void(&&)()>,
211 static_assert(is_type
<std::common_type
<ImplicitTo
<int>, int>, int>(), "");
212 static_assert(is_type
<std::common_type
<ImplicitTo
<int>, ImplicitTo
<int>>,
213 ImplicitTo
<int>>(), "");
214 static_assert(is_type
<std::common_type
<ImplicitTo
<int>, int,
215 ImplicitTo
<int>>, int>(), "");
216 static_assert(is_type
<std::common_type
<ExplicitTo
<int>, ExplicitTo
<int>>,
217 ExplicitTo
<int>>(), "");
218 static_assert(is_type
<std::common_type
<decltype(lmd1
), decltype(lmd1
)>,
219 decltype(lmd1
)>(), "");
220 static_assert(is_type
<std::common_type
<decltype(lmd1
)&, decltype(lmd1
)&>,
221 decltype(lmd1
)>(), "");
222 static_assert(is_type
<std::common_type
<decltype(lmd1
)&, decltype(lmd2
)&>,
223 void(*)(int, double)>(), "");
224 static_assert(is_type
<std::common_type
<decltype(nullptr), void*>, void*>(), "");
225 static_assert(is_type
<std::common_type
<decltype(nullptr), int*>, int*>(), "");
226 static_assert(is_type
<std::common_type
<const decltype(nullptr)&, int*>,
228 static_assert(is_type
<std::common_type
<decltype(nullptr), const volatile int*>,
229 const volatile int*>(), "");
230 static_assert(is_type
<std::common_type
<decltype(nullptr), int (B::*)()>,
231 int (B::*)()>(), "");
232 static_assert(is_type
<std::common_type
<decltype(nullptr), int (B::*)() const>,
233 int (B::*)() const>(), "");
234 static_assert(is_type
<std::common_type
<decltype(nullptr), const int B::*>,
235 const int B::*>(), "");
236 static_assert(is_type
<std::common_type
<Abstract
&, Abstract
&>, Abstract
>(), "");
237 static_assert(is_type
<std::common_type
<Ukn
&, Ukn
&>, Ukn
>(), "");
238 static_assert(is_type
<std::common_type
<ImplicitTo
<B
&>, B
&>, B
>(), "");
239 static_assert(is_type
<std::common_type
<ImplicitTo
<B
&>&, B
&&>, B
>(), "");
240 static_assert(is_type
<std::common_type
<UConv1
, const Abstract
*&>,
241 const Abstract
*>(), "");
242 static_assert(is_type
<std::common_type
<UConv1
, UConv2
>, Abstract
*>(), "");
243 static_assert(is_type
<std::common_type
<UConv1
&, UConv2
&>, Abstract
*>(), "");
245 static_assert(is_type
<std::common_type
<Abstract
&&, Abstract
&&>,
247 static_assert(is_type
<std::common_type
<const Abstract
&&,
248 const Abstract
&&>, Abstract
>(), "");
249 static_assert(is_type
<std::common_type
<volatile Abstract
&&,
250 volatile Abstract
&&>, Abstract
>(), "");
251 static_assert(is_type
<std::common_type
<Ukn
&&, Ukn
&&>, Ukn
>(), "");
252 static_assert(is_type
<std::common_type
<const Ukn
&&, const Ukn
&&>,
254 static_assert(is_type
<std::common_type
<volatile Ukn
&&, volatile Ukn
&&>,
257 static_assert(is_type
<std::common_type
<X1
, X2
>, RX12
>(), "");
258 static_assert(is_type
<std::common_type
<X2
, X1
>, RX21
>(), "");
260 static_assert(is_type
<std::common_type
<X1
, X2
, X1
>, Y1
>(), "");
261 static_assert(is_type
<std::common_type
<X2
, X1
, X1
>, Y3
>(), "");
263 static_assert(is_type
<std::common_type
<X1
, X1
, X2
>, RX12
>(), "");
264 static_assert(is_type
<std::common_type
<X1
, X1
, X2
, X1
>, Y1
>(), "");
266 static_assert(!has_type
<std::common_type
<>>(), "");
267 static_assert(!has_type
<std::common_type
<int, S
>>(), "");
268 static_assert(!has_type
<std::common_type
<U
, S
>>(), "");
269 static_assert(!has_type
<std::common_type
<U
, U2
>>(), "");
270 static_assert(!has_type
<std::common_type
<const ImplicitTo
<int>, int>>(), "");
271 static_assert(!has_type
<std::common_type
<PrivateImplicitTo
<int>, int>>(), "");
272 static_assert(!has_type
<std::common_type
<const PrivateImplicitTo
<int>,
274 static_assert(!has_type
<std::common_type
<int, Ukn
>>(), "");
275 static_assert(!has_type
<std::common_type
<int, Abstract
>>(), "");
276 static_assert(!has_type
<std::common_type
<Ukn
, Abstract
>>(), "");
277 static_assert(!has_type
<std::common_type
<int, void>>(), "");
278 static_assert(!has_type
<std::common_type
<int, const volatile void>>(), "");
279 static_assert(!has_type
<std::common_type
<Abstract
, void>>(), "");
280 static_assert(!has_type
<std::common_type
<Ukn
, void>>(), "");
281 static_assert(!has_type
<std::common_type
<int[4], void>>(), "");
282 static_assert(!has_type
<std::common_type
<ScEn
, void>>(), "");
283 static_assert(!has_type
<std::common_type
<UnscEn
, void>>(), "");
284 static_assert(!has_type
<std::common_type
<U
, void>>(), "");
285 static_assert(!has_type
<std::common_type
<std::initializer_list
<int>,
287 static_assert(!has_type
<std::common_type
<int, int, int, S
>>(), "");
288 static_assert(!has_type
<std::common_type
<int, int, S
, int>>(), "");
289 static_assert(!has_type
<std::common_type
<int, S
, int, int>>(), "");
290 static_assert(!has_type
<std::common_type
<S
, int, int, int>>(), "");
291 static_assert(!has_type
<std::common_type
<int, int, void, int, int>>(), "");
292 static_assert(!has_type
<std::common_type
<B
, S
>>(), "");
293 static_assert(!has_type
<std::common_type
<int, B
, S
>>(), "");
294 static_assert(!has_type
<std::common_type
<B
, int, S
>>(), "");
295 static_assert(!has_type
<std::common_type
<B
, S
, int>>(), "");
296 static_assert(!has_type
<std::common_type
<int*, double*>>(), "");
297 static_assert(!has_type
<std::common_type
<void*, void(*)(...)>>(), "");
298 static_assert(!has_type
<std::common_type
<void(*)(), void(*)(...)>>(), "");
299 static_assert(!has_type
<std::common_type
<void(*)(), void(S::*)()>>(), "");
300 static_assert(!has_type
<std::common_type
<void(S::*)() const,
301 void(S::*)()>>(), "");
302 static_assert(!has_type
<std::common_type
<int S::*, long S::*>>(), "");
303 static_assert(!has_type
<std::common_type
<int S::*, void(S::*)()>>(), "");
304 static_assert(!has_type
<std::common_type
<int (B::*)(),
305 int (D::*)() const>>(), "");
306 static_assert(!has_type
<std::common_type
<int (B::*)() const,
307 int (D::*)()>>(), "");
308 static_assert(!has_type
<std::common_type
<int, ExplicitTo
<int>>>(), "");
309 static_assert(!has_type
<std::common_type
<ImplicitTo
<int>,
310 ExplicitTo
<int>>>(), "");
311 static_assert(!has_type
<std::common_type
<ScEn
, int>>(), "");
312 static_assert(!has_type
<std::common_type
<ScEn
, UnscEn
>>(), "");
313 static_assert(!has_type
<std::common_type
<U
, S
, Abstract
, void, D
,
314 int (B::*)(), int[5]>>(), "");
315 static_assert(!has_type
<std::common_type
<UConv1
, Abstract
&&>>(), "");
316 static_assert(!has_type
<std::common_type
<std::initializer_list
<int>,
317 std::initializer_list
<long>>>(), "");
321 auto local_lmd1
= [=](int, double) { return i
+ i
; };
322 auto local_lmd2
= [=](int, double) { return i
- i
; };
324 static_assert(is_type
<std::common_type
<decltype(local_lmd1
),
325 decltype(local_lmd1
)>, decltype(local_lmd1
)>(), "");
326 static_assert(is_type
<std::common_type
<decltype(local_lmd1
)&,
327 decltype(local_lmd1
)>, decltype(local_lmd1
)>(), "");
328 static_assert(is_type
<std::common_type
<decltype(local_lmd1
)&,
329 decltype(local_lmd1
)&>, decltype(local_lmd1
)>(), "");
331 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
),
332 decltype(lmd1
)>>(), "");
333 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
)&,
334 decltype(lmd1
)&>>(), "");
335 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
),
336 decltype(local_lmd2
)>>(), "");
337 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
)&,
338 decltype(local_lmd2
)&>>(), "");