Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / testsuite / g++.dg / template / sfinae10.C
blobf44c4458f35fc100e3a863714c596865a801db2c
1 // DR 339
2 //
3 // Test of the use of various unary operators 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 #define DEFINE_PREFIX_UNARY_TRAIT(Name,Op)                      \
20 template<typename T>                                            \
21   typename enable_if<(sizeof(Op create_a<T>(), 1) > 0),         \
22                      yes_type>::type                            \
23   JOIN(check_,Name)(int);                                       \
24                                                                 \
25 template<typename T>                                            \
26   no_type JOIN(check_,Name)(...);                               \
27                                                                 \
28 template<typename T>                                            \
29 struct Name                                                     \
30 {                                                               \
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),         \
38                      yes_type>::type                            \
39   JOIN(check_,Name)(int);                                       \
40                                                                 \
41 template<typename T>                                            \
42   no_type JOIN(check_,Name)(...);                               \
43                                                                 \
44 template<typename T>                                            \
45 struct Name                                                     \
46 {                                                               \
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)
53 #else
54 #  define STATIC_ASSERT(Expr) int JOIN(a,__LINE__)[Expr? 1 : -1]
55 #endif
57 struct W {
58   W operator+();
59   W operator-();
60   int operator*();
61   W operator~();
62   bool operator!();
63   W& operator++();
64   W& operator--();
65   W& operator++(int);
66   W& operator--(int);
69 struct X { };
70 X operator+(X);
71 X operator-(X);
72 int operator*(X);
73 X operator~(X);
74 bool operator!(X);
75 X& operator++(X&);
76 X& operator--(X&);
77 X& operator++(X&, int);
78 X& operator--(X&, int);
80 struct Y { };
82 struct Z {
83 private:
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" }  
95 // has_unary_plus
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));
103 // is_negatable
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));
119 // has_bitwise_not
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));
127 // has_truth_not
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));
135 // has_preincrement
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));
144 // has_predecrement
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));
153 // has_postincrement
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));
162 // has_postdecrement
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" }