]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/common_type/requirements/sfinae_friendly_1.cc
1 // { dg-do compile { target c++11 } }
3 // Copyright (C) 2012-2024 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>(), "");
163 static_assert(is_type
<std::common_type
<const int>, int>(), "");
164 static_assert(is_type
<std::common_type
<int, int>, int>(), "");
165 static_assert(is_type
<std::common_type
<const int, int>, int>(), "");
166 static_assert(is_type
<std::common_type
<ScEn
, ScEn
>, ScEn
>(), "");
167 static_assert(is_type
<std::common_type
<UnscEn
, UnscEn
>, UnscEn
>(), "");
168 static_assert(is_type
<std::common_type
<UnscEn
, int>, int>(), "");
169 static_assert(is_type
<std::common_type
<int, int, int>, int>(), "");
170 static_assert(is_type
<std::common_type
<int, int, int, int>, int>(), "");
171 static_assert(is_type
<std::common_type
<int, int, int, int, int>, int>(), "");
172 static_assert(is_type
<std::common_type
<S
, S
>, S
>(), "");
173 static_assert(is_type
<std::common_type
<const S
, const S
>, S
>(), "");
174 static_assert(is_type
<std::common_type
<std::initializer_list
<int>,
175 std::initializer_list
<int>>, std::initializer_list
<int>>(), "");
176 static_assert(is_type
<std::common_type
<B
, D
>, B
>(), "");
177 static_assert(is_type
<std::common_type
<D
, B
>, B
>(), "");
178 static_assert(is_type
<std::common_type
<F1
, F2
>, void*>(), "");
179 static_assert(is_type
<std::common_type
<F2
, F1
>, void*>(), "");
180 static_assert(is_type
<std::common_type
<G1
, G2
>, const volatile void*>(), "");
181 static_assert(is_type
<std::common_type
<G2
, G1
>, const volatile void*>(), "");
182 static_assert(is_type
<std::common_type
<int*, const volatile int*>,
183 const volatile int*>(), "");
184 static_assert(is_type
<std::common_type
<void*, const volatile int*>,
185 const volatile void*>(), "");
186 static_assert(is_type
<std::common_type
<void>, void>(), "");
187 static_assert(is_type
<std::common_type
<const void>, void>(), "");
188 static_assert(is_type
<std::common_type
<void, void>, void>(), "");
189 static_assert(is_type
<std::common_type
<const void, const void>, void>(), "");
190 static_assert(is_type
<std::common_type
<int&, int&&>, int>(), "");
191 static_assert(is_type
<std::common_type
<int&, int&>, int>(), "");
192 static_assert(is_type
<std::common_type
<int&&, int&&>, int>(), "");
193 static_assert(is_type
<std::common_type
<int&&, const int&&>, int>(), "");
194 static_assert(is_type
<std::common_type
<U
&, const U
&&>, U
>(), "");
195 static_assert(is_type
<std::common_type
<U
&, U
&>, U
>(), "");
196 static_assert(is_type
<std::common_type
<U
&&, U
&&>, U
>(), "");
197 static_assert(is_type
<std::common_type
<int B::*, int D::*>, int D::*>(), "");
198 static_assert(is_type
<std::common_type
<int D::*, int B::*>, int D::*>(), "");
199 static_assert(is_type
<std::common_type
<const int B::*, volatile int D::*>,
200 const volatile int D::*>(), "");
201 static_assert(is_type
<std::common_type
<int (B::*)(), int (D::*)()>,
202 int (D::*)()>(), "");
203 static_assert(is_type
<std::common_type
<int (B::*)() const, int (D::*)() const>,
204 int (D::*)() const>(), "");
205 static_assert(is_type
<std::common_type
<int[3], int[3]>, int*>(), "");
206 static_assert(is_type
<std::common_type
<int[1], const int[3]>,
208 static_assert(is_type
<std::common_type
<void(), void()>, void(*)()>(), "");
209 static_assert(is_type
<std::common_type
<void(&)(), void(&)()>, void(*)()>(), "");
210 static_assert(is_type
<std::common_type
<void(&)(), void(&&)()>,
212 static_assert(is_type
<std::common_type
<void(&&)(), void(&)()>,
214 static_assert(is_type
<std::common_type
<void(&&)(), void(&&)()>,
216 static_assert(is_type
<std::common_type
<ImplicitTo
<int>, int>, int>(), "");
217 static_assert(is_type
<std::common_type
<const ImplicitTo
<int>, int>, int>(), "");
218 static_assert(is_type
<std::common_type
<ImplicitTo
<int>, ImplicitTo
<int>>,
219 ImplicitTo
<int>>(), "");
220 static_assert(is_type
<std::common_type
<ImplicitTo
<int>, int,
221 ImplicitTo
<int>>, int>(), "");
222 static_assert(is_type
<std::common_type
<ExplicitTo
<int>, ExplicitTo
<int>>,
223 ExplicitTo
<int>>(), "");
224 static_assert(is_type
<std::common_type
<decltype(lmd1
), decltype(lmd1
)>,
225 decltype(lmd1
)>(), "");
226 static_assert(is_type
<std::common_type
<decltype(lmd1
)&, decltype(lmd1
)&>,
227 decltype(lmd1
)>(), "");
228 static_assert(is_type
<std::common_type
<decltype(lmd1
)&, decltype(lmd2
)&>,
229 void(*)(int, double)>(), "");
230 static_assert(is_type
<std::common_type
<decltype(nullptr), void*>, void*>(), "");
231 static_assert(is_type
<std::common_type
<decltype(nullptr), int*>, int*>(), "");
232 static_assert(is_type
<std::common_type
<const decltype(nullptr)&, int*>,
234 static_assert(is_type
<std::common_type
<decltype(nullptr), const volatile int*>,
235 const volatile int*>(), "");
236 static_assert(is_type
<std::common_type
<decltype(nullptr), int (B::*)()>,
237 int (B::*)()>(), "");
238 static_assert(is_type
<std::common_type
<decltype(nullptr), int (B::*)() const>,
239 int (B::*)() const>(), "");
240 static_assert(is_type
<std::common_type
<decltype(nullptr), const int B::*>,
241 const int B::*>(), "");
242 static_assert(is_type
<std::common_type
<Abstract
&, Abstract
&>, Abstract
>(), "");
243 static_assert(is_type
<std::common_type
<Ukn
&, Ukn
&>, Ukn
>(), "");
244 static_assert(is_type
<std::common_type
<ImplicitTo
<B
&>, B
&>, B
>(), "");
245 static_assert(is_type
<std::common_type
<ImplicitTo
<B
&>&, B
&&>, B
>(), "");
246 static_assert(is_type
<std::common_type
<UConv1
, const Abstract
*&>,
247 const Abstract
*>(), "");
248 static_assert(is_type
<std::common_type
<UConv1
, UConv2
>, Abstract
*>(), "");
249 static_assert(is_type
<std::common_type
<UConv1
&, UConv2
&>, Abstract
*>(), "");
251 static_assert(is_type
<std::common_type
<Abstract
&&, Abstract
&&>,
253 static_assert(is_type
<std::common_type
<const Abstract
&&,
254 const Abstract
&&>, Abstract
>(), "");
255 static_assert(is_type
<std::common_type
<volatile Abstract
&&,
256 volatile Abstract
&&>, Abstract
>(), "");
257 static_assert(is_type
<std::common_type
<Ukn
&&, Ukn
&&>, Ukn
>(), "");
258 static_assert(is_type
<std::common_type
<const Ukn
&&, const Ukn
&&>,
260 static_assert(is_type
<std::common_type
<volatile Ukn
&&, volatile Ukn
&&>,
263 static_assert(is_type
<std::common_type
<X1
, X2
>, RX12
>(), "");
264 static_assert(is_type
<std::common_type
<const X1
, X2
>, RX12
>(), "");
265 static_assert(is_type
<std::common_type
<X1
&, const X2
>, RX12
>(), "");
266 static_assert(is_type
<std::common_type
<const X1
&, const X2
&>, RX12
>(), "");
267 static_assert(is_type
<std::common_type
<X2
, X1
>, RX21
>(), "");
269 static_assert(is_type
<std::common_type
<X1
, X2
, X1
>, Y1
>(), "");
270 static_assert(is_type
<std::common_type
<X2
, X1
, X1
>, Y3
>(), "");
272 static_assert(is_type
<std::common_type
<X1
, X1
, X2
>, RX12
>(), "");
273 static_assert(is_type
<std::common_type
<X1
&, const X1
, const X2
&&>, RX12
>(), "");
274 static_assert(is_type
<std::common_type
<X1
, X1
, X2
, X1
>, Y1
>(), "");
276 static_assert(!has_type
<std::common_type
<>>(), "");
277 static_assert(!has_type
<std::common_type
<int, S
>>(), "");
278 static_assert(!has_type
<std::common_type
<U
, S
>>(), "");
279 static_assert(!has_type
<std::common_type
<U
, U2
>>(), "");
280 static_assert(!has_type
<std::common_type
<PrivateImplicitTo
<int>, int>>(), "");
281 static_assert(!has_type
<std::common_type
<const PrivateImplicitTo
<int>,
283 static_assert(!has_type
<std::common_type
<int, Ukn
>>(), "");
284 static_assert(!has_type
<std::common_type
<int, Abstract
>>(), "");
285 static_assert(!has_type
<std::common_type
<Ukn
, Abstract
>>(), "");
286 static_assert(!has_type
<std::common_type
<int, void>>(), "");
287 static_assert(!has_type
<std::common_type
<int, const volatile void>>(), "");
288 static_assert(!has_type
<std::common_type
<Abstract
, void>>(), "");
289 static_assert(!has_type
<std::common_type
<Ukn
, void>>(), "");
290 static_assert(!has_type
<std::common_type
<int[4], void>>(), "");
291 static_assert(!has_type
<std::common_type
<ScEn
, void>>(), "");
292 static_assert(!has_type
<std::common_type
<UnscEn
, void>>(), "");
293 static_assert(!has_type
<std::common_type
<U
, void>>(), "");
294 static_assert(!has_type
<std::common_type
<std::initializer_list
<int>,
296 static_assert(!has_type
<std::common_type
<int, int, int, S
>>(), "");
297 static_assert(!has_type
<std::common_type
<int, int, S
, int>>(), "");
298 static_assert(!has_type
<std::common_type
<int, S
, int, int>>(), "");
299 static_assert(!has_type
<std::common_type
<S
, int, int, int>>(), "");
300 static_assert(!has_type
<std::common_type
<int, int, void, int, int>>(), "");
301 static_assert(!has_type
<std::common_type
<B
, S
>>(), "");
302 static_assert(!has_type
<std::common_type
<int, B
, S
>>(), "");
303 static_assert(!has_type
<std::common_type
<B
, int, S
>>(), "");
304 static_assert(!has_type
<std::common_type
<B
, S
, int>>(), "");
305 static_assert(!has_type
<std::common_type
<int*, double*>>(), "");
306 static_assert(!has_type
<std::common_type
<void*, void(*)(...)>>(), "");
307 static_assert(!has_type
<std::common_type
<void(*)(), void(*)(...)>>(), "");
308 static_assert(!has_type
<std::common_type
<void(*)(), void(S::*)()>>(), "");
309 static_assert(!has_type
<std::common_type
<void(S::*)() const,
310 void(S::*)()>>(), "");
311 static_assert(!has_type
<std::common_type
<int S::*, long S::*>>(), "");
312 static_assert(!has_type
<std::common_type
<int S::*, void(S::*)()>>(), "");
313 static_assert(!has_type
<std::common_type
<int (B::*)(),
314 int (D::*)() const>>(), "");
315 static_assert(!has_type
<std::common_type
<int (B::*)() const,
316 int (D::*)()>>(), "");
317 static_assert(!has_type
<std::common_type
<int, ExplicitTo
<int>>>(), "");
318 static_assert(!has_type
<std::common_type
<ImplicitTo
<int>,
319 ExplicitTo
<int>>>(), "");
320 static_assert(!has_type
<std::common_type
<ScEn
, int>>(), "");
321 static_assert(!has_type
<std::common_type
<ScEn
, UnscEn
>>(), "");
322 static_assert(!has_type
<std::common_type
<U
, S
, Abstract
, void, D
,
323 int (B::*)(), int[5]>>(), "");
324 static_assert(!has_type
<std::common_type
<UConv1
, Abstract
&&>>(), "");
325 static_assert(!has_type
<std::common_type
<std::initializer_list
<int>,
326 std::initializer_list
<long>>>(), "");
328 // PR libstdc++/89102
329 static_assert(!has_type
<std::common_type
<int() &>>(), "");
330 static_assert(!has_type
<std::common_type
<int() & noexcept
>>(), "");
331 static_assert(!has_type
<std::common_type
<int() const>>(), "");
332 static_assert(!has_type
<std::common_type
<int(...) &>>(), "");
333 static_assert(!has_type
<std::common_type
<int(...) & noexcept
>>(), "");
334 static_assert(!has_type
<std::common_type
<int(...) const>>(), "");
338 auto local_lmd1
= [=](int, double) { return i
+ i
; };
339 auto local_lmd2
= [=](int, double) { return i
- i
; };
341 static_assert(is_type
<std::common_type
<decltype(local_lmd1
),
342 decltype(local_lmd1
)>, decltype(local_lmd1
)>(), "");
343 static_assert(is_type
<std::common_type
<decltype(local_lmd1
)&,
344 decltype(local_lmd1
)>, decltype(local_lmd1
)>(), "");
345 static_assert(is_type
<std::common_type
<decltype(local_lmd1
)&,
346 decltype(local_lmd1
)&>, decltype(local_lmd1
)>(), "");
348 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
),
349 decltype(lmd1
)>>(), "");
350 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
)&,
351 decltype(lmd1
)&>>(), "");
352 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
),
353 decltype(local_lmd2
)>>(), "");
354 static_assert(!has_type
<std::common_type
<decltype(local_lmd1
)&,
355 decltype(local_lmd2
)&>>(), "");