[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / cxx0x-initializer-references.cpp
blobe2510bc039e5de0b30a9c5c6eb8ea57a8e20e579
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
3 struct one { char c; };
4 struct two { char c[2]; };
6 namespace reference {
7 struct A {
8 int i1, i2;
9 };
11 void single_init() {
12 const int &cri1a = {1};
13 const int &cri1b{1};
15 int i = 1;
16 int &ri1a = {i};
17 int &ri1b{i};
19 int &ri2 = {1}; // expected-error {{cannot bind to an initializer list temporary}}
21 A a{1, 2};
22 A &ra1a = {a};
23 A &ra1b{a};
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}}
30 const int (&arrayRef)[] = {1, 2, 3};
31 static_assert(sizeof(arrayRef) == 3 * sizeof(int), "bad array size");
34 struct B {
35 int i1;
38 void call() {
39 one f(const int&);
40 f({1});
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});
47 void h(const B&);
48 h({1});
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});
56 void overloading() {
57 one f(const int&);
58 two f(const B&);
60 // First is identity conversion, second is user-defined conversion.
61 static_assert(sizeof(f({1})) == sizeof(one), "bad overload resolution");
63 one g(int&);
64 two g(const B&);
66 static_assert(sizeof(g({1})) == sizeof(two), "bad overload resolution");
68 one h(const int&);
69 two h(const A&);
71 static_assert(sizeof(h({1, 2})) == sizeof(two), "bad overload resolution");
74 struct X {};
76 void edge_cases() {
77 int const &b({0}); // expected-error {{cannot initialize reference type 'const int &' with a parenthesized initializer list}}
78 const int (&arr)[3] ({1, 2, 3}); // expected-error {{cannot initialize reference type 'const int (&)[3]' with a parenthesized initializer list}}
79 const X &x({}); // expected-error {{cannot initialize reference type 'const X &' with a parenthesized initializer list}}
82 template<typename T> void dependent_edge_cases() {
83 T b({}); // expected-error-re 3{{cannot initialize reference type {{.*}} with a parenthesized init}}
84 T({}); // expected-error-re 3{{cannot initialize reference type {{.*}} with a parenthesized init}}
86 template void dependent_edge_cases<X>(); // ok
87 template void dependent_edge_cases<const int&>(); // expected-note {{instantiation of}}
88 template void dependent_edge_cases<const int(&)[1]>(); // expected-note {{instantiation of}}
89 template void dependent_edge_cases<const X&>(); // expected-note {{instantiation of}}
92 namespace PR12182 {
93 void f(int const(&)[3]);
95 void g() {
96 f({1, 2});
100 namespace PR12660 {
101 const int &i { 1 };
102 struct S { S(int); } const &s { 2 };
105 namespace b7891773 {
106 typedef void (*ptr)();
107 template <class T> void f();
108 int g(const ptr &);
109 int k = g({ f<int> });
112 namespace inner_init {
113 struct A { int n; };
114 struct B { A &&r; };
115 B b1 { 0 }; // expected-error {{reference to type 'A' could not bind to an rvalue of type 'int'}}
116 B b2 { { 0 } };
117 B b3 { { { 0 } } }; // expected-warning {{braces around scalar init}}
119 struct C { C(int); }; // expected-note 2{{candidate constructor (the implicit}} \
120 // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'int'}}
121 struct D { C &&r; };
122 D d1 { 0 }; // ok, 0 implicitly converts to C
123 D d2 { { 0 } }; // ok, { 0 } calls C(0)
124 D d3 { { { 0 } } }; // ok, { { 0 } } calls C({ 0 }), expected-warning {{braces around scalar init}}
125 D d4 { { { { 0 } } } }; // expected-error {{no matching constructor for initialization of 'C &&'}}
127 struct E { explicit E(int); }; // expected-note 2{{here}}
128 struct F { E &&r; };
129 F f1 { 0 }; // expected-error {{could not bind to an rvalue of type 'int'}}
130 F f2 { { 0 } }; // expected-error {{chosen constructor is explicit}}
131 F f3 { { { 0 } } }; // expected-error {{chosen constructor is explicit}}
134 namespace PR20844 {
135 struct A {};
136 struct B { operator A&(); } b;
137 A &a{b}; // expected-error {{excess elements}} expected-note {{in initialization of temporary of type 'A'}}
140 namespace PR21834 {
141 const int &a = (const int &){0}; // expected-error {{cannot bind to an initializer list}}