3 // Test of the use of various unary operators 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 DEFINE_PREFIX_UNARY_TRAIT(Name,Op) \
20 template<typename T> \
21 typename enable_if<(sizeof(Op create_a<T>(), 1) > 0), \
23 JOIN(check_,Name)(int); \
25 template<typename T> \
26 no_type JOIN(check_,Name)(...); \
28 template<typename T> \
31 static const bool value = \
32 (sizeof(JOIN(check_,Name)<T&>(0)) == sizeof(yes_type)); \
35 #define DEFINE_POSTFIX_UNARY_TRAIT(Name,Op) \
36 template<typename T> \
37 typename enable_if<(sizeof(create_a<T>() Op, 1) > 0), \
39 JOIN(check_,Name)(int); \
41 template<typename T> \
42 no_type JOIN(check_,Name)(...); \
44 template<typename T> \
47 static const bool value = \
48 (sizeof(JOIN(check_,Name)<T&>(0)) == sizeof(yes_type)); \
51 #ifdef __GXX_EXPERIMENTAL_CXX0X__
52 # define STATIC_ASSERT(Expr) static_assert(Expr, #Expr)
54 # define STATIC_ASSERT(Expr) int JOIN(a,__LINE__)[Expr? 1 : -1]
77 X& operator++(X&, int);
78 X& operator--(X&, int);
84 Z operator+(); // { dg-error "is private" }
85 Z operator-(); // { dg-error "is private" }
86 int operator*(); // { dg-error "is private" }
87 Z operator~(); // { dg-error "is private" }
88 bool operator!(); // { dg-error "is private" }
89 Z& operator++(); // { dg-error "is private" }
90 Z& operator--(); // { dg-error "is private" }
91 Z& operator++(int); // { dg-error "is private" }
92 Z& operator--(int); // { dg-error "is private" }
96 DEFINE_PREFIX_UNARY_TRAIT(has_unary_plus, +); // { dg-error "within this context" }
97 STATIC_ASSERT((has_unary_plus<int>::value));
98 STATIC_ASSERT((!has_unary_plus<int X::*>::value));
99 STATIC_ASSERT((has_unary_plus<W>::value));
100 STATIC_ASSERT((has_unary_plus<X>::value));
101 STATIC_ASSERT((!has_unary_plus<Y>::value));
104 DEFINE_PREFIX_UNARY_TRAIT(is_negatable, -); // { dg-error "within this context" }
105 STATIC_ASSERT((is_negatable<int>::value));
106 STATIC_ASSERT((!is_negatable<int X::*>::value));
107 STATIC_ASSERT((is_negatable<W>::value));
108 STATIC_ASSERT((is_negatable<X>::value));
109 STATIC_ASSERT((!is_negatable<Y>::value));
111 // is_dereferenceable
112 DEFINE_PREFIX_UNARY_TRAIT(is_dereferenceable, *); // { dg-error "within this context" }
113 STATIC_ASSERT((!is_dereferenceable<int>::value));
114 STATIC_ASSERT((is_dereferenceable<int*>::value));
115 STATIC_ASSERT((is_dereferenceable<W>::value));
116 STATIC_ASSERT((is_dereferenceable<X>::value));
117 STATIC_ASSERT((!is_dereferenceable<Y>::value));
120 DEFINE_PREFIX_UNARY_TRAIT(has_bitwise_not, ~); // { dg-error "within this context" }
121 STATIC_ASSERT((has_bitwise_not<int>::value));
122 STATIC_ASSERT((!has_bitwise_not<int*>::value));
123 STATIC_ASSERT((has_bitwise_not<W>::value));
124 STATIC_ASSERT((has_bitwise_not<X>::value));
125 STATIC_ASSERT((!has_bitwise_not<Y>::value));
128 DEFINE_PREFIX_UNARY_TRAIT(has_truth_not, !); // { dg-error "within this context" }
129 STATIC_ASSERT((has_truth_not<int>::value));
130 STATIC_ASSERT((has_truth_not<int*>::value));
131 STATIC_ASSERT((has_truth_not<W>::value));
132 STATIC_ASSERT((has_truth_not<X>::value));
133 STATIC_ASSERT((!has_truth_not<Y>::value));
136 DEFINE_PREFIX_UNARY_TRAIT(has_preincrement, ++); // { dg-error "within this context" }
137 STATIC_ASSERT((has_preincrement<int>::value));
138 STATIC_ASSERT((has_preincrement<int*>::value));
139 STATIC_ASSERT((!has_preincrement<int X::*>::value));
140 STATIC_ASSERT((has_preincrement<W>::value));
141 STATIC_ASSERT((has_preincrement<X>::value));
142 STATIC_ASSERT((!has_preincrement<Y>::value));
145 DEFINE_PREFIX_UNARY_TRAIT(has_predecrement, --); // { dg-error "within this context" }
146 STATIC_ASSERT((has_predecrement<int>::value));
147 STATIC_ASSERT((has_predecrement<int*>::value));
148 STATIC_ASSERT((!has_predecrement<int X::*>::value));
149 STATIC_ASSERT((has_predecrement<W>::value));
150 STATIC_ASSERT((has_predecrement<X>::value));
151 STATIC_ASSERT((!has_predecrement<Y>::value));
154 DEFINE_POSTFIX_UNARY_TRAIT(has_postincrement, ++); // { dg-error "within this context" }
155 STATIC_ASSERT((has_postincrement<int>::value));
156 STATIC_ASSERT((has_postincrement<int*>::value));
157 STATIC_ASSERT((!has_postincrement<int X::*>::value));
158 STATIC_ASSERT((has_postincrement<W>::value));
159 STATIC_ASSERT((has_postincrement<X>::value));
160 STATIC_ASSERT((!has_postincrement<Y>::value));
163 DEFINE_POSTFIX_UNARY_TRAIT(has_postdecrement, --); // { dg-error "within this context" }
164 STATIC_ASSERT((has_postdecrement<int>::value));
165 STATIC_ASSERT((has_postdecrement<int*>::value));
166 STATIC_ASSERT((!has_postdecrement<int X::*>::value));
167 STATIC_ASSERT((has_postdecrement<W>::value));
168 STATIC_ASSERT((has_postdecrement<X>::value));
169 STATIC_ASSERT((!has_postdecrement<Y>::value));
171 // Check for private members
172 STATIC_ASSERT((has_unary_plus<Z>::value)); // { dg-message "instantiated from here" }
173 STATIC_ASSERT((is_negatable<Z>::value)); // { dg-message "instantiated from here" }
174 STATIC_ASSERT((is_dereferenceable<Z>::value)); // { dg-message "instantiated from here" }
175 STATIC_ASSERT((has_bitwise_not<Z>::value)); // { dg-message "instantiated from here" }
176 STATIC_ASSERT((has_truth_not<Z>::value)); // { dg-message "instantiated from here" }
177 STATIC_ASSERT((has_preincrement<Z>::value)); // { dg-message "instantiated from here" }
178 STATIC_ASSERT((has_predecrement<Z>::value)); // { dg-message "instantiated from here" }
179 STATIC_ASSERT((has_postincrement<Z>::value)); // { dg-message "instantiated from here" }
180 STATIC_ASSERT((has_postdecrement<Z>::value)); // { dg-message "instantiated from here" }