2011-11-28 00:50:07 +08:00
|
|
|
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
|
|
|
|
|
2011-12-03 22:54:30 +08:00
|
|
|
struct one { char c; };
|
|
|
|
struct two { char c[2]; };
|
|
|
|
|
2011-11-28 00:50:07 +08:00
|
|
|
namespace reference {
|
|
|
|
struct A {
|
|
|
|
int i1, i2;
|
|
|
|
};
|
|
|
|
|
|
|
|
void single_init() {
|
|
|
|
const int &cri1a = {1};
|
|
|
|
const int &cri1b{1};
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
int &ri1a = {i};
|
|
|
|
int &ri1b{i};
|
|
|
|
|
|
|
|
int &ri2 = {1}; // expected-error {{cannot bind to an initializer list temporary}}
|
|
|
|
|
|
|
|
A a{1, 2};
|
|
|
|
A &ra1a = {a};
|
|
|
|
A &ra1b{a};
|
|
|
|
}
|
|
|
|
|
|
|
|
void reference_to_aggregate() {
|
|
|
|
const A &ra1{1, 2};
|
|
|
|
A &ra2{1, 2}; // expected-error {{cannot bind to an initializer list temporary}}
|
|
|
|
|
|
|
|
const int (&arrayRef)[] = {1, 2, 3};
|
|
|
|
static_assert(sizeof(arrayRef) == 3 * sizeof(int), "bad array size");
|
|
|
|
}
|
|
|
|
|
2011-12-03 22:54:30 +08:00
|
|
|
struct B {
|
|
|
|
int i1;
|
|
|
|
};
|
|
|
|
|
|
|
|
void call() {
|
2013-09-20 09:15:31 +08:00
|
|
|
one f(const int&);
|
2011-12-03 22:54:30 +08:00
|
|
|
f({1});
|
|
|
|
|
2013-09-20 09:15:31 +08:00
|
|
|
one g(int&); // expected-note {{passing argument}}
|
2011-12-03 22:54:30 +08:00
|
|
|
g({1}); // expected-error {{cannot bind to an initializer list temporary}}
|
|
|
|
int i = 0;
|
|
|
|
g({i});
|
|
|
|
|
|
|
|
void h(const B&);
|
|
|
|
h({1});
|
|
|
|
|
|
|
|
void a(B&); // expected-note {{passing argument}}
|
|
|
|
a({1}); // expected-error {{cannot bind to an initializer list temporary}}
|
|
|
|
B b{1};
|
|
|
|
a({b});
|
|
|
|
}
|
|
|
|
|
|
|
|
void overloading() {
|
|
|
|
one f(const int&);
|
|
|
|
two f(const B&);
|
|
|
|
|
|
|
|
// First is identity conversion, second is user-defined conversion.
|
|
|
|
static_assert(sizeof(f({1})) == sizeof(one), "bad overload resolution");
|
|
|
|
|
|
|
|
one g(int&);
|
|
|
|
two g(const B&);
|
|
|
|
|
|
|
|
static_assert(sizeof(g({1})) == sizeof(two), "bad overload resolution");
|
|
|
|
|
|
|
|
one h(const int&);
|
|
|
|
two h(const A&);
|
|
|
|
|
|
|
|
static_assert(sizeof(h({1, 2})) == sizeof(two), "bad overload resolution");
|
|
|
|
}
|
|
|
|
|
2017-03-24 09:14:25 +08:00
|
|
|
struct X {};
|
|
|
|
|
2012-02-13 00:37:36 +08:00
|
|
|
void edge_cases() {
|
2017-03-24 09:14:25 +08:00
|
|
|
int const &b({0}); // expected-error {{cannot initialize reference type 'const int &' with a parenthesized initializer list}}
|
|
|
|
const int (&arr)[3] ({1, 2, 3}); // expected-error {{cannot initialize reference type 'const int (&)[3]' with a parenthesized initializer list}}
|
|
|
|
const X &x({}); // expected-error {{cannot initialize reference type 'const reference::X &' with a parenthesized initializer list}}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> void dependent_edge_cases() {
|
|
|
|
T b({}); // expected-error-re 3{{cannot initialize reference type {{.*}} with a parenthesized init}}
|
|
|
|
T({}); // expected-error-re 3{{cannot initialize reference type {{.*}} with a parenthesized init}}
|
2012-02-13 00:37:36 +08:00
|
|
|
}
|
2017-03-24 09:14:25 +08:00
|
|
|
template void dependent_edge_cases<X>(); // ok
|
|
|
|
template void dependent_edge_cases<const int&>(); // expected-note {{instantiation of}}
|
|
|
|
template void dependent_edge_cases<const int(&)[1]>(); // expected-note {{instantiation of}}
|
|
|
|
template void dependent_edge_cases<const X&>(); // expected-note {{instantiation of}}
|
2011-11-28 00:50:07 +08:00
|
|
|
}
|
2012-03-08 00:10:45 +08:00
|
|
|
|
|
|
|
namespace PR12182 {
|
|
|
|
void f(int const(&)[3]);
|
|
|
|
|
|
|
|
void g() {
|
|
|
|
f({1, 2});
|
|
|
|
}
|
|
|
|
}
|
2012-04-26 11:16:45 +08:00
|
|
|
|
|
|
|
namespace PR12660 {
|
|
|
|
const int &i { 1 };
|
|
|
|
struct S { S(int); } const &s { 2 };
|
|
|
|
}
|
2013-01-15 15:58:29 +08:00
|
|
|
|
|
|
|
namespace b7891773 {
|
|
|
|
typedef void (*ptr)();
|
|
|
|
template <class T> void f();
|
|
|
|
int g(const ptr &);
|
|
|
|
int k = g({ f<int> });
|
|
|
|
}
|
2013-05-31 10:56:17 +08:00
|
|
|
|
|
|
|
namespace inner_init {
|
|
|
|
struct A { int n; };
|
|
|
|
struct B { A &&r; };
|
|
|
|
B b1 { 0 }; // expected-error {{reference to type 'inner_init::A' could not bind to an rvalue of type 'int'}}
|
|
|
|
B b2 { { 0 } };
|
|
|
|
B b3 { { { 0 } } }; // expected-warning {{braces around scalar init}}
|
|
|
|
|
2015-01-28 02:47:05 +08:00
|
|
|
struct C { C(int); }; // expected-note 2{{candidate constructor (the implicit}} \
|
|
|
|
// expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'int'}}
|
2013-05-31 10:56:17 +08:00
|
|
|
struct D { C &&r; };
|
|
|
|
D d1 { 0 }; // ok, 0 implicitly converts to C
|
|
|
|
D d2 { { 0 } }; // ok, { 0 } calls C(0)
|
2015-02-12 09:50:05 +08:00
|
|
|
D d3 { { { 0 } } }; // ok, { { 0 } } calls C({ 0 }), expected-warning {{braces around scalar init}}
|
2015-01-28 02:47:05 +08:00
|
|
|
D d4 { { { { 0 } } } }; // expected-error {{no matching constructor for initialization of 'inner_init::C &&'}}
|
2013-05-31 10:56:17 +08:00
|
|
|
|
|
|
|
struct E { explicit E(int); }; // expected-note 2{{here}}
|
|
|
|
struct F { E &&r; };
|
|
|
|
F f1 { 0 }; // expected-error {{could not bind to an rvalue of type 'int'}}
|
|
|
|
F f2 { { 0 } }; // expected-error {{chosen constructor is explicit}}
|
|
|
|
F f3 { { { 0 } } }; // expected-error {{chosen constructor is explicit}}
|
|
|
|
}
|
2014-09-05 06:13:39 +08:00
|
|
|
|
|
|
|
namespace PR20844 {
|
|
|
|
struct A {};
|
|
|
|
struct B { operator A&(); } b;
|
|
|
|
A &a{b}; // expected-error {{excess elements}} expected-note {{in initialization of temporary of type 'PR20844::A'}}
|
|
|
|
}
|
2015-04-26 15:35:03 +08:00
|
|
|
|
|
|
|
namespace PR21834 {
|
|
|
|
const int &a = (const int &){0}; // expected-error {{cannot bind to an initializer list}}
|
|
|
|
}
|