convert-to-bool.cpp [plain text]
struct ConvToBool {
operator bool() const;
};
struct ConvToInt {
operator int();
};
struct ExplicitConvToBool {
explicit operator bool(); };
void test_conv_to_bool(ConvToBool ctb, ConvToInt cti, ExplicitConvToBool ecb) {
if (ctb) { }
if (cti) { }
if (ecb) { }
for (; ctb; ) { }
for (; cti; ) { }
for (; ecb; ) { }
while (ctb) { };
while (cti) { }
while (ecb) { }
do { } while (ctb);
do { } while (cti);
do { } while (ecb);
if (!ctb) { }
if (!cti) { }
if (!ecb) { }
bool b1 = !ecb;
if (ctb && ecb) { }
bool b2 = ctb && ecb;
if (ctb || ecb) { }
bool b3 = ctb || ecb;
}
void accepts_bool(bool) { }
struct ExplicitConvToRef {
explicit operator int&(); };
void test_explicit_bool(ExplicitConvToBool ecb) {
bool b1(ecb); bool b2 = ecb; accepts_bool(ecb); }
void test_explicit_conv_to_ref(ExplicitConvToRef ecr) {
int& i1 = ecr; int& i2(ecr); }
struct A { };
struct B { };
struct C {
explicit operator A&(); operator B&(); };
void test_copy_init_conversions(C c) {
A &a = c; B &b = c; }