Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/reference initialization"

From cppreference.com
< cpp‎ | language
(Improved styles of the syntaxes.)
(Added CWG issue #2657 DR.)
 
(18 intermediate revisions by 6 users not shown)
Line 4: Line 4:
  
 
===Syntax===
 
===Syntax===
 +
=====Non-list-initialization=====
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|<!-- Force generation of a p-element --><nowiki/>
+
{{sdsc|num=1|
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{{=}}}} {{spar|target}} {{ttb|;}}
+
{{spar sep|T}}{{ttb|&}} {{spar|ref}} {{ttb|1==}} {{spar|target}} {{ttb|;}}<br>
 
+
{{spar sep|T}}{{ttb|&}} {{spar|ref}} {{ttb|(}} {{spar|target}} {{ttb|);}}
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{{=}} {}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}
+
 
+
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|(}} {{spar|target}} {{ttb|);}}
+
 
+
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}
+
 
}}
 
}}
{{sdsc|num=2|<!-- Force generation of a p-element --><nowiki/>
+
{{sdsc|num=2|notes={{mark since c++11}}|
{{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|{{=}}}} {{spar|target}} {{ttb|;}}
+
{{spar sep|T}}{{ttb|&&}} {{spar|ref}} {{ttb|1==}} {{spar|target}} {{ttb|;}}<br>
 
+
{{spar sep|T}}{{ttb|&&}} {{spar|ref}} {{ttb|(}} {{spar|target}} {{ttb|);}}
{{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|{{=}} {}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}
+
 
+
{{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|(}} {{spar|target}} {{ttb|);}}
+
 
+
{{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|{}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}
+
|notes={{mark since c++11}}
+
 
}}
 
}}
{{sdsc|num=3|given {{spar|R}} {{spar|fn}} {{ttb|(}} {{spar|T}} {{ttb|&}} {{spar|arg}} {{ttb|);}}{{rev inl|since=c++11| or {{spar|R}} {{spar|fn}} {{ttb|(}} {{spar|T}} {{ttb|&&}} {{spar|arg}} {{ttb|);}}}}
+
{{sdsc|num=3|
 
+
{{spar|func-refpar}} {{ttb|(}} {{spar|target}} {{ttb|)}}
{{spar|fn}} {{ttb|(}} {{spar|target}} {{ttb|)}}
+
 
+
{{spar|fn}} {{ttb|({}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|});}}
+
 
}}
 
}}
{{sdsc|num=4|inside {{spar|T}} {{ttb|&}} {{spar|fn}} {{ttb|()}}{{rev inl|since=c++11| or {{spar|T}} {{ttb|&&}} {{spar|fn}} {{ttb|()}}}}
+
{{sdsc|num=4|notes={{mark|inside the definition of {{spar sep|func-refret}}}}|
 +
{{ttb|return}} {{spar|target}} {{ttb|;}}
 +
}}
 +
{{sdsc|num=5|notes={{mark|inside the definition of {{spar sep|Class}}}}|
 +
{{spar|Class}}{{ttb|::}}{{spar|Class}}{{ttb|(}}...{{ttb|) :}} {{spar|ref-member}} {{ttb|(}} {{spar|target}} {{ttb|) {}} ... {{ttb|}<!-- -->}}
 +
}}
 +
{{sdsc end}}
  
{{ttb|return}} {{spar|target}} {{ttb|;}}  
+
=====Ordinary list-initialization {{mark since c++11}}=====
 +
{{sdsc begin}}
 +
{{sdsc|num=1|
 +
{{spar sep|T}}{{ttb|&}} {{spar|ref}} {{ttb|1== {}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}<br>
 +
{{spar sep|T}}{{ttb|&}} {{spar|ref}} {{ttb|{}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}
 
}}
 
}}
{{sdsc|num=5|given {{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|;}}{{rev inl|since=c++11| or {{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|;}}}} inside the definition of {{spar|Class}}
+
{{sdsc|num=2|
 +
{{spar sep|T}}{{ttb|&&}} {{spar|ref}} {{ttb|1== {}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}<br>
 +
{{spar sep|T}}{{ttb|&&}} {{spar|ref}} {{ttb|{}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|};}}
 +
}}
 +
{{sdsc|num=3|
 +
{{spar|func-refpar}} {{ttb|({}} {{spar|arg1}}{{ttb|,}} {{spar|arg2}}{{ttb|,}} ... {{ttb|});}}
 +
}}
 +
{{sdsc end}}
  
{{spar|Class}}{{ttb|::}}{{spar|Class}}{{ttb|(}}...{{ttb|) :}} {{spar|ref}} {{ttb|(}} {{spar|target}} {{ttb|) {}} ... {{ttb|}<!-- -->}}}}
+
=====Designated list-initialization {{mark since c++20}}=====
 +
{{sdsc begin}}
 +
{{sdsc|num=1|
 +
{{spar sep|T}}{{ttb|&}} {{spar|ref}} {{ttb|1== {.}}{{spar|des1}} {{ttb|1==}} {{spar|arg1}} {{ttb|, .}}{{spar|des2}} {{ttb|{}} {{spar|arg2}} {{ttb|}<!-- -->}} ... {{ttb|};}}<br>
 +
{{spar sep|T}}{{ttb|&}} {{spar|ref}} {{ttb|{.}}{{spar|des1}} {{ttb|1==}} {{spar|arg1}} {{ttb|, .}}{{spar|des2}} {{ttb|{}} {{spar|arg2}} {{ttb|}<!-- -->}} ... {{ttb|};}}
 +
}}
 +
{{sdsc|num=2|
 +
{{spar sep|T}}{{ttb|&&}} {{spar|ref}} {{ttb|1== {.}}{{spar|des1}} {{ttb|1==}} {{spar|arg1}} {{ttb|, .}}{{spar|des2}} {{ttb|{}} {{spar|arg2}} {{ttb|}<!-- -->}} ... {{ttb|};}}<br>
 +
{{spar sep|T}}{{ttb|&&}} {{spar|ref}} {{ttb|{.}}{{spar|des1}} {{ttb|1==}} {{spar|arg1}} {{ttb|, .}}{{spar|des2}} {{ttb|{}} {{spar|arg2}} {{ttb|}<!-- -->}} ... {{ttb|};}}
 +
}}
 +
{{sdsc|num=3|
 +
{{spar|func-refpar}} {{ttb|({.}}{{spar|des1}} {{ttb|1==}} {{spar|arg1}} {{ttb|, .}}{{spar|des2}} {{ttb|{}} {{spar|arg2}} {{ttb|}<!-- -->}} ... {{ttb|});}}
 +
}}
 
{{sdsc end}}
 
{{sdsc end}}
  
===Explanation===
+
A reference to {{tt|T}} can be initialized with an object of type {{tt|T}}, a function of type {{tt|T}}, or an object implicitly convertible to {{tt|T}}. Once initialized, a reference cannot be reseated (changed) to refer to another object.
A reference to {{tt|T}} can be initialized with an object of type {{tt|T}}, a function of type {{tt|T}}, or an object implicitly convertible to {{tt|T}}. Once initialized, a reference cannot be changed to refer to another object.
+
  
 
References are initialized in the following situations:
 
References are initialized in the following situations:
@1@ When a named {{rlp|reference#Lvalue references|lvalue reference}} variable is declared with an initializer
+
@1@ When a named {{rlp|reference#Lvalue references|lvalue reference}} variable is declared with an initializer.
@2@ When a named {{rlp|reference#Rvalue references|rvalue reference}} variable is declared with an initializer
+
@2@ When a named {{rlp|reference#Rvalue references|rvalue reference}} variable is declared with an initializer.
@3@ In a function call expression, when the function parameter has reference type
+
@3@ In a function call expression, when the function parameter has reference type.
@4@ In the {{c|return}} statement, when the function returns a reference type
+
@4@ In the {{c/core|return}} statement, when the function returns a reference type. {{rev inl|since=c++26|The program is ill-formed if the returned reference is bound to the result of a [[#Lifetime_of_a_temporary|temporary expression]].}}
@5@ When a {{rlp|data members|non-static data member}} of reference type is initialized using a {{rlp|initializer list|member initializer}}
+
@5@ When a {{rlp|data members|non-static data member}} of reference type is initialized using a {{rlp|initializer list|member initializer}}.
  
The effects of reference initialization are:
+
===Explanation===
 +
{{par begin}}
 +
{{par|{{spar|T}}|the referenced type}}
 +
{{par|{{spar|ref}}|the reference variable to be initialized}}
 +
{{par|{{spar|target}}|the initializer expression being used}}
 +
{{par|{{spar|func-refpar}}|a function with a parameter of reference type ({{spar sep|T}}{{tt|&}}{{rev inl|since=c++11| or {{spar sep|T}}{{tt|&&}}}})}}
 +
{{par|{{spar|func-refret}}|a function whose returns type is a reference type ({{spar sep|T}}{{tt|&}}{{rev inl|since=c++11| or {{spar sep|T}}{{tt|&&}}}})}}
 +
{{par|{{spar|Class}}|a class name}}
 +
{{par|{{spar|ref-member}}|a non-static data member of reference type ({{spar sep|T}}{{tt|&}}{{rev inl|since=c++11| or {{spar sep|T}}{{tt|&&}}}}) of {{spar|Class}}}}
 +
{{par|{{spar|des1}}, {{spar|des2}}, ...|designators}}
 +
{{par|{{spar|arg1}}, {{spar|arg2}}, ...|the initializers in initializer lists}}
 +
{{par end}}
  
* If the initializer is a braced-init-list {{ttb|{}} {{spar|arg1, arg2, ...}} {{ttb|}<!-- -->}}, rules of {{rlp|list initialization}} are followed.
+
===Definitions===
 +
For two types {{tt|T1}} and {{tt|T2}}:
 +
* Given the cv-unqualified versions of {{tt|T1}} and {{tt|T2}} as {{tt|U1}} and {{tt|U2}} respectively, if {{tt|U1}} is {{rlp|implicit conversion#Similar types|similar}} to {{tt|U2}}, or {{tt|U1}} is a {{rlp|derived class|base class}} of {{tt|U2}}, {{tt|T1}} is ''reference-related'' to {{tt|T2}}.
 +
* If a prvalue of type “pointer to {{tt|T2}}” can be converted to the type “pointer to {{tt|T1}}” via a standard conversion sequence, {{tt|T1}} is ''reference-compatible'' with {{tt|T2}}.
  
* Otherwise, if the reference is an lvalue reference:
+
===Initialization rules===
 +
{{rrev|since=c++11|
 +
If a reference initialization uses an ordinary{{rev inl|since=c++20| or designated}} list-initialization, the rules of {{rlp|list initialization|list-initialization}} are followed.
 +
}}
 +
 
 +
For non-list reference initialization, given the type of {{spar|target}} as {{tt|U}}, the reference either ''binds directly'' to {{spar|target}} or binds to a value of type {{tt|T}} converted from {{spar|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.
  
:* If {{spar|target}} is an lvalue expression, and its type is {{tt|T}} or derived from {{tt|T}}, 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.
+
====Direct binding====
 +
If all following conditions are satisfied:
 +
* The reference to be initialized is an lvalue reference.
 +
* {{spar|target}} is a non-{{rlp|bit field|bit-field}} lvalue.
 +
* {{tt|T}} is reference-compatible with {{tt|U}}.
 +
Then the reference binds to {{spar|target}}, or to its appropriate base class subobject:
 
{{source|1=
 
{{source|1=
 
double d = 2.0;
 
double d = 2.0;
Line 68: Line 109:
 
}}
 
}}
  
:* Otherwise, if the type of {{spar|target}} is not same or derived from {{tt|T}}, and {{spar|target}} has conversion function to an lvalue whose type is either {{tt|T}} or derived from {{tt|T}}, equally or less cv-qualified, <!--the non-explicit conversion functions of the source type and its base classes that return lvalue references are considered and the best one is selected by overload resolution,--> 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 all following conditions are satisfied:
 +
* The reference to be initialized is an lvalue reference.
 +
* {{tt|U}} is a class type.
 +
* {{tt|T}} is not reference-related to {{tt|U}}.
 +
* {{spar|target}} can be converted to an lvalue of type {{tt|V}} such that {{tt|T}} is reference-compatible with {{tt|V}}.
 +
Then the reference binds to the lvalue result of the conversion, or to its appropriate base class subobject:
 
{{source|1=
 
{{source|1=
 
struct A {};
 
struct A {};
Line 76: Line 122:
 
}}
 
}}
  
* Otherwise, if the reference is lvalue reference to a non-volatile const-qualified type {{rev inl|since=c++11| or rvalue reference}}:
+
Otherwise, if the reference to be initialized is an lvalue reference, and {{tt|T}} is not const-qualified or is volatile-qualified, the prorgram is ill-formed:
 +
{{source|1=
 +
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
 +
}}
  
:* If {{spar|target}} is a non-bit-field rvalue or a function lvalue, and its type is either {{tt|T}} or derived from {{tt|T}}, equally or less cv-qualified, then the reference is bound to the value of the initializer expression or to its base subobject {{rev inl|since=c++17|(after {{rlp|implicit conversion#Temporary materialization|materializing a temporary}} if necessary)}}.
+
Otherwise, if all following conditions are satisfied:
 +
* {{spar|target}} is a value of any following category:
 +
{{rev begin}}
 +
{{rev|until=c++11|
 +
:* rvalue
 +
}}
 +
{{rev|since=c++11|until=c++17|
 +
:* non-bit-field xvalue
 +
:* class prvalue
 +
:* array prvalue
 +
:* function lvalue
 +
}}
 +
{{rev|since=c++17|
 +
:* non-bit-field rvalue
 +
:* function lvalue
 +
}}
 +
{{rev end}}
 +
* {{tt|T}} is reference-compatible with {{tt|U}}.
 +
Then the reference binds to {{spar|target}}, or to its appropriate base class subobject:
 
{{source|1=
 
{{source|1=
 
struct A {};
 
struct A {};
Line 91: Line 160:
 
}}
 
}}
  
:* Otherwise, if the type of {{spar|target}} is not same or derived from {{tt|T}}, and {{spar|target}} has conversion function to a rvalue or a function lvalue whose type is either {{tt|T}} or derived from {{tt|T}}, equally or less cv-qualified, <!--the non-explicit conversion functions of the source type and its base classes that return values or rvalue references are considered and the best one is selected by overload resolution,--> then the reference is bound to the result of the conversion function or to its base class subobject {{rev inl|since=c++17|(after {{rlp|implicit conversion#Temporary materialization|materializing a temporary}} if necessary)}}.
+
{{rrev|since=c++17|
 +
If {{spar|target}} is a prvalue, {{rlpsd|implicit conversion#Temporary materialization}} is applied to it, considering the type of the prvalue to be the adjusted type {{tt|P}}.
 +
* {{tt|P}} is {{rlp|implicit conversion#Qualification conversions|adjusted}} from the type of {{spar|target}} (i.e. {{tt|U}}) by adding the cv-qualification of {{tt|T}} to it.
 +
 
 +
In this case, the reference binds to the result object, or to its appropriate base class subobject.
 +
}}
 +
 
 +
Otherwise, if all following conditions are satisfied:
 +
* {{tt|U}} is a class type.
 +
* {{tt|T}} is not reference-related to {{tt|U}}.
 +
* {{spar|target}} can be converted to a value {{c|v}} of type {{tt|V}} such that {{tt|T}} is reference-compatible with {{tt|V}}, where {{c|v}} is of any following category:
 +
{{rev begin}}
 +
{{rev|until=c++11|
 +
:* rvalue
 +
}}
 +
{{rev|since=c++11|until=c++17|
 +
:* xvalue
 +
:* class prvalue
 +
:* function lvalue
 +
}}
 +
{{rev|since=c++17|
 +
:* rvalue
 +
:* function lvalue
 +
}}
 +
{{rev end}}
 +
Then the reference binds to the result of the conversion, or to its appropriate base class subobject:
 
{{source|1=
 
{{source|1=
 
struct A {};
 
struct A {};
Line 101: Line 195:
 
}}
 
}}
  
:* Otherwise, {{spar|target}} is implicitly converted to {{tt|T}}. The reference is bound to the result of the conversion{{rev inl|since=c++17| (after {{rlp|implicit conversion#Temporary materialization|materializing a temporary}}, and the cv-qualification of {{tt|T}} is preserved even if it is a scalar type<!-- CWG 2481 -->)}}. If the {{spar|target}} (or, if the conversion is done by user-defined conversion, the result of the conversion function) is of type {{tt|T}} or derived from {{tt|T}}, it must be equally or less cv-qualified than {{tt|T}}{{rev inl|since=c++11|, and, if the reference is an rvalue reference, must not be an lvalue}}.
+
{{rrev|since=c++17|
 +
If the result of the conversion is a prvalue, {{rlpsd|implicit conversion#Temporary materialization}} is applied to it, considering the type of the prvalue to be the adjusted type {{tt|P}}.
 +
* {{tt|P}} is {{rlp|implicit conversion#Qualification conversions|adjusted}} from the type of the conversion result by adding the cv-qualification of {{tt|T}} to it.
 +
 
 +
In this case, the reference binds to the result object, or to its appropriate base class subobject.
 +
}}
 +
 
 +
====Indirect binding====
 +
If direct binding is not available, indirect binding is considered. In this case, {{tt|T}} cannot be reference-related to {{tt|U}}.
 +
 
 +
If {{tt|T}} or {{tt|U}} is a class type, user-defined conversions are considered using the rules for {{rlp|copy initialization|copy-initialization}} of an object of type {{tt|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 {{rlp|copy initialization|copy-initialization}}, is then used to direct-initialize the reference. For this direct-initialization, user-defined conversions are not considered.
 +
 
 +
{{rev begin}}
 +
{{rev|until=c++17|
 +
Otherwise, a temporary of type {{tt|T}} is created and copy-initialized from {{spar|target}}. The reference is then bound to the temporary.
 +
}}
 +
{{rev|since=c++17|
 +
Otherwise, {{spar|target}} is implicitly converted to a prvalue of type “cv-unqualified {{tt|T}}”. The temporary materialization conversion is applied, considering the type of the prvalue to be {{tt|T}}, and the reference is bound to the result object.
 +
}}
 +
{{rev end}}
 +
 
 
{{source|1=
 
{{source|1=
 
const std::string& rs = "abc"; // rs refers to temporary copy-initialized from char array
 
const std::string& rs = "abc"; // rs refers to temporary copy-initialized from char array
Line 112: Line 226:
 
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 {{rlp|lifetime#Temporary object lifetime|temporary object lifetime exceptions}}), where the temporary object or its subobject is denoted by one of following expression:
 
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 {{rlp|lifetime#Temporary object lifetime|temporary object lifetime exceptions}}), where the temporary object or its subobject is denoted by one of following expression:
 
{{rrev multi|rev1=
 
{{rrev multi|rev1=
* a {{rlp|value category#prvalue|prvalue}} expression of an object type,
+
* a {{rlps|value category#prvalue}} expression of an object type,
 
|since2=c++17|rev2=
 
|since2=c++17|rev2=
 
* a {{rlp|implicit conversion#Temporary materialization|temporary materialization conversion}},
 
* a {{rlp|implicit conversion#Temporary materialization|temporary materialization conversion}},
Line 125: Line 239:
  
 
There are following exceptions to this lifetime rule:
 
There are following exceptions to this lifetime rule:
* a temporary bound to a return value of a function in a {{c|return}} statement is not extended: it is destroyed immediately at the end of the return expression. Such {{c|return}} statement always returns a dangling reference.
+
{{rrev|until=c++26|
 +
* a temporary bound to a return value of a function in a {{c/core|return}} statement is not extended: it is destroyed immediately at the end of the return expression. Such {{c/core|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.
 
* 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.
 
{{rrev|since=c++11|
 
{{rrev|since=c++11|
Line 131: Line 247:
 
}}
 
}}
 
{{rrev|since=c++20|
 
{{rrev|since=c++20|
* a temporary bound to a reference in a reference element of an aggregate initialized using direct-initialization syntax (parentheses) as opposed to list-initialization syntax (braces) exists until the end of the full expression containing the initializer.
+
* a temporary bound to a reference in a reference element of an aggregate initialized using {{rlp|direct_initialization|direct-initialization}} syntax '''('''parentheses''')''' exists until the end of the full expression containing the initializer, as opposed to {{rlp|list_initialization|list-initialization}} syntax '''{'''braces'''}'''.
 
{{source|1=
 
{{source|1=
 
struct A
 
struct A
Line 146: Line 262:
  
 
===Notes===
 
===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 {{tt|extern}} specifier.
+
References appear without initializers only in function parameter declaration, in function return type declaration, in the declaration of a class member, and with the {{rlpt|storage duration|extern}} specifier.
  
Until the resolution of [https://wg21.cmeerw.net/cwg/issue1696 CWG issue 1696], a temporary is permitted to bound to a reference member in a constructor {{rlp|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).
+
Until the resolution of {{cwg|1696}}, a temporary is permitted to bound to a reference member in a constructor {{rlp|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).
  
 
===Example===
 
===Example===
Line 154: Line 270:
 
|
 
|
 
|code=
 
|code=
#include <utility>
 
 
#include <sstream>
 
#include <sstream>
 +
#include <utility>
  
 
struct S
 
struct S
Line 210: Line 326:
  
 
//  Restrictions on temporary lifetimes
 
//  Restrictions on temporary lifetimes
    std::ostream& buf_ref = std::ostringstream() << 'a'; // the ostringstream temporary
+
//  std::ostream& buf_ref = std::ostringstream() << 'a';
                      // was bound to the left operand of operator<<
+
                    // the ostringstream temporary was bound to the left operand
                      // but its lifetime ended at the semicolon
+
                    // of operator<< but its lifetime ended at the semicolon so
                      // so buf_ref is a dangling reference
+
                    // the buf_ref is a dangling reference
  
     S a {1, {2, 3}<!-- -->};         // temporary pair {2, 3} bound to the reference member
+
     S a {1, {2, 3}<!---->}; // temporary pair {2, 3} bound to the reference member
                            // a.mp and its lifetime is extended to match  
+
                    // a.mp and its lifetime is extended to match  
                            // the lifetime of object a
+
                    // the lifetime of object a
     S* p = new S{1, {2, 3}<!-- -->}; // temporary pair {2, 3} bound to the reference
+
 
 +
     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
 
                             // member p->mp, but its lifetime ended at the semicolon
 
                             // p->mp is a dangling reference
 
                             // p->mp is a dangling reference
 
     delete p;
 
     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
 +
    );
 
}
 
}
 
|output=
 
|output=
Line 229: Line 353:
 
{{dr list begin}}
 
{{dr list begin}}
 
{{dr list item|wg=cwg|dr=391|std=C++98|before=initialize a reference to const-qualified type with a class type<br>rvalue might create a temporary, and a constructor of that class<br>was required in order to copy the rvalue into that temporary|after=no temporary is<br>created, constructor<br>is not required}}
 
{{dr list item|wg=cwg|dr=391|std=C++98|before=initialize a reference to const-qualified type with a class type<br>rvalue might create a temporary, and a constructor of that class<br>was required in order to copy the rvalue into that temporary|after=no temporary is<br>created, constructor<br>is not required}}
 +
{{dr list item|wg=cwg|dr=450|std=C++98|before=a reference to const-qualified array could not be<br>initialized with a reference-compatible array rvalue|after=allowed}}
 +
{{dr list item|wg=cwg|dr=589|std=C++98|before=a reference could not bind directly to an array or class rvalue|after=allowed}}
 +
{{dr list item|wg=cwg|dr=656|std=C++98|before=a reference to const-qualified type initialized with a type which is not<br>reference-compatible but has a conversion function to a reference-<br>compatible type was bound to a temporary copied from the return<br>value (or its base class subobject) of the conversion function|after=bound to the return<br>value (or its base class<br>subobject) directly}}
 +
{{dr list item|wg=cwg|dr=1287|std=C++11|before=the conversion from {{spar|target}} of class type to another<br>reference-compatible type could only be implicit|after=allow explicit<br>conversions}}
 +
{{dr list item|wg=cwg|dr=1295|std=C++11|before=a reference could bind to a bit-field xvalue|after=prohibited}}
 
{{dr list item|wg=cwg|dr=1299|std=C++98|before=the definition of temporary was unclear|after=made clear}}
 
{{dr list item|wg=cwg|dr=1299|std=C++98|before=the definition of temporary was unclear|after=made clear}}
 +
{{dr list item|wg=cwg|dr=1571|std=C++98|before=user-defined conversions in indirect<br>binding did not consider the type of {{spar|target}}|after=considered}}
 +
{{dr list item|wg=cwg|dr=1604|std=C++98|before=user-defined conversions were not considered in indirect binding|after=considered}}
 +
{{dr list item|wg=cwg|dr=2352|std=C++98|before=reference compatibility did not consider qualification conversions|after=considered}}
 +
{{dr list item|wg=cwg|dr=2481|std=C++17|before=cv-qualification was not added to the result type<br>of temporary materialization in indirect binding|after=added}}
 +
{{dr list item|wg=cwg|dr=2657|std=C++17|before=cv-qualification was not added to the result type<br>of temporary materialization in direct binding|after=added}}
 +
{{dr list item|wg=cwg|dr=2801|std=C++98|before=reference-related types were allowed for indirect binding|after=prohibited}}
 
{{dr list end}}
 
{{dr list end}}
  
Line 237: Line 372:
 
* {{rlp|copy assignment}}
 
* {{rlp|copy assignment}}
 
* {{rlp|copy constructor}}
 
* {{rlp|copy constructor}}
* {{rlp|explicit}}
+
* {{rlpt|explicit}}
 
* {{rlp|initialization}}
 
* {{rlp|initialization}}
 
** {{rlp|constant initialization}}
 
** {{rlp|constant initialization}}
Line 245: Line 380:
 
* {{rlp|move assignment}}
 
* {{rlp|move assignment}}
 
* {{rlp|move constructor}}
 
* {{rlp|move constructor}}
* {{rlp|new}}
+
* {{rlpt|new}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 18:25, 20 May 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Binds a reference to an object.

Contents

[edit] Syntax

[edit] Non-list-initialization
T & ref = target ;

T & ref ( target );

(1)
T && ref = target ;

T && ref ( target );

(2) (since C++11)
func-refpar ( 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, ... };

(1)
T && ref = { arg1, arg2, ... };

T && ref { arg1, arg2, ... };

(2)
func-refpar ({ arg1, arg2, ... }); (3)
[edit] Designated list-initialization (since C++20)
T & ref = {.des1 = arg1 , .des2 { arg2 } ... };

T & ref {.des1 = arg1 , .des2 { arg2 } ... };

(1)
T && ref = {.des1 = arg1 , .des2 { arg2 } ... };

T && ref {.des1 = arg1 , .des2 { arg2 } ... };

(2)
func-refpar ({.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:

1) When a named lvalue reference variable is declared with an initializer.
2) When a named rvalue reference variable is declared with an initializer.
3) In a function call expression, when the function parameter has reference type.
4) In the return statement, 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)
5) When a non-static data member of reference type is initialized using a member initializer.

[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:

  • Given the cv-unqualified versions of 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.
  • If a prvalue of type “pointer to T2” can be converted to the type “pointer to T1” via a standard conversion sequence, T1 is reference-compatible with T2.

[edit] Initialization rules

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 binding

If all following conditions are satisfied:

  • The reference to be initialized is an lvalue reference.
  • target is a non-bit-field lvalue.
  • 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:

  • The reference to be initialized is an lvalue reference.
  • U is a class type.
  • T is not reference-related to U.
  • target can be converted to an lvalue of type 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:

  • target is a value of any following category:
  • rvalue
(until C++11)
  • non-bit-field xvalue
  • class prvalue
  • array prvalue
  • function lvalue
(since C++11)
(until C++17)
  • non-bit-field rvalue
  • function lvalue
(since C++17)
  • 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.
  • target can be converted to a value v of type V such that T is reference-compatible with V, where v is of any following category:
  • rvalue
(until C++11)
  • xvalue
  • class prvalue
  • function lvalue
(since C++11)
(until C++17)
  • rvalue
  • function lvalue
(since C++17)

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 binding

If 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.

(until C++17)

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.

(since C++17)
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:

  • a prvalue expression of an object type,
(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, 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),
  • 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.
(until C++26)
  • 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.
  • a temporary bound to a reference in the initializer used in a new-expression exists until the end of the full expression containing that new-expression, not as long as the initialized object. If the initialized object outlives the full expression, its reference member becomes a dangling reference.
(since C++11)
  • a temporary bound to a reference in a reference element of an aggregate initialized using direct-initialization syntax (parentheses) exists until the end of the full expression containing the initializer, as opposed to list-initialization syntax {braces}.
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 589 C++98 a reference could not bind directly to an array or class 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 1287 C++11 the conversion from target of class type to another
reference-compatible type could only be implicit
allow explicit
conversions
CWG 1295 C++11 a reference could bind to a bit-field xvalue prohibited
CWG 1299 C++98 the definition of temporary was unclear made clear
CWG 1571 C++98 user-defined conversions in indirect
binding did not consider the type of target
considered
CWG 1604 C++98 user-defined conversions were not considered in indirect binding considered
CWG 2352 C++98 reference compatibility did not consider qualification conversions considered
CWG 2481 C++17 cv-qualification was not added to the result type
of temporary materialization in indirect binding
added
CWG 2657 C++17 cv-qualification was not added to the result type
of temporary materialization in direct binding
added
CWG 2801 C++98 reference-related types were allowed for indirect binding prohibited

[edit] See also