| 1 |
// DR 339 |
| 2 |
// |
| 3 |
// Test of the use of the ternary operator with SFINAE |
| 4 |
|
| 5 |
// Boilerplate helpers |
| 6 |
typedef char yes_type; |
| 7 |
struct no_type { char data[2]; }; |
| 8 |
|
| 9 |
template<typename T> T create_a(); |
| 10 |
template<typename T> struct type { }; |
| 11 |
|
| 12 |
template<bool, typename T = void> struct enable_if { typedef T type; }; |
| 13 |
template<typename T> struct enable_if<false, T> { }; |
| 14 |
|
| 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 |
| 18 |
|
| 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 |
| 29 |
{ |
| 30 |
static const bool value = |
| 31 |
(sizeof(check_ternary<T, U, V>(0)) == sizeof(yes_type)); |
| 32 |
}; |
| 33 |
|
| 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 |
| 39 |
|
| 40 |
struct X { }; |
| 41 |
struct Y { operator bool(); }; |
| 42 |
|
| 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)); |