xref: /aosp_15_r20/external/clang/test/SemaCXX/cxx0x-initializer-references.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li struct one { char c; };
4*67e74705SXin Li struct two { char c[2]; };
5*67e74705SXin Li 
6*67e74705SXin Li namespace reference {
7*67e74705SXin Li   struct A {
8*67e74705SXin Li     int i1, i2;
9*67e74705SXin Li   };
10*67e74705SXin Li 
single_init()11*67e74705SXin Li   void single_init() {
12*67e74705SXin Li     const int &cri1a = {1};
13*67e74705SXin Li     const int &cri1b{1};
14*67e74705SXin Li 
15*67e74705SXin Li     int i = 1;
16*67e74705SXin Li     int &ri1a = {i};
17*67e74705SXin Li     int &ri1b{i};
18*67e74705SXin Li 
19*67e74705SXin Li     int &ri2 = {1}; // expected-error {{cannot bind to an initializer list temporary}}
20*67e74705SXin Li 
21*67e74705SXin Li     A a{1, 2};
22*67e74705SXin Li     A &ra1a = {a};
23*67e74705SXin Li     A &ra1b{a};
24*67e74705SXin Li   }
25*67e74705SXin Li 
reference_to_aggregate()26*67e74705SXin Li   void reference_to_aggregate() {
27*67e74705SXin Li     const A &ra1{1, 2};
28*67e74705SXin Li     A &ra2{1, 2}; // expected-error {{cannot bind to an initializer list temporary}}
29*67e74705SXin Li 
30*67e74705SXin Li     const int (&arrayRef)[] = {1, 2, 3};
31*67e74705SXin Li     static_assert(sizeof(arrayRef) == 3 * sizeof(int), "bad array size");
32*67e74705SXin Li   }
33*67e74705SXin Li 
34*67e74705SXin Li   struct B {
35*67e74705SXin Li     int i1;
36*67e74705SXin Li   };
37*67e74705SXin Li 
call()38*67e74705SXin Li   void call() {
39*67e74705SXin Li     one f(const int&);
40*67e74705SXin Li     f({1});
41*67e74705SXin Li 
42*67e74705SXin Li     one g(int&); // expected-note {{passing argument}}
43*67e74705SXin Li     g({1}); // expected-error {{cannot bind to an initializer list temporary}}
44*67e74705SXin Li     int i = 0;
45*67e74705SXin Li     g({i});
46*67e74705SXin Li 
47*67e74705SXin Li     void h(const B&);
48*67e74705SXin Li     h({1});
49*67e74705SXin Li 
50*67e74705SXin Li     void a(B&); // expected-note {{passing argument}}
51*67e74705SXin Li     a({1}); // expected-error {{cannot bind to an initializer list temporary}}
52*67e74705SXin Li     B b{1};
53*67e74705SXin Li     a({b});
54*67e74705SXin Li   }
55*67e74705SXin Li 
overloading()56*67e74705SXin Li   void overloading() {
57*67e74705SXin Li     one f(const int&);
58*67e74705SXin Li     two f(const B&);
59*67e74705SXin Li 
60*67e74705SXin Li     // First is identity conversion, second is user-defined conversion.
61*67e74705SXin Li     static_assert(sizeof(f({1})) == sizeof(one), "bad overload resolution");
62*67e74705SXin Li 
63*67e74705SXin Li     one g(int&);
64*67e74705SXin Li     two g(const B&);
65*67e74705SXin Li 
66*67e74705SXin Li     static_assert(sizeof(g({1})) == sizeof(two), "bad overload resolution");
67*67e74705SXin Li 
68*67e74705SXin Li     one h(const int&);
69*67e74705SXin Li     two h(const A&);
70*67e74705SXin Li 
71*67e74705SXin Li     static_assert(sizeof(h({1, 2})) == sizeof(two), "bad overload resolution");
72*67e74705SXin Li   }
73*67e74705SXin Li 
edge_cases()74*67e74705SXin Li   void edge_cases() {
75*67e74705SXin Li     // FIXME: very poor error message
76*67e74705SXin Li     int const &b({0}); // expected-error {{could not bind}}
77*67e74705SXin Li   }
78*67e74705SXin Li 
79*67e74705SXin Li }
80*67e74705SXin Li 
81*67e74705SXin Li namespace PR12182 {
82*67e74705SXin Li   void f(int const(&)[3]);
83*67e74705SXin Li 
g()84*67e74705SXin Li   void g() {
85*67e74705SXin Li       f({1, 2});
86*67e74705SXin Li   }
87*67e74705SXin Li }
88*67e74705SXin Li 
89*67e74705SXin Li namespace PR12660 {
90*67e74705SXin Li   const int &i { 1 };
91*67e74705SXin Li   struct S { S(int); } const &s { 2 };
92*67e74705SXin Li }
93*67e74705SXin Li 
94*67e74705SXin Li namespace b7891773 {
95*67e74705SXin Li   typedef void (*ptr)();
96*67e74705SXin Li   template <class T> void f();
97*67e74705SXin Li   int g(const ptr &);
98*67e74705SXin Li   int k = g({ f<int> });
99*67e74705SXin Li }
100*67e74705SXin Li 
101*67e74705SXin Li namespace inner_init {
102*67e74705SXin Li   struct A { int n; };
103*67e74705SXin Li   struct B { A &&r; };
104*67e74705SXin Li   B b1 { 0 }; // expected-error {{reference to type 'inner_init::A' could not bind to an rvalue of type 'int'}}
105*67e74705SXin Li   B b2 { { 0 } };
106*67e74705SXin Li   B b3 { { { 0 } } }; // expected-warning {{braces around scalar init}}
107*67e74705SXin Li 
108*67e74705SXin Li   struct C { C(int); };   // expected-note 2{{candidate constructor (the implicit}} \
109*67e74705SXin Li                           // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'int'}}
110*67e74705SXin Li   struct D { C &&r; };
111*67e74705SXin Li   D d1 { 0 }; // ok, 0 implicitly converts to C
112*67e74705SXin Li   D d2 { { 0 } }; // ok, { 0 } calls C(0)
113*67e74705SXin Li   D d3 { { { 0 } } }; // ok, { { 0 } } calls C({ 0 }), expected-warning {{braces around scalar init}}
114*67e74705SXin Li   D d4 { { { { 0 } } } }; // expected-error {{no matching constructor for initialization of 'inner_init::C &&'}}
115*67e74705SXin Li 
116*67e74705SXin Li   struct E { explicit E(int); }; // expected-note 2{{here}}
117*67e74705SXin Li   struct F { E &&r; };
118*67e74705SXin Li   F f1 { 0 }; // expected-error {{could not bind to an rvalue of type 'int'}}
119*67e74705SXin Li   F f2 { { 0 } }; // expected-error {{chosen constructor is explicit}}
120*67e74705SXin Li   F f3 { { { 0 } } }; // expected-error {{chosen constructor is explicit}}
121*67e74705SXin Li }
122*67e74705SXin Li 
123*67e74705SXin Li namespace PR20844 {
124*67e74705SXin Li   struct A {};
125*67e74705SXin Li   struct B { operator A&(); } b;
126*67e74705SXin Li   A &a{b}; // expected-error {{excess elements}} expected-note {{in initialization of temporary of type 'PR20844::A'}}
127*67e74705SXin Li }
128*67e74705SXin Li 
129*67e74705SXin Li namespace PR21834 {
130*67e74705SXin Li const int &a = (const int &){0}; // expected-error {{cannot bind to an initializer list}}
131*67e74705SXin Li }
132