summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/template/sfinae9.C
blob: 4e27ff61bd5eb490cd9ce48f1b196bce4ac5d462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// DR 339
//
// Test of the use of various assignment operators with SFINAE

// Boilerplate helpers
typedef char yes_type;
struct no_type { char data[2]; };

template<typename T> T create_a();
template<typename T> struct type { };

template<bool, typename T = void> struct enable_if { typedef T type; };
template<typename T> struct enable_if<false, T> { };

#define JOIN( X, Y ) DO_JOIN( X, Y )
#define DO_JOIN( X, Y ) DO_JOIN2(X,Y)
#define DO_JOIN2( X, Y ) X##Y

#define DEFINE_INFIX_BINARY_TRAIT(Name,Op)				\
template<typename T, typename U>					\
  typename enable_if<(sizeof(create_a<T>() Op create_a<U>(), 1) > 0),	\
		     yes_type>::type					\
  JOIN(check_,Name)(type<T>, type<U>);		                        \
									\
no_type JOIN(check_,Name)(...);						\
									\
template<typename T, typename U = T>					\
struct Name								\
{									\
  static const bool value =						\
    (sizeof(JOIN(check_,Name)(type<T&>(), type<U>())) == sizeof(yes_type)); \
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#  define STATIC_ASSERT(Expr) static_assert(Expr, #Expr)
#else
#  define STATIC_ASSERT(Expr) int JOIN(a,__LINE__)[Expr? 1 : -1]
#endif

struct Y { 
  Y& operator=(Y&);
};

struct X { 
  X& operator=(Y);
  X& operator+=(X);
  X& operator-=(X);
  X& operator*=(X);
  X& operator/=(X);
  X& operator%=(X);
  X& operator^=(X);
  X& operator&=(X);
  X& operator|=(X);
  X& operator<<=(X);
  X& operator>>=(X);
};
struct Z { };

// is_assignable
DEFINE_INFIX_BINARY_TRAIT(is_assignable, =);
STATIC_ASSERT((is_assignable<int>::value));
STATIC_ASSERT((is_assignable<int, long>::value));
STATIC_ASSERT((is_assignable<X>::value));
STATIC_ASSERT((!is_assignable<int*, int>::value));
STATIC_ASSERT((is_assignable<int*>::value));
STATIC_ASSERT((is_assignable<X, Y>::value));
STATIC_ASSERT((!is_assignable<X, Z>::value));
STATIC_ASSERT((!is_assignable<Y>::value));
STATIC_ASSERT((!is_assignable<const int, long>::value));

// has_plus_assign
DEFINE_INFIX_BINARY_TRAIT(has_plus_assign, +=);
X& operator+=(X&, Y);
STATIC_ASSERT((has_plus_assign<int>::value));
STATIC_ASSERT((has_plus_assign<int, long>::value));
STATIC_ASSERT((has_plus_assign<X>::value));
STATIC_ASSERT((has_plus_assign<int*, int>::value));
STATIC_ASSERT((!has_plus_assign<int*>::value));
STATIC_ASSERT((has_plus_assign<X, Y>::value));
STATIC_ASSERT((!has_plus_assign<X, Z>::value));
STATIC_ASSERT((!has_plus_assign<Y>::value));
STATIC_ASSERT((!has_plus_assign<const int, long>::value));

// has_minus_assign
DEFINE_INFIX_BINARY_TRAIT(has_minus_assign, -=);
X& operator-=(X&, Y);
STATIC_ASSERT((has_minus_assign<int>::value));
STATIC_ASSERT((has_minus_assign<int, long>::value));
STATIC_ASSERT((has_minus_assign<X>::value));
STATIC_ASSERT((has_minus_assign<int*, int>::value));
STATIC_ASSERT((!has_minus_assign<int*>::value));
STATIC_ASSERT((has_minus_assign<X, Y>::value));
STATIC_ASSERT((!has_minus_assign<X, Z>::value));
STATIC_ASSERT((!has_minus_assign<Y>::value));
STATIC_ASSERT((!has_minus_assign<int X::*>::value));
STATIC_ASSERT((!has_minus_assign<const int, long>::value));

// has_multiply_assign
DEFINE_INFIX_BINARY_TRAIT(has_multiply_assign, *=);
X& operator*=(X&, Y);
STATIC_ASSERT((has_multiply_assign<int>::value));
STATIC_ASSERT((has_multiply_assign<int, long>::value));
STATIC_ASSERT((has_multiply_assign<X>::value));
STATIC_ASSERT((!has_multiply_assign<int*, int>::value));
STATIC_ASSERT((!has_multiply_assign<int*>::value));
STATIC_ASSERT((has_multiply_assign<X, Y>::value));
STATIC_ASSERT((!has_multiply_assign<X, Z>::value));
STATIC_ASSERT((!has_multiply_assign<Y>::value));
STATIC_ASSERT((!has_multiply_assign<int X::*>::value));
STATIC_ASSERT((!has_multiply_assign<const int, long>::value));

// has_divide_assign
DEFINE_INFIX_BINARY_TRAIT(has_divide_assign, /=);
X& operator/=(X&, Y);
STATIC_ASSERT((has_divide_assign<int>::value));
STATIC_ASSERT((has_divide_assign<int, long>::value));
STATIC_ASSERT((has_divide_assign<X>::value));
STATIC_ASSERT((!has_divide_assign<int*, int>::value));
STATIC_ASSERT((!has_divide_assign<int*>::value));
STATIC_ASSERT((has_divide_assign<X, Y>::value));
STATIC_ASSERT((!has_divide_assign<X, Z>::value));
STATIC_ASSERT((!has_divide_assign<Y>::value));
STATIC_ASSERT((!has_divide_assign<int X::*>::value));

// has_remainder_assign
DEFINE_INFIX_BINARY_TRAIT(has_remainder_assign, %=);
X& operator%=(X&, Y);
STATIC_ASSERT((has_remainder_assign<int>::value));
STATIC_ASSERT((has_remainder_assign<int, long>::value));
STATIC_ASSERT((!has_remainder_assign<float>::value));
STATIC_ASSERT((has_remainder_assign<X>::value));
STATIC_ASSERT((!has_remainder_assign<int*, int>::value));
STATIC_ASSERT((!has_remainder_assign<int*>::value));
STATIC_ASSERT((has_remainder_assign<X, Y>::value));
STATIC_ASSERT((!has_remainder_assign<X, Z>::value));
STATIC_ASSERT((!has_remainder_assign<Y>::value));
STATIC_ASSERT((!has_remainder_assign<int X::*>::value));

// has_xor_assign
DEFINE_INFIX_BINARY_TRAIT(has_xor_assign, ^=);
X& operator^=(X&, Y);
STATIC_ASSERT((has_xor_assign<int>::value));
STATIC_ASSERT((has_xor_assign<int, long>::value));
STATIC_ASSERT((!has_xor_assign<float>::value));
STATIC_ASSERT((has_xor_assign<X>::value));
STATIC_ASSERT((!has_xor_assign<int*, int>::value));
STATIC_ASSERT((!has_xor_assign<int*>::value));
STATIC_ASSERT((has_xor_assign<X, Y>::value));
STATIC_ASSERT((!has_xor_assign<X, Z>::value));
STATIC_ASSERT((!has_xor_assign<Y>::value));
STATIC_ASSERT((!has_xor_assign<int X::*>::value));

// has_bitand_assign
DEFINE_INFIX_BINARY_TRAIT(has_bitand_assign, &=);
X& operator&=(X&, Y);
STATIC_ASSERT((has_bitand_assign<int>::value));
STATIC_ASSERT((has_bitand_assign<int, long>::value));
STATIC_ASSERT((!has_bitand_assign<float>::value));
STATIC_ASSERT((has_bitand_assign<X>::value));
STATIC_ASSERT((!has_bitand_assign<int*, int>::value));
STATIC_ASSERT((!has_bitand_assign<int*>::value));
STATIC_ASSERT((has_bitand_assign<X, Y>::value));
STATIC_ASSERT((!has_bitand_assign<X, Z>::value));
STATIC_ASSERT((!has_bitand_assign<Y>::value));
STATIC_ASSERT((!has_bitand_assign<int X::*>::value));

// has_bitor_assign
DEFINE_INFIX_BINARY_TRAIT(has_bitor_assign, |=);
X& operator|=(X&, Y);
STATIC_ASSERT((has_bitor_assign<int>::value));
STATIC_ASSERT((has_bitor_assign<int, long>::value));
STATIC_ASSERT((!has_bitor_assign<float>::value));
STATIC_ASSERT((has_bitor_assign<X>::value));
STATIC_ASSERT((!has_bitor_assign<int*, int>::value));
STATIC_ASSERT((!has_bitor_assign<int*>::value));
STATIC_ASSERT((has_bitor_assign<X, Y>::value));
STATIC_ASSERT((!has_bitor_assign<X, Z>::value));
STATIC_ASSERT((!has_bitor_assign<Y>::value));
STATIC_ASSERT((!has_bitor_assign<int X::*>::value));

// has_left_shift_assign
DEFINE_INFIX_BINARY_TRAIT(has_left_shift_assign, <<=);
X& operator<<=(X&, Y);
STATIC_ASSERT((has_left_shift_assign<int>::value));
STATIC_ASSERT((has_left_shift_assign<int, long>::value));
STATIC_ASSERT((!has_left_shift_assign<float>::value));
STATIC_ASSERT((has_left_shift_assign<X>::value));
STATIC_ASSERT((!has_left_shift_assign<int*, int>::value));
STATIC_ASSERT((!has_left_shift_assign<int*>::value));
STATIC_ASSERT((has_left_shift_assign<X, Y>::value));
STATIC_ASSERT((!has_left_shift_assign<X, Z>::value));
STATIC_ASSERT((!has_left_shift_assign<Y>::value));
STATIC_ASSERT((!has_left_shift_assign<int X::*>::value));

// has_right_shift_assign
DEFINE_INFIX_BINARY_TRAIT(has_right_shift_assign, >>=);
X& operator>>=(X&, Y);
STATIC_ASSERT((has_right_shift_assign<int>::value));
STATIC_ASSERT((has_right_shift_assign<int, long>::value));
STATIC_ASSERT((!has_right_shift_assign<float>::value));
STATIC_ASSERT((has_right_shift_assign<X>::value));
STATIC_ASSERT((!has_right_shift_assign<int*, int>::value));
STATIC_ASSERT((!has_right_shift_assign<int*>::value));
STATIC_ASSERT((has_right_shift_assign<X, Y>::value));
STATIC_ASSERT((!has_right_shift_assign<X, Z>::value));
STATIC_ASSERT((!has_right_shift_assign<Y>::value));
STATIC_ASSERT((!has_right_shift_assign<int X::*>::value));