1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
31 // C++ [dcl.init.ref]p5b1
37 double& rd = d; // rd refers to d
38 const double& rcd = d; // rcd refers to d
40 A& ra = b; // ra refers to A subobject in b
41 const A& rca = b; // rca refers to A subobject in b
46 // C++ [dcl.init.ref]p5b2
48 double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
50 double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
56 // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
57 const volatile int cvi = 1;
58 const int& r = cvi; // expected-error{{binding value of type 'const volatile int' to reference to type 'const int' drops 'volatile' qualifier}}
60 #if __cplusplus >= 201103L
61 const int& r2{cvi}; // expected-error{{binding value of type 'const volatile int' to reference to type 'const int' drops 'volatile' qualifier}}
64 int& r3{a}; // expected-error{{binding value of type 'const int' to reference to type 'int' drops 'const'}}
66 const int&& r4{a}; // expected-error{{rvalue reference to type 'const int' cannot bind to lvalue of type 'const int'}}
70 int &ptr1 = {func}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
71 int &&ptr2{func}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
72 // expected-note@-4{{candidate function}}
73 // expected-note@-4{{candidate function}}
74 // expected-note@-6{{candidate function}}
75 // expected-note@-6{{candidate function}}
79 // C++ [dcl.init.ref]p3
81 int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
85 int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
86 extern int& not_initialized_okay;
88 class Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}}
89 int& okay; // expected-note{{reference member 'okay' will never be initialized}}
92 struct C : B, A { }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> struct B -> struct A\nstruct C -> struct A}}
95 A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
98 // C++ [dcl.ref]p1, C++ [dcl.ref]p4
99 void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
101 void&, // expected-error{{cannot form a reference to 'void'}}
102 int& &) // expected-error{{type name declared as a reference to a reference}}
105 typedef intref& intrefref; // C++ DR 106: reference collapsing
107 typedef intref const intref_c; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}}
108 typedef intref_c intref; // ok, same type
110 typedef intref volatile intref; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}}
111 typedef intref _Atomic intref; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}}
113 void restrict_ref(__restrict intref); // ok
114 void restrict_ref(int &__restrict); // ok
117 template<typename T> int const_param(const T) {}
118 int const_ref_param = const_param<int&>(const_ref_param); // no-warning
132 string &s = getInput(); // expected-error{{lvalue reference}}
136 __attribute((vector_size(16))) typedef int vec4;
137 typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
140 int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
144 int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}}
149 template<typename T> struct X0
152 X0(T& p1) : first(p1) { }
159 X0< const int[1]> c(p1);
164 bool& f(unsigned char& c) { return (bool&)c; }
167 // The following crashed trying to recursively evaluate the LValue.
168 const int &do_not_crash = do_not_crash; // expected-warning{{reference 'do_not_crash' is not yet bound to a value when used within its own initialization}}
170 namespace ExplicitRefInit {
171 // This is invalid: we can't copy-initialize an 'A' temporary using an
172 // explicit constructor.
173 struct A { explicit A(int); };
174 const A &a(0); // expected-error {{reference to type 'const ExplicitRefInit::A' could not bind to an rvalue of type 'int'}}
177 namespace RefCollapseTypePrinting {
178 template<typename T> void add_lref() {
179 using X = int(T); // expected-note 4{{previous}}
180 using X = const volatile T&;
181 // expected-error@-1 {{'int &' vs 'int (int &)'}}
182 // expected-error@-2 {{'int &' vs 'int (int &&)'}}
183 // expected-error@-3 {{'const int &' vs 'int (const int &)'}}
184 // expected-error@-4 {{'const int &' vs 'int (const int &&)'}}
186 template void add_lref<int&>(); // expected-note {{instantiation of}}
187 template void add_lref<int&&>(); // expected-note {{instantiation of}}
188 template void add_lref<const int&>(); // expected-note {{instantiation of}}
189 template void add_lref<const int&&>(); // expected-note {{instantiation of}}
191 template<typename T> void add_rref() {
192 using X = int(T); // expected-note 4{{previous}}
193 using X = const volatile T&&;
194 // expected-error@-1 {{'int &' vs 'int (int &)'}}
195 // expected-error@-2 {{'int &&' vs 'int (int &&)'}}
196 // expected-error@-3 {{'const int &' vs 'int (const int &)'}}
197 // expected-error@-4 {{'const int &&' vs 'int (const int &&)'}}
199 template void add_rref<int&>(); // expected-note {{instantiation of}}
200 template void add_rref<int&&>(); // expected-note {{instantiation of}}
201 template void add_rref<const int&>(); // expected-note {{instantiation of}}
202 template void add_rref<const int&&>(); // expected-note {{instantiation of}}