tmp/tmprecvsoyc/{from.md → to.md}
RENAMED
|
@@ -1,36 +1,36 @@
|
|
| 1 |
### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
- when a prvalue is converted to an xvalue [[conv.rval]]
|
| 6 |
- when needed by the implementation to pass or return an object of
|
| 7 |
-
|
| 8 |
-
- when throwing an exception [[except.throw]]. \[*Note 1*: The lifetime
|
| 9 |
-
of exception objects is described in [[except.throw]]. — *end note*]
|
| 10 |
|
| 11 |
Even when the creation of the temporary object is unevaluated
|
| 12 |
[[expr.context]], all the semantic restrictions shall be respected as if
|
| 13 |
the temporary object had been created and later destroyed.
|
| 14 |
|
| 15 |
-
[*Note
|
| 16 |
is deleted, for the constructor selected and for the destructor.
|
| 17 |
However, in the special case of the operand of a *decltype-specifier*
|
| 18 |
[[dcl.type.decltype]], no temporary is introduced, so the foregoing does
|
| 19 |
not apply to such a prvalue. — *end note*]
|
| 20 |
|
| 21 |
The materialization of a temporary object is generally delayed as long
|
| 22 |
as possible in order to avoid creating unnecessary temporary objects.
|
| 23 |
|
| 24 |
-
[*Note
|
| 25 |
|
| 26 |
Temporary objects are materialized:
|
| 27 |
|
| 28 |
- when binding a reference to a prvalue
|
| 29 |
[[dcl.init.ref]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.const.cast]], [[expr.cast]],
|
| 30 |
-
- when performing member
|
| 31 |
[[expr.ref]], [[expr.mptr.oper]],
|
|
|
|
|
|
|
| 32 |
- when performing an array-to-pointer conversion or subscripting on an
|
| 33 |
array prvalue [[conv.array]], [[expr.sub]],
|
| 34 |
- when initializing an object of type `std::initializer_list<T>` from a
|
| 35 |
*braced-init-list* [[dcl.init.list]],
|
| 36 |
- for certain unevaluated operands [[expr.typeid]], [[expr.sizeof]], and
|
|
@@ -78,49 +78,56 @@ result is constructed directly in `c`. On the other hand, the expression
|
|
| 78 |
materialized so that the reference parameter of `X::operator=(const X&)`
|
| 79 |
can bind to it.
|
| 80 |
|
| 81 |
— *end example*]
|
| 82 |
|
| 83 |
-
When an object of
|
| 84 |
-
function, if `X`
|
| 85 |
-
[[special]], each such constructor is trivial, and the destructor of `X`
|
| 86 |
-
is either trivial or deleted, implementations are permitted to create a
|
| 87 |
-
temporary object to hold the function parameter or result object. The
|
| 88 |
-
temporary object is constructed from the function argument or return
|
| 89 |
-
value, respectively, and the function’s parameter or return object is
|
| 90 |
-
initialized as if by using the eligible trivial constructor to copy the
|
| 91 |
-
temporary (even if that constructor is inaccessible or would not be
|
| 92 |
-
selected by overload resolution to perform a copy or move of the
|
| 93 |
-
object).
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
it shall ensure that a constructor is called for the temporary object.
|
| 101 |
-
Similarly, the destructor shall be called for a temporary with a
|
| 102 |
-
non-trivial destructor [[class.dtor]]. Temporary objects are destroyed
|
| 103 |
-
as the last step in evaluating the full-expression [[intro.execution]]
|
| 104 |
-
that (lexically) contains the point where they were created. This is
|
| 105 |
-
true even if that evaluation ends in throwing an exception. The value
|
| 106 |
-
computations and side effects of destroying a temporary object are
|
| 107 |
-
associated only with the full-expression, not with any specific
|
| 108 |
-
subexpression.
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
[[expr.prim.lambda.capture]], [[class.copy.ctor]]. In either case, if
|
| 117 |
the constructor has one or more default arguments, the destruction of
|
| 118 |
every temporary created in a default argument is sequenced before the
|
| 119 |
construction of the next array element, if any.
|
| 120 |
|
| 121 |
-
The third context is when a reference binds to a temporary object.[^
|
| 122 |
|
| 123 |
The temporary object to which the reference is bound or the temporary
|
| 124 |
object that is the complete object of a subobject to which the reference
|
| 125 |
is bound persists for the lifetime of the reference if the glvalue to
|
| 126 |
which the reference is bound was obtained through one of the following:
|
|
@@ -162,11 +169,11 @@ int&& c = cond ? id<int[3]>{1, 2, 3}[i] : static_cast<int&&>(0);
|
|
| 162 |
// exactly one of the two temporaries is lifetime-extended
|
| 163 |
```
|
| 164 |
|
| 165 |
— *end example*]
|
| 166 |
|
| 167 |
-
[*Note
|
| 168 |
|
| 169 |
An explicit type conversion [[expr.type.conv]], [[expr.cast]] is
|
| 170 |
interpreted as a sequence of elementary casts, covered above.
|
| 171 |
|
| 172 |
[*Example 3*:
|
|
@@ -177,11 +184,11 @@ const int& x = (const int&)1; // temporary for value 1 has same lifetime as x
|
|
| 177 |
|
| 178 |
— *end example*]
|
| 179 |
|
| 180 |
— *end note*]
|
| 181 |
|
| 182 |
-
[*Note
|
| 183 |
|
| 184 |
If a temporary object has a reference member initialized by another
|
| 185 |
temporary object, lifetime extension applies recursively to such a
|
| 186 |
member’s initializer.
|
| 187 |
|
|
@@ -205,43 +212,52 @@ The exceptions to this lifetime rule are:
|
|
| 205 |
containing the call.
|
| 206 |
- A temporary object bound to a reference element of an aggregate of
|
| 207 |
class type initialized from a parenthesized *expression-list*
|
| 208 |
[[dcl.init]] persists until the completion of the full-expression
|
| 209 |
containing the *expression-list*.
|
| 210 |
-
- The lifetime of a temporary bound to the returned value in a function
|
| 211 |
-
`return` statement [[stmt.return]] is not extended; the temporary is
|
| 212 |
-
destroyed at the end of the full-expression in the `return` statement.
|
| 213 |
- A temporary bound to a reference in a *new-initializer* [[expr.new]]
|
| 214 |
persists until the completion of the full-expression containing the
|
| 215 |
*new-initializer*.
|
| 216 |
-
\[*Note
|
| 217 |
\[*Example 5*:
|
| 218 |
``` cpp
|
| 219 |
struct S { int mi; const std::pair<int,int>& mp; };
|
| 220 |
S a { 1, {2,3} };
|
| 221 |
S* p = new S{ 1, {2,3} }; // creates dangling reference
|
| 222 |
```
|
| 223 |
|
| 224 |
— *end example*]
|
| 225 |
|
| 226 |
-
The fourth context is when a temporary object
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
be destroyed at the end of the
|
| 230 |
-
the object persists for the
|
| 231 |
-
*for-range-initializer*.
|
| 232 |
|
| 233 |
-
The
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
[[basic.stc.static]], [[basic.stc.thread]], [[basic.stc.auto]]; that is,
|
| 244 |
if `obj1` is an object with the same storage duration as the temporary
|
| 245 |
and created before the temporary is created the temporary shall be
|
| 246 |
destroyed before `obj1` is destroyed; if `obj2` is an object with the
|
| 247 |
same storage duration as the temporary and created after the temporary
|
|
|
|
| 1 |
### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
|
| 2 |
|
| 3 |
+
A *temporary object* is an object created
|
| 4 |
|
| 5 |
+
- when a prvalue is converted to an xvalue [[conv.rval]] and
|
| 6 |
- when needed by the implementation to pass or return an object of
|
| 7 |
+
suitable type (see below).
|
|
|
|
|
|
|
| 8 |
|
| 9 |
Even when the creation of the temporary object is unevaluated
|
| 10 |
[[expr.context]], all the semantic restrictions shall be respected as if
|
| 11 |
the temporary object had been created and later destroyed.
|
| 12 |
|
| 13 |
+
[*Note 1*: This includes accessibility [[class.access]] and whether it
|
| 14 |
is deleted, for the constructor selected and for the destructor.
|
| 15 |
However, in the special case of the operand of a *decltype-specifier*
|
| 16 |
[[dcl.type.decltype]], no temporary is introduced, so the foregoing does
|
| 17 |
not apply to such a prvalue. — *end note*]
|
| 18 |
|
| 19 |
The materialization of a temporary object is generally delayed as long
|
| 20 |
as possible in order to avoid creating unnecessary temporary objects.
|
| 21 |
|
| 22 |
+
[*Note 2*:
|
| 23 |
|
| 24 |
Temporary objects are materialized:
|
| 25 |
|
| 26 |
- when binding a reference to a prvalue
|
| 27 |
[[dcl.init.ref]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.const.cast]], [[expr.cast]],
|
| 28 |
+
- when performing certain member accesses on a class prvalue
|
| 29 |
[[expr.ref]], [[expr.mptr.oper]],
|
| 30 |
+
- when invoking an implicit object member function on a class prvalue
|
| 31 |
+
[[expr.call]],
|
| 32 |
- when performing an array-to-pointer conversion or subscripting on an
|
| 33 |
array prvalue [[conv.array]], [[expr.sub]],
|
| 34 |
- when initializing an object of type `std::initializer_list<T>` from a
|
| 35 |
*braced-init-list* [[dcl.init.list]],
|
| 36 |
- for certain unevaluated operands [[expr.typeid]], [[expr.sizeof]], and
|
|
|
|
| 78 |
materialized so that the reference parameter of `X::operator=(const X&)`
|
| 79 |
can bind to it.
|
| 80 |
|
| 81 |
— *end example*]
|
| 82 |
|
| 83 |
+
When an object of type `X` is passed to or returned from a
|
| 84 |
+
potentially-evaluated function call, if `X` is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
- a scalar type or
|
| 87 |
+
- a class type that has at least one eligible copy or move constructor
|
| 88 |
+
[[special]], where each such constructor is trivial, and the
|
| 89 |
+
destructor of `X` is either trivial or deleted,
|
| 90 |
|
| 91 |
+
implementations are permitted to create temporary objects to hold the
|
| 92 |
+
function parameter or result object, as follows:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
- The first such temporary object is constructed from the function
|
| 95 |
+
argument or return value, respectively.
|
| 96 |
+
- Each successive temporary object is initialized from the previous one
|
| 97 |
+
as if by direct-initialization if `X` is a scalar type, otherwise by
|
| 98 |
+
using an eligible trivial constructor.
|
| 99 |
+
- The function parameter or return object is initialized from the final
|
| 100 |
+
temporary as if by direct-initialization if `X` is a scalar type,
|
| 101 |
+
otherwise by using an eligible trivial constructor.
|
| 102 |
+
|
| 103 |
+
(In all cases, the eligible constructor is used even if that constructor
|
| 104 |
+
is inaccessible or would not be selected by overload resolution to
|
| 105 |
+
perform a copy or move of the object).
|
| 106 |
+
|
| 107 |
+
[*Note 3*: This latitude is granted to allow objects to be passed to or
|
| 108 |
+
returned from functions in registers. — *end note*]
|
| 109 |
+
|
| 110 |
+
Temporary objects are destroyed as the last step in evaluating the
|
| 111 |
+
full-expression [[intro.execution]] that (lexically) contains the point
|
| 112 |
+
where they were created. This is true even if that evaluation ends in
|
| 113 |
+
throwing an exception. The value computations and side effects of
|
| 114 |
+
destroying a temporary object are associated only with the
|
| 115 |
+
full-expression, not with any specific subexpression.
|
| 116 |
+
|
| 117 |
+
Temporary objects are destroyed at a different point than the end of the
|
| 118 |
+
full-expression in the following contexts: The first context is when a
|
| 119 |
+
default constructor is called to initialize an element of an array with
|
| 120 |
+
no corresponding initializer [[dcl.init]]. The second context is when a
|
| 121 |
+
copy constructor is called to copy an element of an array while the
|
| 122 |
+
entire array is copied
|
| 123 |
[[expr.prim.lambda.capture]], [[class.copy.ctor]]. In either case, if
|
| 124 |
the constructor has one or more default arguments, the destruction of
|
| 125 |
every temporary created in a default argument is sequenced before the
|
| 126 |
construction of the next array element, if any.
|
| 127 |
|
| 128 |
+
The third context is when a reference binds to a temporary object.[^11]
|
| 129 |
|
| 130 |
The temporary object to which the reference is bound or the temporary
|
| 131 |
object that is the complete object of a subobject to which the reference
|
| 132 |
is bound persists for the lifetime of the reference if the glvalue to
|
| 133 |
which the reference is bound was obtained through one of the following:
|
|
|
|
| 169 |
// exactly one of the two temporaries is lifetime-extended
|
| 170 |
```
|
| 171 |
|
| 172 |
— *end example*]
|
| 173 |
|
| 174 |
+
[*Note 4*:
|
| 175 |
|
| 176 |
An explicit type conversion [[expr.type.conv]], [[expr.cast]] is
|
| 177 |
interpreted as a sequence of elementary casts, covered above.
|
| 178 |
|
| 179 |
[*Example 3*:
|
|
|
|
| 184 |
|
| 185 |
— *end example*]
|
| 186 |
|
| 187 |
— *end note*]
|
| 188 |
|
| 189 |
+
[*Note 5*:
|
| 190 |
|
| 191 |
If a temporary object has a reference member initialized by another
|
| 192 |
temporary object, lifetime extension applies recursively to such a
|
| 193 |
member’s initializer.
|
| 194 |
|
|
|
|
| 212 |
containing the call.
|
| 213 |
- A temporary object bound to a reference element of an aggregate of
|
| 214 |
class type initialized from a parenthesized *expression-list*
|
| 215 |
[[dcl.init]] persists until the completion of the full-expression
|
| 216 |
containing the *expression-list*.
|
|
|
|
|
|
|
|
|
|
| 217 |
- A temporary bound to a reference in a *new-initializer* [[expr.new]]
|
| 218 |
persists until the completion of the full-expression containing the
|
| 219 |
*new-initializer*.
|
| 220 |
+
\[*Note 6*: This might introduce a dangling reference. — *end note*]
|
| 221 |
\[*Example 5*:
|
| 222 |
``` cpp
|
| 223 |
struct S { int mi; const std::pair<int,int>& mp; };
|
| 224 |
S a { 1, {2,3} };
|
| 225 |
S* p = new S{ 1, {2,3} }; // creates dangling reference
|
| 226 |
```
|
| 227 |
|
| 228 |
— *end example*]
|
| 229 |
|
| 230 |
+
The fourth context is when a temporary object is created in the
|
| 231 |
+
*for-range-initializer* of either a range-based `for` statement or an
|
| 232 |
+
enumerating expansion statement [[stmt.expand]]. If such a temporary
|
| 233 |
+
object would otherwise be destroyed at the end of the
|
| 234 |
+
*for-range-initializer* full-expression, the object persists for the
|
| 235 |
+
lifetime of the reference initialized by the *for-range-initializer*.
|
| 236 |
|
| 237 |
+
The fifth context is when a temporary object is created in the
|
| 238 |
+
*expansion-initializer* of an iterating or destructuring expansion
|
| 239 |
+
statement. If such a temporary object would otherwise be destroyed at
|
| 240 |
+
the end of that *expansion-initializer*, the object persists for the
|
| 241 |
+
lifetime of the reference initialized by the *expansion-initializer*, if
|
| 242 |
+
any.
|
| 243 |
+
|
| 244 |
+
The sixth context is when a temporary object is created in a structured
|
| 245 |
+
binding declaration [[dcl.struct.bind]]. Any temporary objects
|
| 246 |
+
introduced by the *initializer*s for the variables with unique names are
|
| 247 |
+
destroyed at the end of the structured binding declaration.
|
| 248 |
+
|
| 249 |
+
Let `x` and `y` each be either a temporary object whose lifetime is not
|
| 250 |
+
extended, or a function parameter. If the lifetimes of `x` and `y` end
|
| 251 |
+
at the end of the same full-expression, and `x` is initialized before
|
| 252 |
+
`y`, then the destruction of `y` is sequenced before that of `x`. If the
|
| 253 |
+
lifetime of two or more temporaries with lifetimes extending beyond the
|
| 254 |
+
full-expressions in which they were created ends at the same point,
|
| 255 |
+
these temporaries are destroyed at that point in the reverse order of
|
| 256 |
+
the completion of their construction. In addition, the destruction of
|
| 257 |
+
such temporaries shall take into account the ordering of destruction of
|
| 258 |
+
objects with static, thread, or automatic storage duration
|
| 259 |
[[basic.stc.static]], [[basic.stc.thread]], [[basic.stc.auto]]; that is,
|
| 260 |
if `obj1` is an object with the same storage duration as the temporary
|
| 261 |
and created before the temporary is created the temporary shall be
|
| 262 |
destroyed before `obj1` is destroyed; if `obj2` is an object with the
|
| 263 |
same storage duration as the temporary and created after the temporary
|