[dcl.init.ref] (original) (raw)

A variable whose declared type is “reference to type T” ([dcl.ref]) shall be initialized.

[ Example

:

int g(int) noexcept; void f() { int i; int& r = i;
r = 1;
int* p = &r;
int& rr = r;
int (&rg)(int) = g;
rg(i);
int a[3]; int (&ra)[3] = a;
ra[1] = i;
}

end example

]

A reference cannot be changed to refer to another object after initialization.

[ Note

: Assignment to a reference assigns to the object referred to by the reference ([expr.ass]). — end note

]

Argument passing ([expr.call])and function value return ([stmt.return]) are initializations.

The initializer can be omitted for a reference only in a parameter declaration ([dcl.fct]), in the declaration of a function return type, in the declaration of a class member within its class definition ([class.mem]), and where theexternspecifier is explicitly used.

[ Example

:

int& r1;
extern int& r2;

end example

]

Given types “cv1 T1” and “cv2 T2”, “cv1 T1” is reference-related to “cv2 T2” ifT1 is similar ([conv.qual]) to T2, orT1 is a base class of T2.

“cv1 T1” is reference-compatiblewith “cv2 T2” if a prvalue of type “pointer to cv2 T2” can be converted to the type “pointer to cv1 T1” via a standard conversion sequence ([conv]).

In all cases where the reference-compatible relationship of two types is used to establish the validity of a reference binding and the standard conversion sequence would be ill-formed, a program that necessitates such a binding is ill-formed.

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:

double d2 = 1.0;
double&& rrd2 = d2;
struct X { operator int&(); };
int&& rri2 = X();
int i3 = 2;
double&& rrd3 = i3;
end example
]

In all cases except the last (i.e., implicitly converting the initializer expression to the referenced type), the reference is said to bind directly to the initializer expression.