Update concepts branch to revision 131834
[official-gcc.git] / gcc / testsuite / g++.dg / template / sfinae12.C
blobc51211be2b9ddd0baa3c14265cb196dff4b52886
1 // DR 339
2 //
3 // Test of the use of the ternary operator with SFINAE
5 // Boilerplate helpers
6 typedef char yes_type;
7 struct no_type { char data[2]; };
9 template<typename T> T create_a();
10 template<typename T> struct type { };
12 template<bool, typename T = void> struct enable_if { typedef T type; };
13 template<typename T> struct enable_if<false, T> { };
15 #define JOIN( X, Y ) DO_JOIN( X, Y )
16 #define DO_JOIN( X, Y ) DO_JOIN2(X,Y)
17 #define DO_JOIN2( X, Y ) X##Y
19 template<typename T, typename U, typename V>
20 typename enable_if<
21            (sizeof((create_a<T>()? create_a<U>() : create_a<V>()), 0) > 0),
22            yes_type>::type
23   check_ternary(int);
24                                                             
25 template<typename T, typename U, typename V> no_type check_ternary(...);
26                                                             
27 template<typename T, typename U, typename V>
28 struct has_ternary
30   static const bool value =
31     (sizeof(check_ternary<T, U, V>(0)) == sizeof(yes_type));
34 #ifdef __GXX_EXPERIMENTAL_CXX0X__
35 #  define STATIC_ASSERT(Expr) static_assert(Expr, #Expr)
36 #else
37 #  define STATIC_ASSERT(Expr) int JOIN(a,__LINE__)[Expr? 1 : -1]
38 #endif
40 struct X { };
41 struct Y { operator bool(); };
43 STATIC_ASSERT((has_ternary<int, float, double>::value));
44 STATIC_ASSERT((has_ternary<bool, double, double>::value));
45 STATIC_ASSERT((!has_ternary<int, float*, double>::value));
46 STATIC_ASSERT((!has_ternary<X, double, double>::value));
47 STATIC_ASSERT((has_ternary<Y, double, double>::value));