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