convert-to-bool.cpp revision 76f7d287020a0b4996d6e9d3968d5bd9a39f7d84
1// RUN: clang -fsyntax-only -verify %s 2struct ConvToBool { 3 operator bool() const; 4}; 5 6struct ConvToInt { 7 operator int(); 8}; 9 10struct ExplicitConvToBool { 11 explicit operator bool(); // expected-warning{{explicit conversion functions are a C++0x extension}} 12}; 13 14void test_conv_to_bool(ConvToBool ctb, ConvToInt cti, ExplicitConvToBool ecb) { 15 if (ctb) { } 16 if (cti) { } 17 if (ecb) { } 18 for (; ctb; ) { } 19 for (; cti; ) { } 20 for (; ecb; ) { } 21 while (ctb) { }; 22 while (cti) { } 23 while (ecb) { } 24 do { } while (ctb); 25 do { } while (cti); 26 do { } while (ecb); 27 28 if (!ctb) { } 29 if (!cti) { } 30 if (!ecb) { } 31 32 bool b1 = !ecb; 33 if (ctb && ecb) { } 34 bool b2 = ctb && ecb; 35 if (ctb || ecb) { } 36 bool b3 = ctb || ecb; 37} 38 39void accepts_bool(bool) { } 40 41struct ExplicitConvToRef { 42 explicit operator int&(); // expected-warning{{explicit conversion functions are a C++0x extension}} 43}; 44 45void test_explicit_bool(ExplicitConvToBool ecb) { 46 bool b1(ecb); // okay 47 bool b2 = ecb; // expected-error{{incompatible type initializing 'struct ExplicitConvToBool', expected '_Bool'}} 48 accepts_bool(ecb); // expected-error{{incompatible type passing 'struct ExplicitConvToBool', expected '_Bool'}} 49} 50 51void test_explicit_conv_to_ref(ExplicitConvToRef ecr) { 52 int& i1 = ecr; // expected-error{{non-const reference to type 'int' cannot be initialized with a value of type 'struct ExplicitConvToRef'}} 53 int& i2(ecr); // okay 54} 55 56struct A { }; 57struct B { }; 58struct C { 59 explicit operator A&(); // expected-warning{{explicit conversion functions are a C++0x extension}} 60 operator B&(); 61}; 62 63void test_copy_init_conversions(C c) { 64 A &a = c; // expected-error{{non-const reference to type 'struct A' cannot be initialized with a value of type 'struct C'}} 65 B &b = b; // okay 66} 67 68