146 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s2 3// C++ [dcl.ref]p5:4// There shall be no references to references, no arrays of5// references, and no pointers to references.6 7// The crazy formatting in here is to enforce the exact report locations.8 9typedef int &intref;10typedef intref &intrefref;11 12template <class T> class RefMem { // expected-warning{{class 'RefMem<int &>' does not declare any constructor to initialize its non-modifiable members}}13 T14 &15 member; // expected-note{{reference member 'member' will never be initialized}}16};17 18struct RefRef {19 int20 &21 & // expected-error {{declared as a reference to a reference}}22 refref0;23 24 intref25 &26 refref1; // collapses27 28 intrefref29 &30 refref2; // collapses31 32 RefMem33 <34 int35 &36 >37 refref3; // collapses expected-note{{in instantiation of template class 'RefMem<int &>' requested here}}38};39 40 41template <class T> class PtrMem {42 T43 * // expected-error {{declared as a pointer to a reference}}44 member;45};46 47struct RefPtr {48 typedef49 int50 &51 * // expected-error {{declared as a pointer to a reference}}52 intrefptr;53 54 typedef55 intref56 * // expected-error {{declared as a pointer to a reference}}57 intrefptr2;58 59 int60 &61 * // expected-error {{declared as a pointer to a reference}}62 refptr0;63 64 intref65 * // expected-error {{declared as a pointer to a reference}}66 refptr1;67 68 PtrMem69 <70 int71 &72 >73 refptr2; // expected-note {{in instantiation}}74};75 76template <class T> class ArrMem {77 T78 member79 [ // expected-error {{declared as array of references}}80 1081 ];82};83template <class T, unsigned N> class DepArrMem {84 T85 member86 [ // expected-error {{declared as array of references}}87 N88 ];89};90 91struct RefArr {92 typedef 93 int94 &95 intrefarr96 [ // expected-error {{declared as array of references}}97 298 ];99 100 typedef101 intref102 intrefarr103 [ // expected-error {{declared as array of references}}104 2105 ];106 107 int108 &109 refarr0110 [ // expected-error {{declared as array of references}}111 2112 ];113 intref114 refarr1115 [ // expected-error {{declared as array of references}}116 2117 ];118 ArrMem119 <120 int121 &122 >123 refarr2; // expected-note {{in instantiation}}124 DepArrMem125 <126 int127 &,128 10129 >130 refarr3; // expected-note {{in instantiation}}131};132 133 134// The declaration of a reference shall contain an initializer135// (8.5.3) except when the declaration contains an explicit extern136// specifier (7.1.1), is a class member (9.2) declaration within a137// class definition, or is the declaration of a parameter or a138// return type (8.3.5); see 3.1. A reference shall be initialized to139// refer to a valid object or function. [ Note: in particular, a140// null reference cannot exist in a well-defined program, because141// the only way to create such a reference would be to bind it to142// the "object" obtained by dereferencing a null pointer, which143// causes undefined behavior. As described in 9.6, a reference144// cannot be bound directly to a bit-field.145 146