Binds a reference to an object.
[edit] Syntax [edit] Non-list-initialization T &
ref =
target ;
T &
ref (
target );
&&
ref =
target ;
T &&
ref (
target );
(
target )
(3) return
target ;
(4) (inside the definition of func-refret ) Class::
Class(
...) :
ref-member (
target ) {
... }
(5) (inside the definition of Class ) [edit] Ordinary list-initialization (since C++11) T &
ref = {
arg1,
arg2,
... };
T &
ref {
arg1,
arg2,
... };
&&
ref = {
arg1,
arg2,
... };
T &&
ref {
arg1,
arg2,
... };
({
arg1,
arg2,
... });
(3) [edit] Designated list-initialization (since C++20) T &
ref = {.
des1 =
arg1 , .
des2 {
arg2 }
... };
T &
ref {.
des1 =
arg1 , .
des2 {
arg2 }
... };
&&
ref = {.
des1 =
arg1 , .
des2 {
arg2 }
... };
T &&
ref {.
des1 =
arg1 , .
des2 {
arg2 }
... };
({.
des1 =
arg1 , .
des2 {
arg2 }
... });
(3)
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:
3) In a function call expression, when the function parameter has reference type.
4)In the
returnstatement, when the function returns a reference type.
The program is ill-formed if the returned reference is bound to the result of a temporary expression.(since C++26) [edit] Explanation T - the referenced type ref - the reference variable to be initialized target - the initializer expression being used func-refpar - a function with a parameter of reference type (T &
or T &&
(since C++11)) func-refret - a function whose returns type is a reference type (T &
or T &&
(since C++11)) Class - a class name ref-member - a non-static data member of reference type (T &
or T &&
(since C++11)) of Class des1, des2, ... - designators arg1, arg2, ... - the initializers in initializer lists [edit] Definitions
For two types T1
and T2
:
T1
and T2
as U1
and U2
respectively, if U1
is similar to U2
, or U1
is a base class of U2
, T1
is reference-related to T2
.T2
â can be converted to the type âpointer to T1
â via a standard conversion sequence, T1
is reference-compatible with T2
.If a reference initialization uses an ordinary or designated(since C++20) list-initialization, the rules of list-initialization are followed.
(since C++11)For non-list reference initialization, given the type of target as U
, the reference either binds directly to target or binds to a value of type T
converted from target. Direct binding is considered first, followed by indirect binding, if neither binding is available, the program is ill-formed.
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.
[edit] Direct bindingIf all following conditions are satisfied:
T
is reference-compatible with U
.Then the reference binds to target, or to its appropriate base class subobject:
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 all following conditions are satisfied:
U
is a class type.T
is not reference-related to U
.V
such that T
is reference-compatible with V
.Then the reference binds to the lvalue result of the conversion, or to its appropriate base class subobject:
struct A {}; struct B : A { operator int&(); }; int& ir = B(); // ir refers to the result of B::operator int&
Otherwise, if the reference to be initialized is an lvalue reference, and T
is not const-qualified or is volatile-qualified, the prorgram is ill-formed:
double& rd2 = 2.0; // error: not an lvalue and reference is not const int i = 2; double& rd3 = i; // error: type mismatch and reference is not const
Otherwise, if all following conditions are satisfied:
T
is reference-compatible with U
.Then the reference binds to target, or to its appropriate base class subobject:
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
If target is a prvalue, temporary materialization is applied to it, considering the type of the prvalue to be the adjusted type P
.
P
is adjusted from the type of target (i.e. U
) by adding the cv-qualification of T
to it.In this case, the reference binds to the result object, or to its appropriate base class subobject.
(since C++17)Otherwise, if all following conditions are satisfied:
U
is a class type.T
is not reference-related to U
.V
such that T
is reference-compatible with V
, where v is of any following category:Then the reference binds to the result of the conversion, or to its appropriate base class subobject:
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
If the result of the conversion is a prvalue, temporary materialization is applied to it, considering the type of the prvalue to be the adjusted type P
.
P
is adjusted from the type of the conversion result by adding the cv-qualification of T
to it.In this case, the reference binds to the result object, or to its appropriate base class subobject.
(since C++17) [edit] Indirect bindingIf direct binding is not available, indirect binding is considered. In this case, T
cannot be reference-related to U
.
If T
or U
is a class type, user-defined conversions are considered using the rules for copy-initialization of an object of type T
by user-defined conversion. The program is ill-formed if the corresponding non-reference copy-initialization would be ill-formed. The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.
Otherwise, a temporary of type T
is created and copy-initialized from target. The reference is then bound to the temporary.
Otherwise, target is implicitly converted to a prvalue of type âcv-unqualified T
â. The temporary materialization conversion is applied, considering the type of the prvalue to be T
, and the reference is bound to the result object.
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:
const_cast
, static_cast
, dynamic_cast
, or reinterpret_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),There are following exceptions to this lifetime rule:
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] NotesReferences 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 typeexplicit
new
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4