blob: f9164fb1ae65c937ab70a6fb52dc92bbb1c19ee0 [file] [log] [blame]
Sebastian Redl13dc8f92011-11-27 16:50:07 +00001// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
Sebastian Redl1cdb70b2011-12-03 14:54:30 +00003struct one { char c; };
4struct two { char c[2]; };
5
Sebastian Redl13dc8f92011-11-27 16:50:07 +00006namespace 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
Sebastian Redl1cdb70b2011-12-03 14:54:30 +000034 struct B {
35 int i1;
36 };
37
38 void call() {
Richard Smitha41c97a2013-09-20 01:15:31 +000039 one f(const int&);
Sebastian Redl1cdb70b2011-12-03 14:54:30 +000040 f({1});
41
Richard Smitha41c97a2013-09-20 01:15:31 +000042 one g(int&); // expected-note {{passing argument}}
Sebastian Redl1cdb70b2011-12-03 14:54:30 +000043 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
Sebastian Redl3a45c0e2012-02-12 16:37:36 +000074 void edge_cases() {
75 // FIXME: very poor error message
76 int const &b({0}); // expected-error {{could not bind}}
77 }
78
Sebastian Redl13dc8f92011-11-27 16:50:07 +000079}
Sebastian Redlcbf82092012-03-07 16:10:45 +000080
81namespace PR12182 {
82 void f(int const(&)[3]);
83
84 void g() {
85 f({1, 2});
86 }
87}
Richard Smith6e4a0af2012-04-26 03:16:45 +000088
89namespace PR12660 {
90 const int &i { 1 };
91 struct S { S(int); } const &s { 2 };
92}
Richard Smith02d65ee2013-01-15 07:58:29 +000093
94namespace b7891773 {
95 typedef void (*ptr)();
96 template <class T> void f();
97 int g(const ptr &);
98 int k = g({ f<int> });
99}
Richard Smith6242a452013-05-31 02:56:17 +0000100
101namespace inner_init {
102 struct A { int n; };
103 struct B { A &&r; };
104 B b1 { 0 }; // expected-error {{reference to type 'inner_init::A' could not bind to an rvalue of type 'int'}}
105 B b2 { { 0 } };
106 B b3 { { { 0 } } }; // expected-warning {{braces around scalar init}}
107
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700108 struct C { C(int); }; // expected-note 2{{candidate constructor (the implicit}} \
109 // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'int'}}
Richard Smith6242a452013-05-31 02:56:17 +0000110 struct D { C &&r; };
111 D d1 { 0 }; // ok, 0 implicitly converts to C
112 D d2 { { 0 } }; // ok, { 0 } calls C(0)
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700113 D d3 { { { 0 } } }; // ok, { { 0 } } calls C({ 0 }), expected-warning {{braces around scalar init}}
114 D d4 { { { { 0 } } } }; // expected-error {{no matching constructor for initialization of 'inner_init::C &&'}}
Richard Smith6242a452013-05-31 02:56:17 +0000115
116 struct E { explicit E(int); }; // expected-note 2{{here}}
117 struct F { E &&r; };
118 F f1 { 0 }; // expected-error {{could not bind to an rvalue of type 'int'}}
119 F f2 { { 0 } }; // expected-error {{chosen constructor is explicit}}
120 F f3 { { { 0 } } }; // expected-error {{chosen constructor is explicit}}
121}
Stephen Hines176edba2014-12-01 14:53:08 -0800122
123namespace PR20844 {
124 struct A {};
125 struct B { operator A&(); } b;
126 A &a{b}; // expected-error {{excess elements}} expected-note {{in initialization of temporary of type 'PR20844::A'}}
127}