1// DR 339 2// 3// Test of the use of the ternary operator with SFINAE 4 5// Boilerplate helpers 6typedef char yes_type; 7struct no_type { char data[2]; }; 8 9template<typename T> T create_a(); 10template<typename T> struct type { }; 11 12template<bool, typename T = void> struct enable_if { typedef T type; }; 13template<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 19template<typename T, typename U, typename V> 20typename enable_if< 21 (sizeof((create_a<T>()? create_a<U>() : create_a<V>()), 0) > 0), 22 yes_type>::type 23 check_ternary(int); 24 25template<typename T, typename U, typename V> no_type check_ternary(...); 26 27template<typename T, typename U, typename V> 28struct 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 40struct X { }; 41struct Y { operator bool(); }; 42 43STATIC_ASSERT((has_ternary<int, float, double>::value)); 44STATIC_ASSERT((has_ternary<bool, double, double>::value)); 45STATIC_ASSERT((!has_ternary<int, float*, double>::value)); 46STATIC_ASSERT((!has_ternary<X, double, double>::value)); 47STATIC_ASSERT((has_ternary<Y, double, double>::value)); 48