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