[dcl.ref] (original) (raw)
9 Declarations [dcl.dcl]
9.3 Declarators [dcl.decl]
9.3.3 Meaning of declarators [dcl.meaning]
9.3.3.2 References [dcl.ref]
In a declarationT DwhereDhas either of the forms
& attribute-specifier-seq D1 && attribute-specifier-seq D1
and the type of the identifier in the declarationT D1is “derived-declarator-type-list T”, then the type of the identifier ofDis “derived-declarator-type-list reference toT”.
[ Example
:
typedef int& A; const A aref = 3;
The type ofarefis “lvalue reference to int”, not “lvalue reference to const int”.
— end example
]
[ Note
:
A reference can be thought of as a name of an object.
— end note
]
A declarator that specifies the type “reference to cv void” is ill-formed.
A reference type that is declared using & is called anlvalue reference, and a reference type that is declared using && is called anrvalue reference.
Lvalue references and rvalue references are distinct types.
Except where explicitly noted, they are semantically equivalent and commonly referred to as references.
[ Example
:
void f(double& a) { a += 3.14; }
double d = 0; f(d);
declaresato be a reference parameter offso the callf(d)will add3.14tod.
int v[20];
int& g(int i) { return v[i]; }
g(3) = 7;
declares the functiong()to return a reference to an integer sog(3)=7will assign7to the fourth element of the arrayv.
For another example,
struct link { link* next; };
link* first;
void h(link*& p) {
p->next = first;
first = p;
p = 0;
}
void k() { link* q = new link; h(q); }
declarespto be a reference to a pointer tolinksoh(q)will leaveqwith the value zero.
— end example
]
It is unspecified whether or not a reference requires storage ([basic.stc]).
There shall be no references to references, no arrays of references, and no pointers to references.
The declaration of a reference shall contain aninitializer ([dcl.init.ref]) except when the declaration contains an explicitexternspecifier ([dcl.stc]), is a class member ([class.mem]) declaration within a class definition, or is the declaration of a parameter or a return type ([dcl.fct]); see [basic.def].
A reference shall be initialized to refer to a valid object or function.
[ Note
:
In particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by indirection through a null pointer, which causes undefined behavior.
As described in [class.bit], a reference cannot be bound directly to a bit-field.
— end note
]
If a typedef-name ([dcl.typedef], [temp.param]) or a decltype-specifier ([dcl.type.simple]) denotes a type TR that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T”, while an attempt to create the type “rvalue reference to cv TR” creates the type TR.
[ Note
:
This rule is known as reference collapsing.
— end note
]
[ Example
:
int i; typedef int& LRI; typedef int&& RRI;
LRI& r1 = i;
const LRI& r2 = i;
const LRI&& r3 = i;
RRI& r4 = i;
RRI&& r5 = 5;
decltype(r2)& r6 = i;
decltype(r2)&& r7 = i;
— end example
]