Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -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++11 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 
     39 void accepts_bool(bool) { } // expected-note{{candidate function}}
     40 
     41 struct ExplicitConvToRef {
     42   explicit operator int&(); // expected-warning{{explicit conversion functions are a C++11 extension}}
     43 };
     44 
     45 void test_explicit_bool(ExplicitConvToBool ecb) {
     46   bool b1(ecb); // okay
     47   bool b2 = ecb; // expected-error{{no viable conversion from 'ExplicitConvToBool' to 'bool'}}
     48   accepts_bool(ecb); // expected-error{{no matching function for call to}}
     49 }
     50 
     51 void test_explicit_conv_to_ref(ExplicitConvToRef ecr) {
     52   int& i1 = ecr; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'ExplicitConvToRef'}}
     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++11 extension}}
     60   operator B&(); // expected-note{{candidate}}
     61 };
     62 
     63 void test_copy_init_conversions(C c) {
     64   A &a = c; // expected-error{{no viable conversion from 'C' to 'A'}}
     65   B &b = c; // okay
     66 }
     67