tmp/tmpn_l82w2l/{from.md → to.md}
RENAMED
|
@@ -5,34 +5,45 @@ In a declaration `T` `D` where `D` has either of the forms
|
|
| 5 |
``` bnf
|
| 6 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 7 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 8 |
```
|
| 9 |
|
| 10 |
-
and the type of the identifier in the declaration `T` `D1` is
|
| 11 |
-
then the type of the identifier of
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
``` cpp
|
| 19 |
typedef int& A;
|
| 20 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 21 |
```
|
| 22 |
|
| 23 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 24 |
-
to `const int`”.
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
ill-formed.
|
| 27 |
|
| 28 |
A reference type that is declared using `&` is called an *lvalue
|
| 29 |
reference*, and a reference type that is declared using `&&` is called
|
| 30 |
an *rvalue reference*. Lvalue references and rvalue references are
|
| 31 |
distinct types. Except where explicitly noted, they are semantically
|
| 32 |
equivalent and commonly referred to as references.
|
| 33 |
|
|
|
|
|
|
|
| 34 |
``` cpp
|
| 35 |
void f(double& a) { a += 3.14; }
|
| 36 |
// ...
|
| 37 |
double d = 0;
|
| 38 |
f(d);
|
|
@@ -73,33 +84,42 @@ void k() {
|
|
| 73 |
```
|
| 74 |
|
| 75 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 76 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 77 |
|
|
|
|
|
|
|
| 78 |
It is unspecified whether or not a reference requires storage (
|
| 79 |
[[basic.stc]]).
|
| 80 |
|
| 81 |
There shall be no references to references, no arrays of references, and
|
| 82 |
no pointers to references. The declaration of a reference shall contain
|
| 83 |
an *initializer* ([[dcl.init.ref]]) except when the declaration
|
| 84 |
contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
| 85 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 86 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 87 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 88 |
-
object or function.
|
|
|
|
|
|
|
| 89 |
well-defined program, because the only way to create such a reference
|
| 90 |
would be to bind it to the “object” obtained by indirection through a
|
| 91 |
null pointer, which causes undefined behavior. As described in
|
| 92 |
-
[[class.bit]], a reference cannot be bound directly to a
|
|
|
|
| 93 |
|
| 94 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 95 |
*decltype-specifier* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 96 |
a reference to a type `T`, an attempt to create the type “lvalue
|
| 97 |
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 98 |
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 99 |
type `TR`.
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
``` cpp
|
| 102 |
int i;
|
| 103 |
typedef int& LRI;
|
| 104 |
typedef int&& RRI;
|
| 105 |
|
|
@@ -112,8 +132,11 @@ RRI&& r5 = 5; // r5 has the type int&&
|
|
| 112 |
|
| 113 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 114 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 115 |
```
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
|
|
|
| 5 |
``` bnf
|
| 6 |
'&' attribute-specifier-seqₒₚₜ 'D1'
|
| 7 |
'&&' attribute-specifier-seqₒₚₜ 'D1'
|
| 8 |
```
|
| 9 |
|
| 10 |
+
and the type of the identifier in the declaration `T` `D1` is
|
| 11 |
+
“*derived-declarator-type-list* `T`”, then the type of the identifier of
|
| 12 |
+
`D` is “*derived-declarator-type-list* reference to `T`”. The optional
|
| 13 |
+
*attribute-specifier-seq* appertains to the reference type. Cv-qualified
|
| 14 |
+
references are ill-formed except when the cv-qualifiers are introduced
|
| 15 |
+
through the use of a *typedef-name* ([[dcl.typedef]], [[temp.param]])
|
| 16 |
+
or *decltype-specifier* ([[dcl.type.simple]]), in which case the
|
| 17 |
+
cv-qualifiers are ignored.
|
| 18 |
+
|
| 19 |
+
[*Example 1*:
|
| 20 |
|
| 21 |
``` cpp
|
| 22 |
typedef int& A;
|
| 23 |
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue
|
| 24 |
```
|
| 25 |
|
| 26 |
The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
|
| 27 |
+
to `const int`”.
|
| 28 |
+
|
| 29 |
+
— *end example*]
|
| 30 |
+
|
| 31 |
+
[*Note 1*: A reference can be thought of as a name of an
|
| 32 |
+
object. — *end note*]
|
| 33 |
+
|
| 34 |
+
A declarator that specifies the type “reference to cv `void`” is
|
| 35 |
ill-formed.
|
| 36 |
|
| 37 |
A reference type that is declared using `&` is called an *lvalue
|
| 38 |
reference*, and a reference type that is declared using `&&` is called
|
| 39 |
an *rvalue reference*. Lvalue references and rvalue references are
|
| 40 |
distinct types. Except where explicitly noted, they are semantically
|
| 41 |
equivalent and commonly referred to as references.
|
| 42 |
|
| 43 |
+
[*Example 2*:
|
| 44 |
+
|
| 45 |
``` cpp
|
| 46 |
void f(double& a) { a += 3.14; }
|
| 47 |
// ...
|
| 48 |
double d = 0;
|
| 49 |
f(d);
|
|
|
|
| 84 |
```
|
| 85 |
|
| 86 |
declares `p` to be a reference to a pointer to `link` so `h(q)` will
|
| 87 |
leave `q` with the value zero. See also [[dcl.init.ref]].
|
| 88 |
|
| 89 |
+
— *end example*]
|
| 90 |
+
|
| 91 |
It is unspecified whether or not a reference requires storage (
|
| 92 |
[[basic.stc]]).
|
| 93 |
|
| 94 |
There shall be no references to references, no arrays of references, and
|
| 95 |
no pointers to references. The declaration of a reference shall contain
|
| 96 |
an *initializer* ([[dcl.init.ref]]) except when the declaration
|
| 97 |
contains an explicit `extern` specifier ([[dcl.stc]]), is a class
|
| 98 |
member ([[class.mem]]) declaration within a class definition, or is the
|
| 99 |
declaration of a parameter or a return type ([[dcl.fct]]); see
|
| 100 |
[[basic.def]]. A reference shall be initialized to refer to a valid
|
| 101 |
+
object or function.
|
| 102 |
+
|
| 103 |
+
[*Note 2*: In particular, a null reference cannot exist in a
|
| 104 |
well-defined program, because the only way to create such a reference
|
| 105 |
would be to bind it to the “object” obtained by indirection through a
|
| 106 |
null pointer, which causes undefined behavior. As described in
|
| 107 |
+
[[class.bit]], a reference cannot be bound directly to a
|
| 108 |
+
bit-field. — *end note*]
|
| 109 |
|
| 110 |
If a *typedef-name* ([[dcl.typedef]], [[temp.param]]) or a
|
| 111 |
*decltype-specifier* ([[dcl.type.simple]]) denotes a type `TR` that is
|
| 112 |
a reference to a type `T`, an attempt to create the type “lvalue
|
| 113 |
reference to cv `TR`” creates the type “lvalue reference to `T`”, while
|
| 114 |
an attempt to create the type “rvalue reference to cv `TR`” creates the
|
| 115 |
type `TR`.
|
| 116 |
|
| 117 |
+
[*Note 3*: This rule is known as reference collapsing. — *end note*]
|
| 118 |
+
|
| 119 |
+
[*Example 3*:
|
| 120 |
+
|
| 121 |
``` cpp
|
| 122 |
int i;
|
| 123 |
typedef int& LRI;
|
| 124 |
typedef int&& RRI;
|
| 125 |
|
|
|
|
| 132 |
|
| 133 |
decltype(r2)& r6 = i; // r6 has the type int&
|
| 134 |
decltype(r2)&& r7 = i; // r7 has the type int&
|
| 135 |
```
|
| 136 |
|
| 137 |
+
— *end example*]
|
| 138 |
+
|
| 139 |
+
[*Note 4*: Forming a reference to function type is ill-formed if the
|
| 140 |
+
function type has *cv-qualifier*s or a *ref-qualifier*; see
|
| 141 |
+
[[dcl.fct]]. — *end note*]
|
| 142 |
|