xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/convert-to-bool.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc struct ConvToBool {
3*f4a2713aSLionel Sambuc   operator bool() const;
4*f4a2713aSLionel Sambuc };
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc struct ConvToInt {
7*f4a2713aSLionel Sambuc   operator int();
8*f4a2713aSLionel Sambuc };
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc struct ExplicitConvToBool {
11*f4a2713aSLionel Sambuc   explicit operator bool(); // expected-warning{{explicit conversion functions are a C++11 extension}}
12*f4a2713aSLionel Sambuc };
13*f4a2713aSLionel Sambuc 
test_conv_to_bool(ConvToBool ctb,ConvToInt cti,ExplicitConvToBool ecb)14*f4a2713aSLionel Sambuc void test_conv_to_bool(ConvToBool ctb, ConvToInt cti, ExplicitConvToBool ecb) {
15*f4a2713aSLionel Sambuc   if (ctb) { }
16*f4a2713aSLionel Sambuc   if (cti) { }
17*f4a2713aSLionel Sambuc   if (ecb) { }
18*f4a2713aSLionel Sambuc   for (; ctb; ) { }
19*f4a2713aSLionel Sambuc   for (; cti; ) { }
20*f4a2713aSLionel Sambuc   for (; ecb; ) { }
21*f4a2713aSLionel Sambuc   while (ctb) { };
22*f4a2713aSLionel Sambuc   while (cti) { }
23*f4a2713aSLionel Sambuc   while (ecb) { }
24*f4a2713aSLionel Sambuc   do { } while (ctb);
25*f4a2713aSLionel Sambuc   do { } while (cti);
26*f4a2713aSLionel Sambuc   do { } while (ecb);
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc   if (!ctb) { }
29*f4a2713aSLionel Sambuc   if (!cti) { }
30*f4a2713aSLionel Sambuc   if (!ecb) { }
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc   bool b1 = !ecb;
33*f4a2713aSLionel Sambuc   if (ctb && ecb) { }
34*f4a2713aSLionel Sambuc   bool b2 = ctb && ecb;
35*f4a2713aSLionel Sambuc   if (ctb || ecb) { }
36*f4a2713aSLionel Sambuc   bool b3 = ctb || ecb;
37*f4a2713aSLionel Sambuc }
38*f4a2713aSLionel Sambuc 
accepts_bool(bool)39*f4a2713aSLionel Sambuc void accepts_bool(bool) { } // expected-note{{candidate function}}
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc struct ExplicitConvToRef {
42*f4a2713aSLionel Sambuc   explicit operator int&(); // expected-warning{{explicit conversion functions are a C++11 extension}}
43*f4a2713aSLionel Sambuc };
44*f4a2713aSLionel Sambuc 
test_explicit_bool(ExplicitConvToBool ecb)45*f4a2713aSLionel Sambuc void test_explicit_bool(ExplicitConvToBool ecb) {
46*f4a2713aSLionel Sambuc   bool b1(ecb); // okay
47*f4a2713aSLionel Sambuc   bool b2 = ecb; // expected-error{{no viable conversion from 'ExplicitConvToBool' to 'bool'}}
48*f4a2713aSLionel Sambuc   accepts_bool(ecb); // expected-error{{no matching function for call to}}
49*f4a2713aSLionel Sambuc }
50*f4a2713aSLionel Sambuc 
test_explicit_conv_to_ref(ExplicitConvToRef ecr)51*f4a2713aSLionel Sambuc void test_explicit_conv_to_ref(ExplicitConvToRef ecr) {
52*f4a2713aSLionel Sambuc   int& i1 = ecr; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'ExplicitConvToRef'}}
53*f4a2713aSLionel Sambuc   int& i2(ecr); // okay
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc struct A { };
57*f4a2713aSLionel Sambuc struct B { };
58*f4a2713aSLionel Sambuc struct C {
59*f4a2713aSLionel Sambuc   explicit operator A&(); // expected-warning{{explicit conversion functions are a C++11 extension}}
60*f4a2713aSLionel Sambuc   operator B&(); // expected-note{{candidate}}
61*f4a2713aSLionel Sambuc };
62*f4a2713aSLionel Sambuc 
test_copy_init_conversions(C c)63*f4a2713aSLionel Sambuc void test_copy_init_conversions(C c) {
64*f4a2713aSLionel Sambuc   A &a = c; // expected-error{{no viable conversion from 'C' to 'A'}}
65*f4a2713aSLionel Sambuc   B &b = c; // okay
66*f4a2713aSLionel Sambuc }
67