Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language
m (Added a missing period.)
(Added CWG issue #391 DR.)
Line 5: Line 5:
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc | num=1 | <!-- Force generation of a p-element --><nowiki/>
+
{{sdsc|num=1|<!-- Force generation of a p-element --><nowiki/>
 
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{{=}}}} {{spar|target}} {{ttb|;}}
 
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{{=}}}} {{spar|target}} {{ttb|;}}
  
Line 14: Line 14:
 
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{ }} {{spar|arg1, arg2, ...}} {{ttb|} }}{{ttb|;}}
 
{{spar|T}} {{ttb|&}} {{spar|ref}} {{ttb|{ }} {{spar|arg1, arg2, ...}} {{ttb|} }}{{ttb|;}}
 
}}
 
}}
{{sdsc | num=2 | <!-- Force generation of a p-element --><nowiki/>
+
{{sdsc|num=2|<!-- Force generation of a p-element --><nowiki/>
 
{{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|{{=}}}} {{spar|target}} {{ttb|;}}
 
{{spar|T}} {{ttb|&&}} {{spar|ref}} {{ttb|{{=}}}} {{spar|target}} {{ttb|;}}
  
Line 24: Line 24:
 
|notes={{mark since c++11}}
 
|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|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|);}}}}
  
 
{{spar|fn}} {{ttb|(}} {{spar|target}} {{ttb|)}}
 
{{spar|fn}} {{ttb|(}} {{spar|target}} {{ttb|)}}
Line 30: Line 30:
 
{{spar|fn}} {{ttb|(}} {{ttb|{}} {{spar|arg1, arg2, ...}} {{ttb|} }} {{ttb|)}}
 
{{spar|fn}} {{ttb|(}} {{ttb|{}} {{spar|arg1, 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|inside {{spar|T}} {{ttb|&}} {{spar|fn}} {{ttb|()}}{{rev inl|since=c++11| or {{spar|T}} {{ttb|&&}} {{spar|fn}} {{ttb|()}}}}
  
 
{{ttb|return}} {{spar|target}} {{ttb|;}}  
 
{{ttb|return}} {{spar|target}} {{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=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}}
  
{{spar|Class}}{{ttb|::}}{{spar|Class(...)}} {{ttb|:}} {{spar|ref}}{{ttb|(}} {{spar|target}}{{ttb|)}} {...} }}
+
{{spar|Class}}{{ttb|::}}{{spar|Class(...)}} {{ttb|:}} {{spar|ref}}{{ttb|(}} {{spar|target}}{{ttb|)}} {...}<!-- -->}}
 
{{sdsc end}}
 
{{sdsc end}}
  
Line 43: Line 43:
  
 
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|return}} statement, when the function returns a reference type
@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:
 
The effects of reference initialization are:
  
* If the initializer is a braced-init-list {{ttb|{}} {{spar|arg1, arg2, ...}} {{ttb|} }}, rules of {{rlp|list initialization}} are followed.
+
* If the initializer is a braced-init-list {{ttb|{}} {{spar|arg1, arg2, ...}} {{ttb|}<!-- -->}}, rules of {{rlp|list initialization}} are followed.
  
 
* Otherwise, if the reference is an lvalue reference:
 
* Otherwise, if the reference is an lvalue reference:
Line 60: Line 60:
 
double& rd = d;        // rd refers to d
 
double& rd = d;        // rd refers to d
 
const double& rcd = d; // rcd refers to d
 
const double& rcd = d; // rcd refers to d
 +
 
struct A {};
 
struct A {};
 
struct B : A {} b;
 
struct B : A {} b;
 +
 
A& ra = b;            // ra refers to A subobject in b
 
A& ra = b;            // ra refers to A subobject in b
 
const A& rca = b;      // rca refers to A subobject in b
 
const A& rca = b;      // rca refers to A subobject in b
Line 70: Line 72:
 
struct A {};
 
struct A {};
 
struct B : A { operator int&(); };
 
struct B : A { operator int&(); };
 +
 
int& ir = B(); // ir refers to the result of B::operator int&
 
int& ir = B(); // ir refers to the result of B::operator int&
 
}}
 
}}
Line 75: Line 78:
 
* 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 is lvalue reference to a non-volatile const-qualified type {{rev inl|since=c++11| or rvalue reference}}:
  
:* 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)}}.
+
:* 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)}}.
 
{{source|1=
 
{{source|1=
 
struct A {};
 
struct A {};
 
struct B : A {};
 
struct B : A {};
 
extern B f();
 
extern B f();
 +
 
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
 
const A& rca2 = f(); // bound to the A subobject of the B rvalue.
 
A&& rra = f();      // same as above
 
A&& rra = f();      // same as above
Line 87: Line 91:
 
}}
 
}}
  
:* 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)}}.
+
:* 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)}}.
 
{{source|1=
 
{{source|1=
 
struct A {};
 
struct A {};
 
struct B : A {};
 
struct B : A {};
 
struct X { operator B(); } x;
 
struct X { operator B(); } x;
 +
 
const A& r = x; // bound to the A subobject of the result of the conversion
 
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
 
B&& rrb = x;    // bound directly to the result of the conversion
 
}}
 
}}
  
:* 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}}.
+
:* 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}}.
 
{{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 105: Line 110:
  
 
===Lifetime of a temporary===
 
===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 {{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 {{rlp|value category#prvalue|prvalue}} expression of an object type,
Line 128: Line 133:
 
* 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 direct-initialization syntax (parentheses) as opposed to list-initialization syntax (braces) exists until the end of the full expression containing the initializer.
 
{{source|1=
 
{{source|1=
struct A {
+
struct A
  int&& r;
+
{
 +
    int&& r;
 
};
 
};
 +
 
A a1{7}; // OK, lifetime is extended
 
A a1{7}; // OK, lifetime is extended
 
A a2(7); // well-formed, but dangling reference
 
A a2(7); // well-formed, but dangling reference
Line 145: Line 152:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|
| code=
+
|code=
 
#include <utility>
 
#include <utility>
 
#include <sstream>
 
#include <sstream>
  
struct S {
+
struct S
 +
{
 
     int mi;
 
     int mi;
 
     const std::pair<int, int>& mp; // reference member
 
     const std::pair<int, int>& mp; // reference member
Line 159: Line 167:
 
struct A {};
 
struct A {};
  
struct B : A {
+
struct B : A
 +
{
 
     int n;
 
     int n;
 
     operator int&() { return n; }
 
     operator int&() { return n; }
Line 169: Line 178:
 
extern int& ext_r; // OK
 
extern int& ext_r; // OK
  
int main() {
+
int main()
 +
{
 
//  Lvalues
 
//  Lvalues
 
     int n = 1;
 
     int n = 1;
Line 205: Line 215:
 
                       // so buf_ref is a dangling reference
 
                       // so 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;
 
}
 
}
| output=
+
|output=
 
}}
 
}}
  
 
===Defect reports===
 
===Defect reports===
 
{{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=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 end}}
 
{{dr list end}}
  
 
===See also===
 
===See also===
* {{rlp | constructor}}
+
* {{rlp|constructor}}
* {{rlp | converting constructor}}
+
* {{rlp|converting constructor}}
* {{rlp | copy assignment}}
+
* {{rlp|copy assignment}}
* {{rlp | copy constructor}}
+
* {{rlp|copy constructor}}
* {{rlp | explicit}}
+
* {{rlp|explicit}}
* {{rlp | initialization}}
+
* {{rlp|initialization}}
** {{rlp | constant initialization}}
+
** {{rlp|constant initialization}}
** {{rlp | copy initialization}}
+
** {{rlp|copy initialization}}
** {{rlp | direct initialization}}
+
** {{rlp|direct initialization}}
** {{rlp | list initialization}}
+
** {{rlp|list initialization}}
* {{rlp | move assignment}}
+
* {{rlp|move assignment}}
* {{rlp | move constructor}}
+
* {{rlp|move constructor}}
* {{rlp | new}}
+
* {{rlp|new}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Revision as of 19:31, 7 February 2022

 
 
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

Syntax

T & ref = target ;

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

T & ref ( target ) ;

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

(1)

T && ref = target ;

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

T && ref ( target ) ;

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

(2) (since C++11)
given R fn ( T & arg ); or R fn ( T && arg );(since C++11)

fn ( target )

fn ( { arg1, arg2, ... } )

(3)
inside T & fn () or T && fn ()(since C++11)

return target ;

(4)
given T & ref ; or T && ref ;(since C++11) inside the definition of Class

Class::Class(...) : ref( target) {...}

(5)

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 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
5) When a non-static data member of reference type is initialized using a member initializer

The effects of reference initialization are:

  • If the initializer is a braced-init-list { arg1, arg2, ... }, 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 from 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.
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 either T or derived from T, 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).
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 from T, 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).
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 a rvalue or a function lvalue whose type is either T or derived from T, 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).
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 of T 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 type T or derived from T, it must be equally or less cv-qualified than T, and, if the reference is an rvalue reference, must not be an lvalue(since C++11).
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

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.
  • 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) as opposed to list-initialization syntax (braces) exists until the end of the full expression containing the initializer.
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.

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

Example

#include <utility>
#include <sstream>
 
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 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;
}

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 1299 C++98 the definition of temporary was unclear made clear

See also