1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
3 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
32 // C++ [dcl.init.ref]p5b1
38 double& rd
= d
; // rd refers to d
39 const double& rcd
= d
; // rcd refers to d
41 A
& ra
= b
; // ra refers to A subobject in b
42 const A
& rca
= b
; // rca refers to A subobject in b
47 // C++ [dcl.init.ref]p5b2
49 double& rd2
= 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
51 double& rd3
= i
; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
57 // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
58 const volatile int cvi
= 1;
59 const int& r
= cvi
; // expected-error{{binding reference of type 'const int' to value of type 'const volatile int' drops 'volatile' qualifier}}
61 #if __cplusplus >= 201103L
62 const int& r2
{cvi
}; // expected-error{{binding reference of type 'const int' to value of type 'const volatile int' drops 'volatile' qualifier}}
65 int& r3
{a
}; // expected-error{{binding reference of type 'int' to value of type 'const int' drops 'const' qualifier}}
67 const int&& r4
{a
}; // expected-error{{rvalue reference to type 'const int' cannot bind to lvalue of type 'const int'}}
71 int &ptr1
= {func
}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
72 int &&ptr2
{func
}; // expected-error{{address of overloaded function 'func' does not match required type 'int'}}
73 // expected-note@-4{{candidate function}}
74 // expected-note@-4{{candidate function}}
75 // expected-note@-6{{candidate function}}
76 // expected-note@-6{{candidate function}}
80 // C++ [dcl.init.ref]p3
82 int& yo
; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
86 int& not_initialized_error
; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
87 extern int& not_initialized_okay
;
89 class Test6
{ // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}}
90 int& okay
; // expected-note{{reference member 'okay' will never be initialized}}
93 struct C
: B
, A
{ }; // expected-warning {{direct base 'A' is inaccessible due to ambiguity:\n struct C -> B -> A\nstruct C -> A}}
96 A
& a1
= c
; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
99 // C++ [dcl.ref]p1, C++ [dcl.ref]p4
100 void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
102 void&, // expected-error{{cannot form a reference to 'void'}}
103 int& &) // expected-error{{type name declared as a reference to a reference}}
106 typedef intref
& intrefref
; // C++ DR 106: reference collapsing
108 typedef intref
const intref_c
; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}}
109 typedef intref_c intref
; // ok, same type
111 typedef intref
volatile intref
; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}}
112 typedef intref _Atomic intref
; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}}
114 void restrict_ref(__restrict intref
); // ok
115 void restrict_ref(int &__restrict
); // ok
118 namespace var_template
{
119 #if __cplusplus >= 201402L
121 template <typename
> int &ref
= i
; // ok
122 template <> int &ref
<float>; // expected-error {{declaration of reference variable 'ref<float>' requires an initializer}}
124 } // namespace var_template
126 template<typename T
> int const_param(const T
) {}
127 int const_ref_param
= const_param
<int&>(const_ref_param
); // no-warning
141 string
&s
= getInput(); // expected-error{{lvalue reference}}
145 __attribute((vector_size(16))) typedef int vec4
;
146 typedef __attribute__(( ext_vector_type(4) )) int ext_vec4
;
149 int &a
= v
[0]; // expected-error{{non-const reference cannot bind to vector element}}
153 int &c
= ev
.x
; // expected-error{{non-const reference cannot bind to vector element}}
158 template<typename T
> struct X0
161 X0(T
& p1
) : first(p1
) { }
168 X0
< const int[1]> c(p1
);
173 bool& f(unsigned char& c
) { return (bool&)c
; }
176 // The following crashed trying to recursively evaluate the LValue.
177 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}}
179 namespace ExplicitRefInit
{
180 // This is invalid: we can't copy-initialize an 'A' temporary using an
181 // explicit constructor.
182 struct A
{ explicit A(int); };
183 const A
&a(0); // expected-error {{reference to type 'const A' could not bind to an rvalue of type 'int'}}
186 namespace RefCollapseTypePrinting
{
187 template<typename T
> void add_lref() {
188 using X
= int(T
); // expected-note 4{{previous}}
189 using X
= const volatile T
&;
190 // expected-error@-1 {{'int &' vs 'int (int &)'}}
191 // expected-error@-2 {{'int &' vs 'int (int &&)'}}
192 // expected-error@-3 {{'const int &' vs 'int (const int &)'}}
193 // expected-error@-4 {{'const int &' vs 'int (const int &&)'}}
195 template void add_lref
<int&>(); // expected-note {{instantiation of}}
196 template void add_lref
<int&&>(); // expected-note {{instantiation of}}
197 template void add_lref
<const int&>(); // expected-note {{instantiation of}}
198 template void add_lref
<const int&&>(); // expected-note {{instantiation of}}
200 template<typename T
> void add_rref() {
201 using X
= int(T
); // expected-note 4{{previous}}
202 using X
= const volatile T
&&;
203 // expected-error@-1 {{'int &' vs 'int (int &)'}}
204 // expected-error@-2 {{'int &&' vs 'int (int &&)'}}
205 // expected-error@-3 {{'const int &' vs 'int (const int &)'}}
206 // expected-error@-4 {{'const int &&' vs 'int (const int &&)'}}
208 template void add_rref
<int&>(); // expected-note {{instantiation of}}
209 template void add_rref
<int&&>(); // expected-note {{instantiation of}}
210 template void add_rref
<const int&>(); // expected-note {{instantiation of}}
211 template void add_rref
<const int&&>(); // expected-note {{instantiation of}}
215 struct a
{ template<class b
> a(const b
* const&); };