blob: 117b50899b7ef9437211c6356049d6552cd941d7 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Charles Li542f04c2015-11-11 19:34:47 +00002// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
Douglas Gregor6bd18af2009-01-16 03:02:29 +00005struct ConvToBool {
6 operator bool() const;
7};
8
9struct ConvToInt {
10 operator int();
11};
12
13struct ExplicitConvToBool {
Charles Li542f04c2015-11-11 19:34:47 +000014 explicit operator bool();
15#if __cplusplus <= 199711L // C++03 or earlier modes
16 // expected-warning@-2{{explicit conversion functions are a C++11 extension}}
17#endif
Douglas Gregor6bd18af2009-01-16 03:02:29 +000018};
19
20void test_conv_to_bool(ConvToBool ctb, ConvToInt cti, ExplicitConvToBool ecb) {
21 if (ctb) { }
22 if (cti) { }
23 if (ecb) { }
24 for (; ctb; ) { }
25 for (; cti; ) { }
26 for (; ecb; ) { }
27 while (ctb) { };
28 while (cti) { }
29 while (ecb) { }
30 do { } while (ctb);
31 do { } while (cti);
32 do { } while (ecb);
33
34 if (!ctb) { }
35 if (!cti) { }
36 if (!ecb) { }
37
38 bool b1 = !ecb;
39 if (ctb && ecb) { }
40 bool b2 = ctb && ecb;
41 if (ctb || ecb) { }
42 bool b3 = ctb || ecb;
43}
44
Douglas Gregore254f902009-02-04 00:32:51 +000045void accepts_bool(bool) { } // expected-note{{candidate function}}
Douglas Gregor6bd18af2009-01-16 03:02:29 +000046
47struct ExplicitConvToRef {
Charles Li542f04c2015-11-11 19:34:47 +000048 explicit operator int&();
49#if (__cplusplus <= 199711L) // C++03 or earlier modes
50 // expected-warning@-2{{explicit conversion functions are a C++11 extension}}
51#endif
Douglas Gregor6bd18af2009-01-16 03:02:29 +000052};
53
54void test_explicit_bool(ExplicitConvToBool ecb) {
55 bool b1(ecb); // okay
John McCall85f90552010-03-10 11:27:22 +000056 bool b2 = ecb; // expected-error{{no viable conversion from 'ExplicitConvToBool' to 'bool'}}
Douglas Gregore254f902009-02-04 00:32:51 +000057 accepts_bool(ecb); // expected-error{{no matching function for call to}}
Douglas Gregor6bd18af2009-01-16 03:02:29 +000058}
59
60void test_explicit_conv_to_ref(ExplicitConvToRef ecr) {
John McCall85f90552010-03-10 11:27:22 +000061 int& i1 = ecr; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'ExplicitConvToRef'}}
Douglas Gregor6bd18af2009-01-16 03:02:29 +000062 int& i2(ecr); // okay
63}
64
65struct A { };
66struct B { };
67struct C {
Charles Li542f04c2015-11-11 19:34:47 +000068 explicit operator A&();
69#if __cplusplus <= 199711L // C++03 or earlier modes
70// expected-warning@-2{{explicit conversion functions are a C++11 extension}}
71#endif
Douglas Gregor3e1e5272009-12-09 23:02:17 +000072 operator B&(); // expected-note{{candidate}}
Douglas Gregor6bd18af2009-01-16 03:02:29 +000073};
74
75void test_copy_init_conversions(C c) {
John McCall85f90552010-03-10 11:27:22 +000076 A &a = c; // expected-error{{no viable conversion from 'C' to 'A'}}
Hans Wennborge1fdb052012-08-17 10:12:33 +000077 B &b = c; // okay
Douglas Gregor6bd18af2009-01-16 03:02:29 +000078}