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