cpp/language/reference initialization

Binds a reference to an object.

Explanation
A reference to can be initialized with an object of type, a function of type , or an object implicitly convertible to. Once initialized, a reference cannot be reseated (changed) to refer to another object.

References are initialized in the following situations: @1@ When a named variable is declared with an initializer @2@ When a named variable is declared with an initializer @3@ In a function call expression, when the function parameter has reference type @4@ In the statement, when the function returns a reference type @5@ When a of reference type is initialized using a

The effects of reference initialization are:


 * If the initializer is a braced-init-list ( {{ttb|{}}  ... } ), rules of  are followed.


 * Otherwise, if the reference is an lvalue reference:


 * If is an lvalue expression, and its type is  or derived from, and is equally or less cv-qualified, then the reference is bound to the object identified by the lvalue or to its base class subobject.


 * Otherwise, if the type of is not same or derived from, and  has conversion function to an lvalue whose type is either  or derived from , equally or less cv-qualified,  then the reference is bound to the object identified by the lvalue returned by the conversion function (or to its base class subobject).


 * Otherwise, if the reference is lvalue reference to a non-volatile const-qualified type :


 * If is a non-bit-field rvalue or a function lvalue, and its type is either  or derived from, equally or less cv-qualified, then the reference is bound to the value of the initializer expression or to its base subobject.


 * Otherwise, if the type of is not same or derived from, and  has conversion function to an rvalue or a function lvalue whose type is either  or derived from , equally or less cv-qualified,  then the reference is bound to the result of the conversion function or to its base class subobject.


 * Otherwise, is implicitly converted to . The reference is bound to the result of the conversion. If the  (or, if the conversion is done by user-defined conversion, the result of the conversion function) is of type  or derived from, it must be equally or less cv-qualified than.

Lifetime of a temporary
Whenever a reference is bound to a temporary object or to a subobject thereof, the lifetime of the temporary object is extended to match the lifetime of the reference (check ), where the temporary object or its subobject is denoted by one of following expression:


 * a parenthesized expression, where is one of these expressions,
 * a of form  or, where  is an array and is one of these expressions,
 * a of form, where  is one of these expressions and  designates a non-static data member of object type,
 * a of form, where  is one of these expressions and  is a pointer to data member,
 * a, , , or conversion without a user-defined conversion that converts one of these expressions to the glvalue refers to the object designated by the operand, or to its complete object or a subobject thereof (an  expression is interpreted as a sequence of these casts),
 * a of form  that is a glvalue, where  or  is one of these expressions, or
 * a of form  that is a glvalue, where  is one of these expressions.

There are following exceptions to this lifetime rule:
 * a temporary bound to a return value of a function in a statement is not extended: it is destroyed immediately at the end of the return expression. Such  statement always returns a dangling reference.
 * a temporary bound to a reference parameter in a function call exists until the end of the full expression containing that function call: if the function returns a reference, which outlives the full expression, it becomes a dangling reference.

In general, the lifetime of a temporary cannot be further extended by "passing it on": a second reference, initialized from the reference variable or data member to which the temporary was bound, does not affect its lifetime.