1 // RUN: %clang_cc1 -fsyntax-only -verify %s
30 // C++ [dcl.init.ref]p5b1
36 double& rd = d; // rd refers to d
37 const double& rcd = d; // rcd refers to d
39 A& ra = b; // ra refers to A subobject in b
40 const A& rca = b; // rca refers to A subobject in b
45 // C++ [dcl.init.ref]p5b2
47 double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
49 double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
55 // const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
56 const volatile int cvi = 1;
57 const int& r = cvi; // expected-error{{binding value of type 'const volatile int' to reference to type 'const int' drops 'volatile' qualifier}}
60 // C++ [dcl.init.ref]p3
62 int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
66 int& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
67 extern int& not_initialized_okay;
69 class Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}}
70 int& okay; // expected-note{{reference member 'okay' will never be initialized}}
73 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}}
76 A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
79 // C++ [dcl.ref]p1, C++ [dcl.ref]p4
80 void test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
82 void&, // expected-error{{cannot form a reference to 'void'}}
83 int& &) // expected-error{{type name declared as a reference to a reference}}
86 typedef intref& intrefref; // C++ DR 106: reference collapsing
88 typedef intref const intref_c; // expected-warning {{'const' qualifier on reference type 'intref' (aka 'int &') has no effect}}
89 typedef intref_c intref; // ok, same type
91 typedef intref volatile intref; // expected-warning {{'volatile' qualifier on reference type 'intref' (aka 'int &') has no effect}}
92 typedef intref _Atomic intref; // expected-warning {{'_Atomic' qualifier on reference type 'intref' (aka 'int &') has no effect}}
94 void restrict_ref(__restrict intref); // ok
95 void restrict_ref(int &__restrict); // ok
98 template<typename T> int const_param(const T) {}
99 int const_ref_param = const_param<int&>(const_ref_param); // no-warning
113 string &s = getInput(); // expected-error{{lvalue reference}}
117 __attribute((vector_size(16))) typedef int vec4;
118 typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
121 int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
125 int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}}
130 template<typename T> struct X0
133 X0(T& p1) : first(p1) { }
140 X0< const int[1]> c(p1);
145 bool& f(unsigned char& c) { return (bool&)c; }
148 // The following crashed trying to recursively evaluate the LValue.
149 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}}
151 namespace ExplicitRefInit {
152 // This is invalid: we can't copy-initialize an 'A' temporary using an
153 // explicit constructor.
154 struct A { explicit A(int); };
155 const A &a(0); // expected-error {{reference to type 'const ExplicitRefInit::A' could not bind to an rvalue of type 'int'}}