| 1 |
// PR c++/21802 |
| 2 |
// { dg-do run } |
| 3 |
// { dg-require-effective-target size20plus } |
| 4 |
// { dg-skip-if "requires hosted libstdc++ for cassert" { ! hostedlib } } |
| 5 |
|
| 6 |
#include <cassert> |
| 7 |
|
| 8 |
struct X; |
| 9 |
int I = 6; |
| 10 |
|
| 11 |
/* A mostly exhaustive and ad-hoc assortment of operator overloads and calls |
| 12 |
thereof, to stress-test two-stage name lookup of operators inside template |
| 13 |
definitions and then to verify that the calls get built correctly. */ |
| 14 |
|
| 15 |
template <typename T> |
| 16 |
inline int operator+(const X &, T x) { return x; } |
| 17 |
inline int operator-(const X &, int x) { return x; } |
| 18 |
inline int operator*(const X &, int x) { return x; } |
| 19 |
inline int operator/(const X &, int x) { return x; } |
| 20 |
inline int operator+=(const X &, int x) { return x; } |
| 21 |
|
| 22 |
struct X |
| 23 |
{ |
| 24 |
X () : m (1) { } |
| 25 |
template <typename T> |
| 26 |
int operator%(T x) { return m + x; } |
| 27 |
virtual int operator>>(int x) { return m + x; } |
| 28 |
int operator<<(int x) { return m + x; } |
| 29 |
int operator&(int x) { return m + x; } |
| 30 |
int operator|(int x) { return m + x; } |
| 31 |
int operator^(int x) { return m + x; } |
| 32 |
int operator&&(int x) { return m + x; } |
| 33 |
int operator||(int x) { return m + x; } |
| 34 |
friend int operator==(X o, int x) { return o.m + x; } |
| 35 |
int operator!=(int x) { return m + x; } |
| 36 |
int operator<(int x) { return m + x; } |
| 37 |
int operator<=(int x) { return m + x; } |
| 38 |
int operator>(int x) { return m + x; } |
| 39 |
int operator>=(int x) { return m + x; } |
| 40 |
int operator*() { return m + I; } |
| 41 |
int operator!() { return m + I; } |
| 42 |
int operator~() { return m + I; } |
| 43 |
int operator++() { return m + I + 100; } |
| 44 |
int operator--() { return m + I + 100; } |
| 45 |
int operator++(int) { return m + I; } |
| 46 |
int operator--(int) { return m + I; } |
| 47 |
int operator()() { return m + I; } |
| 48 |
int operator,(int x) { return m + x; } |
| 49 |
int operator[](int x) { return m + x; } |
| 50 |
int operator*=(int x) { return m + x; } |
| 51 |
int operator-=(int x) { return m + x; } |
| 52 |
int operator/=(int x) { return m + x; } |
| 53 |
virtual int operator& () { return m + I; } |
| 54 |
int m; |
| 55 |
}; |
| 56 |
struct Y : virtual X |
| 57 |
{ |
| 58 |
/* Virtual override. */ |
| 59 |
int operator>>(int x) { return m + x + 1; } |
| 60 |
int operator& () { return m + I + 1; } |
| 61 |
|
| 62 |
/* Not virtual. */ |
| 63 |
template <typename T> |
| 64 |
int operator&(T x) { return m + x + 1; } |
| 65 |
friend int operator==(Y o, int x) { return o.m + x + 1; } |
| 66 |
int operator!=(int x) { return m + x + 1; } |
| 67 |
}; |
| 68 |
|
| 69 |
/* The folloiwng "FooN" functions each contain a different way to call and to |
| 70 |
resolve these operator overloads. */ |
| 71 |
|
| 72 |
template <typename T> |
| 73 |
void |
| 74 |
Foo1 (T) |
| 75 |
{ |
| 76 |
Y x; |
| 77 |
{ int t = x + I; assert (t == 6); } |
| 78 |
{ int t = x - I; assert (t == 6); } |
| 79 |
{ int t = x * I; assert (t == 6); } |
| 80 |
{ int t = x / I; assert (t == 6); } |
| 81 |
{ int t = (x+=I); assert (t == 6); } |
| 82 |
|
| 83 |
{ int t = x % I; assert (t == 7); } |
| 84 |
{ int t = x << I; assert (t == 7); } |
| 85 |
{ int t = x | I; assert (t == 7); } |
| 86 |
{ int t = x && I; assert (t == 7); } |
| 87 |
{ int t = x || I; assert (t == 7); } |
| 88 |
{ int t = x < I; assert (t == 7); } |
| 89 |
{ int t = x <= I; assert (t == 7); } |
| 90 |
{ int t = x > I; assert (t == 7); } |
| 91 |
{ int t = x >= I; assert (t == 7); } |
| 92 |
{ int t = *x; assert (t == 7); } |
| 93 |
{ int t = !x; assert (t == 7); } |
| 94 |
{ int t = ~x; assert (t == 7); } |
| 95 |
{ int t = x++; assert (t == 7); } |
| 96 |
{ int t = x--; assert (t == 7); } |
| 97 |
{ int t = ++x; assert (t == 107); } |
| 98 |
{ int t = --x; assert (t == 107); } |
| 99 |
{ int t = x (); assert (t == 7); } |
| 100 |
{ int t = (x, I); assert (t == 7); } |
| 101 |
{ int t = x[I]; assert (t == 7); } |
| 102 |
{ int t = (x-=I); assert (t == 7); } |
| 103 |
{ int t = (x/=I); assert (t == 7); } |
| 104 |
{ int t = (x*=I); assert (t == 7); } |
| 105 |
|
| 106 |
{ int t = x >> I; assert (t == 8); } |
| 107 |
{ int t = x & I; assert (t == 8); } |
| 108 |
{ int t = &x; assert (t == 8); } |
| 109 |
{ int t = x == I; assert (t == 8); } |
| 110 |
{ int t = x != I; assert (t == 8); } |
| 111 |
} |
| 112 |
|
| 113 |
template <typename T> |
| 114 |
void |
| 115 |
Foo2 (T) |
| 116 |
{ |
| 117 |
X x; |
| 118 |
{ int t = x + I; assert (t == 6); } |
| 119 |
{ int t = x - I; assert (t == 6); } |
| 120 |
{ int t = x * I; assert (t == 6); } |
| 121 |
{ int t = x / I; assert (t == 6); } |
| 122 |
{ int t = (x+=I); assert (t == 6); } |
| 123 |
|
| 124 |
{ int t = x % I; assert (t == 7); } |
| 125 |
{ int t = x >> I; assert (t == 7); } |
| 126 |
{ int t = x << I; assert (t == 7); } |
| 127 |
{ int t = x | I; assert (t == 7); } |
| 128 |
{ int t = x && I; assert (t == 7); } |
| 129 |
{ int t = x || I; assert (t == 7); } |
| 130 |
{ int t = x == I; assert (t == 7); } |
| 131 |
{ int t = x != I; assert (t == 7); } |
| 132 |
{ int t = x < I; assert (t == 7); } |
| 133 |
{ int t = x <= I; assert (t == 7); } |
| 134 |
{ int t = x > I; assert (t == 7); } |
| 135 |
{ int t = x >= I; assert (t == 7); } |
| 136 |
{ int t = *x; assert (t == 7); } |
| 137 |
{ int t = !x; assert (t == 7); } |
| 138 |
{ int t = ~x; assert (t == 7); } |
| 139 |
{ int t = x++; assert (t == 7); } |
| 140 |
{ int t = x--; assert (t == 7); } |
| 141 |
{ int t = ++x; assert (t == 107); } |
| 142 |
{ int t = --x; assert (t == 107); } |
| 143 |
{ int t = x (); assert (t == 7); } |
| 144 |
{ int t = (x, I); assert (t == 7); } |
| 145 |
{ int t = x[I]; assert (t == 7); } |
| 146 |
{ int t = &x; assert (t == 7); } |
| 147 |
{ int t = (x-=I); assert (t == 7); } |
| 148 |
{ int t = (x/=I); assert (t == 7); } |
| 149 |
{ int t = (x*=I); assert (t == 7); } |
| 150 |
{ int t = x & I; assert (t == 7); } |
| 151 |
} |
| 152 |
|
| 153 |
template <typename T> |
| 154 |
void |
| 155 |
Foo3 (T) |
| 156 |
{ |
| 157 |
Y o; |
| 158 |
X &x = o; |
| 159 |
{ int t = x + I; assert (t == 6); } |
| 160 |
{ int t = x - I; assert (t == 6); } |
| 161 |
{ int t = x * I; assert (t == 6); } |
| 162 |
{ int t = x / I; assert (t == 6); } |
| 163 |
{ int t = (x+=I); assert (t == 6); } |
| 164 |
|
| 165 |
{ int t = x % I; assert (t == 7); } |
| 166 |
{ int t = x << I; assert (t == 7); } |
| 167 |
{ int t = x | I; assert (t == 7); } |
| 168 |
{ int t = x && I; assert (t == 7); } |
| 169 |
{ int t = x || I; assert (t == 7); } |
| 170 |
{ int t = x == I; assert (t == 7); } |
| 171 |
{ int t = x != I; assert (t == 7); } |
| 172 |
{ int t = x < I; assert (t == 7); } |
| 173 |
{ int t = x <= I; assert (t == 7); } |
| 174 |
{ int t = x > I; assert (t == 7); } |
| 175 |
{ int t = x >= I; assert (t == 7); } |
| 176 |
{ int t = *x; assert (t == 7); } |
| 177 |
{ int t = !x; assert (t == 7); } |
| 178 |
{ int t = ~x; assert (t == 7); } |
| 179 |
{ int t = x++; assert (t == 7); } |
| 180 |
{ int t = x--; assert (t == 7); } |
| 181 |
{ int t = ++x; assert (t == 107); } |
| 182 |
{ int t = --x; assert (t == 107); } |
| 183 |
{ int t = x (); assert (t == 7); } |
| 184 |
{ int t = (x, I); assert (t == 7); } |
| 185 |
{ int t = x[I]; assert (t == 7); } |
| 186 |
{ int t = (x-=I); assert (t == 7); } |
| 187 |
{ int t = (x/=I); assert (t == 7); } |
| 188 |
{ int t = (x*=I); assert (t == 7); } |
| 189 |
|
| 190 |
{ int t = x & I; assert (t == 7); } |
| 191 |
{ int t = x >> I; assert (t == 8); } |
| 192 |
{ int t = &x; assert (t == 8); } |
| 193 |
} |
| 194 |
|
| 195 |
template <typename T> |
| 196 |
void |
| 197 |
Foo4 (T) |
| 198 |
{ |
| 199 |
Y x; |
| 200 |
{ int t = operator+ (x, I); assert (t == 6); } |
| 201 |
{ int t = operator- (x, I); assert (t == 6); } |
| 202 |
{ int t = operator* (x, I); assert (t == 6); } |
| 203 |
{ int t = operator/ (x, I); assert (t == 6); } |
| 204 |
{ int t = operator+= (x, I); assert (t == 6); } |
| 205 |
|
| 206 |
{ int t = x.operator% (I); assert (t == 7); } |
| 207 |
{ int t = x.operator<< (I); assert (t == 7); } |
| 208 |
{ int t = x.operator| (I); assert (t == 7); } |
| 209 |
{ int t = x.operator&& (I); assert (t == 7); } |
| 210 |
{ int t = x.operator|| (I); assert (t == 7); } |
| 211 |
{ int t = x.operator< (I); assert (t == 7); } |
| 212 |
{ int t = x.operator<= (I); assert (t == 7); } |
| 213 |
{ int t = x.operator> (I); assert (t == 7); } |
| 214 |
{ int t = x.operator>= (I); assert (t == 7); } |
| 215 |
{ int t = x.operator* (); assert (t == 7); } |
| 216 |
{ int t = x.operator! (); assert (t == 7); } |
| 217 |
{ int t = x.operator~ (); assert (t == 7); } |
| 218 |
{ int t = x.operator++ (0); assert (t == 7); } |
| 219 |
{ int t = x.operator-- (0); assert (t == 7); } |
| 220 |
{ int t = x.operator++ (); assert (t == 107); } |
| 221 |
{ int t = x.operator-- (); assert (t == 107); } |
| 222 |
{ int t = x.operator() (); assert (t == 7); } |
| 223 |
{ int t = x.operator, (I); assert (t == 7); } |
| 224 |
{ int t = x.operator[] (I); assert (t == 7); } |
| 225 |
{ int t = x.operator-= (I); assert (t == 7); } |
| 226 |
{ int t = x.operator/= (I); assert (t == 7); } |
| 227 |
{ int t = x.operator*= (I); assert (t == 7); } |
| 228 |
|
| 229 |
{ int t = x.operator>> (I); assert (t == 8); } |
| 230 |
{ int t = x.operator& (); assert (t == 8); } |
| 231 |
{ int t = x.operator& (I); assert (t == 8); } |
| 232 |
{ int t = operator== (x, I); assert (t == 8); } |
| 233 |
{ int t = x.operator!= (I); assert (t == 8); } |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
/* These definitions should be irrelevant to operator lookup of non-dependent |
| 238 |
expressions inside the above templates since they are not in scope at |
| 239 |
template-definition time (even though they are in scope at instantiation |
| 240 |
time). */ |
| 241 |
inline int operator+(const Y&, int) { return 11; } |
| 242 |
inline int operator-(const Y&, int) { return 11; } |
| 243 |
inline int operator*(const Y&, int) { return 11; } |
| 244 |
inline int operator/(const Y&, int) { return 11; } |
| 245 |
inline int operator%(const Y&, int) { return 11; } |
| 246 |
inline int operator>>(const Y&, int) { return 11; } |
| 247 |
inline int operator<<(const Y&, int) { return 11; } |
| 248 |
inline int operator&(const Y&, int) { return 11; } |
| 249 |
inline int operator|(const Y&, int) { return 11; } |
| 250 |
inline int operator^(const Y&, int) { return 11; } |
| 251 |
inline int operator&&(const Y&, int) { return 11; } |
| 252 |
inline int operator||(const Y&, int) { return 11; } |
| 253 |
inline int operator==(const Y&, int) { return 11; } |
| 254 |
inline int operator!=(const Y&, int) { return 11; } |
| 255 |
inline int operator<(const Y&, int) { return 11; } |
| 256 |
inline int operator<=(const Y&, int) { return 11; } |
| 257 |
inline int operator>(const Y&, int) { return 11; } |
| 258 |
inline int operator>=(const Y&, int) { return 11; } |
| 259 |
inline int operator*(const Y&) { return 11; } |
| 260 |
inline int operator!(const Y&) { return 11; } |
| 261 |
inline int operator~(const Y&) { return 11; } |
| 262 |
inline int operator++(const Y&) { return 11; } |
| 263 |
inline int operator--(const Y&) { return 11; } |
| 264 |
inline int operator++(const Y&, int) { return 11; } |
| 265 |
inline int operator--(const Y&, int) { return 11; } |
| 266 |
inline int operator,(const Y&, int) { return 11; } |
| 267 |
inline int operator&(const Y&) { return 11; } |
| 268 |
inline int operator+=(const Y&, int x) { return 11; } |
| 269 |
inline int operator*=(const Y&, int x) { return 11; } |
| 270 |
inline int operator-=(const Y&, int x) { return 11; } |
| 271 |
inline int operator/=(const Y&, int x) { return 11; } |
| 272 |
|
| 273 |
int |
| 274 |
main () |
| 275 |
{ |
| 276 |
Foo1 (0); |
| 277 |
Foo2 (0); |
| 278 |
Foo3 (0); |
| 279 |
Foo4 (0); |
| 280 |
} |