3 // Test of the use of casts with SFINAE
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 #define CHECK_CAST(CastKind) \
20 template<typename T, typename U> \
21 typename enable_if<(sizeof((JOIN(CastKind,_cast)<U>(create_a<T>())), 0) > 0), \
23 JOIN(check_,JOIN(CastKind,_cast))(int); \
25 template<typename T, typename U> \
26 no_type JOIN(check_,JOIN(CastKind,_cast))(...); \
28 template<typename T, typename U> \
29 struct JOIN(has_,JOIN(CastKind,_cast)) \
31 static const bool value = \
32 (sizeof(JOIN(check_,JOIN(CastKind,_cast))<T, U>(0)) == sizeof(yes_type)); \
35 template<typename T, typename U>
36 typename enable_if<(sizeof(((U)create_a<T>()), 0) > 0), yes_type>::type
39 template<typename T, typename U> no_type check_c_cast(...);
41 template<typename T, typename U>
44 static const bool value =
45 (sizeof(check_c_cast<T, U>(0)) == sizeof(yes_type));
48 #ifdef __GXX_EXPERIMENTAL_CXX0X__
49 # define STATIC_ASSERT(Expr) static_assert(Expr, #Expr)
51 # define STATIC_ASSERT(Expr) int JOIN(a,__LINE__)[Expr? 1 : -1]
57 CHECK_CAST(reinterpret);
59 struct X { virtual void f(); };
60 struct Y { operator bool(); };
61 struct Z : public X { };
63 STATIC_ASSERT((has_static_cast<int, float>::value));
64 STATIC_ASSERT((!has_static_cast<X, Y>::value));
65 STATIC_ASSERT((has_static_cast<Z, X>::value));
67 STATIC_ASSERT(!(has_dynamic_cast<int, float>::value));
68 STATIC_ASSERT(!(has_dynamic_cast<X, Y>::value));
69 STATIC_ASSERT(!(has_dynamic_cast<X, Z>::value));
70 STATIC_ASSERT(!(has_dynamic_cast<Y, Z>::value));
71 STATIC_ASSERT((has_dynamic_cast<X*, Z*>::value));
72 STATIC_ASSERT((has_dynamic_cast<X*, Y*>::value));
73 STATIC_ASSERT(!(has_dynamic_cast<Y*, Z*>::value));
75 STATIC_ASSERT(!(has_const_cast<int, float>::value));
76 STATIC_ASSERT((has_const_cast<const int*, int*>::value));
77 STATIC_ASSERT((has_const_cast<int*, const int*>::value));
78 STATIC_ASSERT(!(has_const_cast<const int*, float*>::value));
80 STATIC_ASSERT((has_reinterpret_cast<int*, float*>::value));
81 STATIC_ASSERT(!(has_reinterpret_cast<void*, char>::value));
82 STATIC_ASSERT(!(has_reinterpret_cast<const X, X>::value));
84 STATIC_ASSERT((has_c_cast<int, float>::value));
85 STATIC_ASSERT(!(has_c_cast<X, Y>::value));
86 STATIC_ASSERT(!(has_c_cast<void*, char>::value));