| 1 |
// Test for "contextually converted to bool" |
| 2 |
// { dg-do compile { target c++11 } } |
| 3 |
|
| 4 |
struct A |
| 5 |
{ |
| 6 |
explicit operator bool(); |
| 7 |
}; |
| 8 |
|
| 9 |
void f (bool); |
| 10 |
|
| 11 |
struct B |
| 12 |
{ |
| 13 |
bool b; |
| 14 |
}; |
| 15 |
|
| 16 |
struct C |
| 17 |
{ |
| 18 |
operator int(); |
| 19 |
}; |
| 20 |
|
| 21 |
struct D |
| 22 |
{ |
| 23 |
operator int(); |
| 24 |
}; |
| 25 |
|
| 26 |
int main() |
| 27 |
{ |
| 28 |
A a; C c; D d; |
| 29 |
// These contexts use an explicit bool conversion. |
| 30 |
if (a) {} |
| 31 |
for (; a; ) {} |
| 32 |
do {} while (a); |
| 33 |
while (a) {} |
| 34 |
a ? 1 : 0; |
| 35 |
a || true; |
| 36 |
a && true; |
| 37 |
!a; |
| 38 |
|
| 39 |
a ? c : 1; |
| 40 |
a ? c : d; |
| 41 |
|
| 42 |
// These do not. |
| 43 |
switch (a); // { dg-error "" } |
| 44 |
bool b = a; // { dg-error "" } |
| 45 |
f(a); // { dg-error "" } |
| 46 |
B b2 = { a }; // { dg-error "" } |
| 47 |
a + true; // { dg-error "5:no match" } |
| 48 |
b ? a : true; // { dg-error "5:?:" } |
| 49 |
a ? a : true; // { dg-error "5:?:" } |
| 50 |
} |