xref: /llvm-project/clang/test/SemaCXX/user-defined-conversions.cpp (revision 83ea47acd7116bf50274534ba9b3bd3035c01da6)
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx17 %std_cxx98-14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify=expected %std_cxx17- %s
3 struct X {
4   operator bool();
5 };
6 
7 int& f(bool);
8 float& f(int);
9 
f_test(X x)10 void f_test(X x) {
11   int& i1 = f(x);
12 }
13 
14 struct Y {
15   operator short();
16   operator float();
17 };
18 
19 void g(int);
20 
g_test(Y y)21 void g_test(Y y) {
22   g(y);
23   short s;
24   s = y;
25 }
26 
27 struct A { };
28 struct B : A { };
29 
30 struct C {
31   operator B&();
32 };
33 
34 // Test reference binding via an lvalue conversion function.
35 void h(volatile A&);
h_test(C c)36 void h_test(C c) {
37   h(c);
38 }
39 
40 // Test conversion followed by copy-construction
41 struct FunkyDerived;
42 
43 struct Base {
44   Base(const FunkyDerived&);
45 };
46 
47 struct Derived : Base { };
48 
49 struct FunkyDerived : Base { };
50 
51 struct ConvertibleToBase {
52   operator Base();
53 };
54 
55 struct ConvertibleToDerived {
56   operator Derived();
57 };
58 
59 struct ConvertibleToFunkyDerived {
60   operator FunkyDerived();
61 };
62 
test_conversion(ConvertibleToBase ctb,ConvertibleToDerived ctd,ConvertibleToFunkyDerived ctfd)63 void test_conversion(ConvertibleToBase ctb, ConvertibleToDerived ctd,
64                      ConvertibleToFunkyDerived ctfd) {
65   Base b1 = ctb;
66   Base b2(ctb);
67   Base b3 = ctd;
68   Base b4(ctd);
69   Base b5 = ctfd;
70 }
71 
72 struct X1 {
73   X1(X1&); // precxx17-note{{candidate constructor not viable: expects an lvalue for 1st argument}}
74 };
75 
76 struct X2 {
77   operator X1();
78 };
79 
80 int &f(X1);
81 float &f(...);
82 
g(X2 b)83 void g(X2 b) {
84   int &ir = f(b); // precxx17-error{{no viable constructor copying parameter of type 'X1'}}
85 }
86 
87 namespace rdar10202900 {
88   class A {
89   public:
90     A();
91 
92   private:
93     A(int i); // expected-note{{declared private here}}
94   };
95 
testA(A a)96   void testA(A a) {
97     int b = 10;
98     a = b; // expected-error{{calling a private constructor of class 'rdar10202900::A'}}
99   }
100 }
101