Douglas Gregor | 76f7d28 | 2009-01-16 03:02:29 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify %s |
| 2 | struct ConvToBool { |
| 3 | operator bool() const; |
| 4 | }; |
| 5 | |
| 6 | struct ConvToInt { |
| 7 | operator int(); |
| 8 | }; |
| 9 | |
| 10 | struct ExplicitConvToBool { |
| 11 | explicit operator bool(); // expected-warning{{explicit conversion functions are a C++0x extension}} |
| 12 | }; |
| 13 | |
| 14 | void 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 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 39 | void accepts_bool(bool) { } // expected-note{{candidate function}} |
Douglas Gregor | 76f7d28 | 2009-01-16 03:02:29 +0000 | [diff] [blame] | 40 | |
| 41 | struct ExplicitConvToRef { |
| 42 | explicit operator int&(); // expected-warning{{explicit conversion functions are a C++0x extension}} |
| 43 | }; |
| 44 | |
| 45 | void test_explicit_bool(ExplicitConvToBool ecb) { |
| 46 | bool b1(ecb); // okay |
| 47 | bool b2 = ecb; // expected-error{{incompatible type initializing 'struct ExplicitConvToBool', expected '_Bool'}} |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 48 | accepts_bool(ecb); // expected-error{{no matching function for call to}} |
Douglas Gregor | 76f7d28 | 2009-01-16 03:02:29 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void test_explicit_conv_to_ref(ExplicitConvToRef ecr) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame^] | 52 | int& i1 = ecr; // expected-error{{non-const lvalue reference to type 'int' cannot be initialized with a value of type 'struct ExplicitConvToRef'}} |
Douglas Gregor | 76f7d28 | 2009-01-16 03:02:29 +0000 | [diff] [blame] | 53 | int& i2(ecr); // okay |
| 54 | } |
| 55 | |
| 56 | struct A { }; |
| 57 | struct B { }; |
| 58 | struct C { |
| 59 | explicit operator A&(); // expected-warning{{explicit conversion functions are a C++0x extension}} |
| 60 | operator B&(); |
| 61 | }; |
| 62 | |
| 63 | void test_copy_init_conversions(C c) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame^] | 64 | A &a = c; // expected-error{{non-const lvalue reference to type 'struct A' cannot be initialized with a value of type 'struct C'}} |
Douglas Gregor | 76f7d28 | 2009-01-16 03:02:29 +0000 | [diff] [blame] | 65 | B &b = b; // okay |
| 66 | } |
| 67 | |