Reference initialization
Binds a reference to an object.
Contents |
[edit] Syntax
T T
T T
|
(1) | ||||||||
T T
T T
|
(2) | (since C++11) | |||||||
given R fn ( T & arg ); or R fn ( T && arg ); (since C++11)
fn fn
|
(3) | ||||||||
inside T & fn () or T && fn () (since C++11)
|
(4) | ||||||||
given T & ref ; or T && ref ; (since C++11) inside the definition of Class
Class |
(5) | ||||||||
[edit] Explanation
A reference to T
can be initialized with an object of type T
, a function of type T
, or an object implicitly convertible to T
. Once initialized, a reference cannot be reseated (changed) to refer to another object.
References are initialized in the following situations:
The effects of reference initialization are:
- If the initializer is a braced-init-list (
{
arg1,
arg2,
...}
or{.
des1=
arg1, .
des2{
arg2}
...}
(since C++20)), rules of list initialization are followed.
- Otherwise, if the reference is an lvalue reference:
- If target is an lvalue expression, and its type is
T
or derived fromT
, 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.
- If target is an lvalue expression, and its type is
double d = 2.0; double& rd = d; // rd refers to d const double& rcd = d; // rcd refers to d struct A {}; struct B : A {} b; A& ra = b; // ra refers to A subobject in b const A& rca = b; // rca refers to A subobject in b
- Otherwise, if the type of target is not same or derived from
T
, and target has conversion function to an lvalue whose type is eitherT
or derived fromT
, 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 type of target is not same or derived from
struct A {}; struct B : A { operator int&(); }; int& ir = B(); // ir refers to the result of B::operator int&
- Otherwise, if the reference is lvalue reference to a non-volatile const-qualified type or rvalue reference (since C++11):
- If target is a non-bit-field rvalue or a function lvalue, and its type is either
T
or derived fromT
, equally or less cv-qualified, then the reference is bound to the value of the initializer expression or to its base subobject (after materializing a temporary if necessary) (since C++17).
- If target is a non-bit-field rvalue or a function lvalue, and its type is either
struct A {}; struct B : A {}; extern B f(); const A& rca2 = f(); // bound to the A subobject of the B rvalue. A&& rra = f(); // same as above int i2 = 42; int&& rri = static_cast<int&&>(i2); // bound directly to i2
- Otherwise, if the type of target is not same or derived from
T
, and target has conversion function to an rvalue or a function lvalue whose type is eitherT
or derived fromT
, equally or less cv-qualified, then the reference is bound to the result of the conversion function or to its base class subobject (after materializing a temporary if necessary) (since C++17).
- Otherwise, if the type of target is not same or derived from
struct A {}; struct B : A {}; struct X { operator B(); } x; const A& r = x; // bound to the A subobject of the result of the conversion B&& rrb = x; // bound directly to the result of the conversion
- Otherwise, target is implicitly converted to
T
. The reference is bound to the result of the conversion (after materializing a temporary, and the cv-qualification ofT
is preserved even if it is a scalar type) (since C++17). If the target (or, if the conversion is done by user-defined conversion, the result of the conversion function) is of typeT
or derived fromT
, it must be equally or less cv-qualified thanT
, and, if the reference is an rvalue reference, must not be an lvalue (since C++11).
- Otherwise, target is implicitly converted to
const std::string& rs = "abc"; // rs refers to temporary copy-initialized from char array const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0 int i3 = 2; double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
[edit] 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 temporary object lifetime exceptions), where the temporary object or its subobject is denoted by one of following expression:
|
(until C++17) |
(since C++17) |
- a parenthesized expression (e), where e is one of these expressions,
- a built-in subscript expression of form a[n] or n[a], where a is an array and is one of these expressions,
- a class member access expression of form e.m, where e is one of these expressions and m designates a non-static data member of object type,
- a pointer-to-member operation of form e.*mp, where e is one of these expressions and mp is a pointer to data member,
- a
const_cast
,static_cast
,dynamic_cast
, orreinterpret_cast
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 explicit cast expression is interpreted as a sequence of these casts), - a conditional expression of form cond ? e1 : e2 that is a glvalue, where e1 or e2 is one of these expressions, or
- a built-in comma expression of form x, e that is a glvalue, where e 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 return statement is not extended: it is destroyed immediately at the end of the return expression. Such return 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.
|
(since C++11) |
struct A { int&& r; }; A a1{7}; // OK, lifetime is extended A a2(7); // well-formed, but dangling reference |
(since C++20) |
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.
[edit] Notes
References appear without initializers only in function parameter declaration, in function return type declaration, in the declaration of a class member, and with the extern
specifier.
Until the resolution of CWG issue 1696, a temporary is permitted to bound to a reference member in a constructor initializer list, and it persists only until the constructor exits, not as long as the object exists. Such initialization is ill-formed since CWG 1696, although many compilers still support it (a notable exception is clang).
[edit] Example
#include <sstream> #include <utility> struct S { int mi; const std::pair<int, int>& mp; // reference member }; void foo(int) {} struct A {}; struct B : A { int n; operator int&() { return n; } }; B bar() { return B(); } //int& bad_r; // error: no initializer extern int& ext_r; // OK int main() { // Lvalues int n = 1; int& r1 = n; // lvalue reference to the object n const int& cr(n); // reference can be more cv-qualified volatile int& cv{n}; // any initializer syntax can be used int& r2 = r1; // another lvalue reference to the object n // int& bad = cr; // error: less cv-qualified int& r3 = const_cast<int&>(cr); // const_cast is needed void (&rf)(int) = foo; // lvalue reference to function int ar[3]; int (&ra)[3] = ar; // lvalue reference to array B b; A& base_ref = b; // reference to base subobject int& converted_ref = b; // reference to the result of a conversion // Rvalues // int& bad = 1; // error: cannot bind lvalue ref to rvalue const int& cref = 1; // bound to rvalue int&& rref = 1; // bound to rvalue const A& cref2 = bar(); // reference to A subobject of B temporary A&& rref2 = bar(); // same int&& xref = static_cast<int&&>(n); // bind directly to n // int&& copy_ref = n; // error: can't bind to an lvalue double&& copy_ref = n; // bind to an rvalue temporary with value 1.0 // Restrictions on temporary lifetimes // std::ostream& buf_ref = std::ostringstream() << 'a'; // the ostringstream temporary was bound to the left operand // of operator<< but its lifetime ended at the semicolon so // the buf_ref is a dangling reference S a {1, {2, 3}}; // temporary pair {2, 3} bound to the reference member // a.mp and its lifetime is extended to match // the lifetime of object a S* p = new S{1, {2, 3}}; // temporary pair {2, 3} bound to the reference // member p->mp, but its lifetime ended at the semicolon // p->mp is a dangling reference delete p; // Imitate [[maybe_unused]] applied to the following variables: [](...){} ( cv, r2, r3, rf, ra, base_ref, converted_ref, a, cref, rref, cref2, rref2, copy_ref, xref ); }
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 391 | C++98 | initialize a reference to const-qualified type with a class type rvalue might create a temporary, and a constructor of that class was required in order to copy the rvalue into that temporary |
no temporary is created, constructor is not required |
CWG 450 | C++98 | a reference to const-qualified array could not be initialized with a reference-compatible array rvalue |
allowed |
CWG 656 | C++98 | a reference to const-qualified type initialized with a type which is not reference-compatible but has a conversion function to a reference- compatible type was bound to a temporary copied from the return value (or its base class subobject) of the conversion function |
bound to the return value (or its base class subobject) directly |
CWG 1299 | C++98 | the definition of temporary was unclear | made clear |