Files
2025-10-25 03:02:53 +03:00

8.4 KiB
Raw Permalink Blame History

[dcl.ref]

9 Declarations [dcl]

9.3 Declarators [dcl.decl]

9.3.4 Meaning of declarators [dcl.meaning]

9.3.4.3 References [dcl.ref]

1

#

In a declarationTD whereD has either of the forms

& attribute-specifier-seqopt D1
&& attribute-specifier-seqopt D1

and the type of the contained declarator-id in the declarationTD1 is “derived-declarator-type-list€, the type of the declarator-id inD is “derived-declarator-type-list reference toT”.

The optional attribute-specifier-seq appertains to the reference type.

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of atypedef-name ([dcl.typedef], [temp.param]) ordecltype-specifier ([dcl.type.decltype]), in which case the cv-qualifiers are ignored.

[Example 1: typedef int& A;const A aref = 3; // error: lvalue reference to non-const initialized with rvalue

The type ofaref is “lvalue reference to int”, not “lvalue reference to const int”.

— end example]

[Note 1:

A reference can be thought of as a name of an object.

— end note]

Forming the type “reference to cv void” is ill-formed.

2

#

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.

3

#

[Example 2:

void f(double& a) { a += 3.14; }// ...double d = 0; f(d); declaresa to be a reference parameter off so the callf(d) will add3.14 tod.

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)=7 will assign7 to the fourth element of the arrayv.

For another example,struct link { link* next;};

link* first;

void h(link*& p) { // p is a reference to pointer p->next = first; first = p; p = 0;}void k() { link* q = new link; h(q);} declaresp to be a reference to a pointer tolink soh(q) will leaveq with the value zero.

See also [dcl.init.ref].

— end example]

4

#

It is unspecified whether or not a reference requires storage ([basic.stc]).

5

#

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 explicitextern specifier ([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].

6

#

Attempting to bind a reference to a function where the converted initializer is a glvalue whose type is not call-compatible ([expr.call]) with the type of the function's definition results in undefined behavior.

Attempting to bind a reference to an object where the converted initializer is a glvalue through which the object is not type-accessible ([basic.lval]) results in undefined behavior.

[Note 2:

The object designated by such a glvalue can be outside its lifetime ([basic.life]).

Because a null pointer value or a pointer past the end of an object does not point to an object, a reference in a well-defined program cannot refer to such things; see [expr.unary.op].

As described in [class.bit], a reference cannot be bound directly to a bit-field.

— end note]

The behavior of an evaluation of a reference ([expr.prim.id], [expr.ref]) that does not happen after ([intro.races]) the initialization of the reference is undefined.

[Example 3: int &f(int&);int &g();extern int &ir3;int *ip = 0;int &ir1 = *ip; // undefined behavior: null pointerint &ir2 = f(ir3); // undefined behavior: ir3 not yet initializedint &ir3 = g();int &ir4 = f(ir4); // undefined behavior: ir4 used in its own initializerchar x alignas(int);int &ir5 = *reinterpret_cast<int *>(&x); // undefined behavior: initializer refers to char object — end example]

7

#

If a typedef-name ([dcl.typedef], [temp.param]) or a decltype-specifier ([dcl.type.decltype]) 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 3:

This rule is known as reference collapsing.

— end note]

[Example 4: int i;typedef int& LRI;typedef int&& RRI;

LRI& r1 = i; // r1 has the type int&const LRI& r2 = i; // r2 has the type int&const LRI&& r3 = i; // r3 has the type int& RRI& r4 = i; // r4 has the type int& RRI&& r5 = 5; // r5 has the type int&&decltype(r2)& r6 = i; // r6 has the type int&decltype(r2)&& r7 = i; // r7 has the type int& — end example]

8

#

[Note 4:

Forming a reference to function type is ill-formed if the function type has cv-qualifiers or a ref-qualifier; see [dcl.fct].

— end note]