tmp/tmpik31x30m/{from.md → to.md}
RENAMED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
#### New <a id="expr.new">[[expr.new]]</a>
|
| 2 |
|
| 3 |
-
The *new-expression* attempts to create an object of the *type-id*
|
| 4 |
-
|
| 5 |
object is the *allocated type*. This type shall be a complete object
|
| 6 |
type [[term.incomplete.type]], but not an abstract class type
|
| 7 |
[[class.abstract]] or array thereof [[intro.object]].
|
| 8 |
|
| 9 |
[*Note 1*: Because references are not objects, references cannot be
|
|
@@ -45,17 +45,17 @@ noptr-new-declarator:
|
|
| 45 |
new-initializer:
|
| 46 |
'(' expression-listₒₚₜ ')'
|
| 47 |
braced-init-list
|
| 48 |
```
|
| 49 |
|
| 50 |
-
If a placeholder type [[dcl.spec.auto]]
|
| 51 |
-
|
| 52 |
-
*new-
|
| 53 |
-
|
| 54 |
-
*type-id* of the *new-expression*, then
|
| 55 |
-
deduced for the variable `x` in the
|
| 56 |
-
[[dcl.spec.auto]]:
|
| 57 |
|
| 58 |
``` cpp
|
| 59 |
T x init ;
|
| 60 |
```
|
| 61 |
|
|
@@ -127,34 +127,41 @@ converted constant expression [[expr.const]] of type `std::size_t` and
|
|
| 127 |
its value shall be greater than zero.
|
| 128 |
|
| 129 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 130 |
well-formed (because `n` is the *expression* of a
|
| 131 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 132 |
-
`n` is not a constant expression).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 135 |
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 136 |
object is an array with `n` elements, where `n` is determined from the
|
| 137 |
number of initial elements supplied in the *new-initializer*
|
| 138 |
[[dcl.init.aggr]], [[dcl.init.string]].
|
| 139 |
|
| 140 |
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 141 |
-
implicitly converted to `std::size_t`. The *expression* is
|
|
|
|
| 142 |
|
| 143 |
- the expression is of non-class type and its value before converting to
|
| 144 |
`std::size_t` is less than zero;
|
| 145 |
- the expression is of class type and its value before application of
|
| 146 |
-
the second standard conversion [[over.ics.user]][^
|
| 147 |
zero;
|
| 148 |
- its value is such that the size of the allocated object would exceed
|
| 149 |
the *implementation-defined* limit [[implimits]]; or
|
| 150 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 151 |
elements for which initializers are provided (including the
|
| 152 |
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 153 |
number of elements to initialize.
|
| 154 |
|
| 155 |
-
If the *expression* is
|
|
|
|
| 156 |
|
| 157 |
- if the *expression* is a potentially-evaluated core constant
|
| 158 |
expression, the program is ill-formed;
|
| 159 |
- otherwise, an allocation function is not called; instead
|
| 160 |
- if the allocation function that would have been called has a
|
|
@@ -166,24 +173,34 @@ If the *expression* is erroneous after converting to `std::size_t`:
|
|
| 166 |
`std::bad_array_new_length` [[new.badlength]].
|
| 167 |
|
| 168 |
When the value of the *expression* is zero, the allocation function is
|
| 169 |
called to allocate an array with no elements.
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
Objects created by a *new-expression* have dynamic storage duration
|
| 172 |
[[basic.stc.dynamic]].
|
| 173 |
|
| 174 |
-
[*Note
|
| 175 |
restricted to the scope in which it is created. — *end note*]
|
| 176 |
|
| 177 |
When the allocated type is “array of `N` `T`” (that is, the
|
| 178 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 179 |
denotes an array type), the *new-expression* yields a prvalue of type
|
| 180 |
“pointer to `T`” that points to the initial element (if any) of the
|
| 181 |
array. Otherwise, let `T` be the allocated type; the *new-expression* is
|
| 182 |
a prvalue of type “pointer to T” that points to the object created.
|
| 183 |
|
| 184 |
-
[*Note
|
| 185 |
type of `new int[i][10]` is `int (*)[10]`. — *end note*]
|
| 186 |
|
| 187 |
A *new-expression* may obtain storage for the object by calling an
|
| 188 |
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 189 |
*new-expression* terminates by throwing an exception, it may release
|
|
@@ -192,11 +209,11 @@ storage by calling a deallocation function
|
|
| 192 |
type, the allocation function’s name is `operator new` and the
|
| 193 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 194 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 195 |
the deallocation function’s name is `operator delete[]`.
|
| 196 |
|
| 197 |
-
[*Note
|
| 198 |
for the global allocation functions
|
| 199 |
[[basic.stc.dynamic]], [[new.delete.single]], [[new.delete.array]]. A
|
| 200 |
C++ program can provide alternative definitions of these functions
|
| 201 |
[[replacement.functions]] and/or class-specific versions [[class.free]].
|
| 202 |
The set of allocation and deallocation functions that can be called by a
|
|
@@ -213,16 +230,12 @@ global scope.
|
|
| 213 |
An implementation is allowed to omit a call to a replaceable global
|
| 214 |
allocation function [[new.delete.single]], [[new.delete.array]]. When it
|
| 215 |
does so, the storage is instead provided by the implementation or
|
| 216 |
provided by extending the allocation of another *new-expression*.
|
| 217 |
|
| 218 |
-
During an evaluation of a constant expression, a call to
|
| 219 |
-
function is always omitted.
|
| 220 |
-
|
| 221 |
-
[*Note 8*: Only *new-expression*s that would otherwise result in a call
|
| 222 |
-
to a replaceable global allocation function can be evaluated in constant
|
| 223 |
-
expressions [[expr.const]]. — *end note*]
|
| 224 |
|
| 225 |
The implementation may extend the allocation of a *new-expression* `e1`
|
| 226 |
to provide storage for a *new-expression* `e2` if the following would be
|
| 227 |
true were the allocation not extended:
|
| 228 |
|
|
@@ -361,13 +374,13 @@ not be done, the deallocation function shall not be called, and the
|
|
| 361 |
value of the *new-expression* shall be null.
|
| 362 |
|
| 363 |
[*Note 11*: When the allocation function returns a value other than
|
| 364 |
null, it must be a pointer to a block of storage in which space for the
|
| 365 |
object has been reserved. The block of storage is assumed to be
|
| 366 |
-
appropriately aligned and of the requested size. The
|
| 367 |
-
created object will not necessarily be the same as that
|
| 368 |
-
the object is an array. — *end note*]
|
| 369 |
|
| 370 |
A *new-expression* that creates an object of type `T` initializes that
|
| 371 |
object as follows:
|
| 372 |
|
| 373 |
- If the *new-initializer* is omitted, the object is default-initialized
|
|
@@ -379,18 +392,14 @@ object as follows:
|
|
| 379 |
The invocation of the allocation function is sequenced before the
|
| 380 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 381 |
the allocated object is sequenced before the value computation of the
|
| 382 |
*new-expression*.
|
| 383 |
|
| 384 |
-
If the *new-expression* creates an
|
| 385 |
-
|
| 386 |
-
function, the deallocation function [[basic.stc.dynamic.deallocation]],
|
| 387 |
-
and the constructor [[class.ctor]] selected for the initialization (if
|
| 388 |
-
any). If the *new-expression* creates an array of objects of class type,
|
| 389 |
-
the destructor is potentially invoked [[class.dtor]].
|
| 390 |
|
| 391 |
-
If any part of the object initialization described above[^
|
| 392 |
|
| 393 |
terminates by throwing an exception and a suitable deallocation function
|
| 394 |
can be found, the deallocation function is called to free the memory in
|
| 395 |
which the object was being constructed, after which the exception
|
| 396 |
continues to propagate in the context of the *new-expression*. If no
|
|
@@ -415,11 +424,13 @@ single matching deallocation function, that function will be called;
|
|
| 415 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 416 |
a usual deallocation function and that function, considered as a
|
| 417 |
placement deallocation function, would have been selected as a match for
|
| 418 |
the allocation function, the program is ill-formed. For a non-placement
|
| 419 |
allocation function, the normal deallocation function lookup is used to
|
| 420 |
-
find the matching deallocation function [[expr.delete]].
|
|
|
|
|
|
|
| 421 |
|
| 422 |
[*Example 7*:
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
struct S {
|
|
|
|
| 1 |
#### New <a id="expr.new">[[expr.new]]</a>
|
| 2 |
|
| 3 |
+
The *new-expression* attempts to create an object of the *type-id* or
|
| 4 |
+
*new-type-id* [[dcl.name]] to which it is applied. The type of that
|
| 5 |
object is the *allocated type*. This type shall be a complete object
|
| 6 |
type [[term.incomplete.type]], but not an abstract class type
|
| 7 |
[[class.abstract]] or array thereof [[intro.object]].
|
| 8 |
|
| 9 |
[*Note 1*: Because references are not objects, references cannot be
|
|
|
|
| 45 |
new-initializer:
|
| 46 |
'(' expression-listₒₚₜ ')'
|
| 47 |
braced-init-list
|
| 48 |
```
|
| 49 |
|
| 50 |
+
If a placeholder type [[dcl.spec.auto]] or a placeholder for a deduced
|
| 51 |
+
class type [[dcl.type.class.deduct]] appears in the *type-specifier-seq*
|
| 52 |
+
of a *new-type-id* or *type-id* of a *new-expression*, the allocated
|
| 53 |
+
type is deduced as follows: Let *init* be the *new-initializer*, if any,
|
| 54 |
+
and `T` be the *new-type-id* or *type-id* of the *new-expression*, then
|
| 55 |
+
the allocated type is the type deduced for the variable `x` in the
|
| 56 |
+
invented declaration [[dcl.spec.auto]]:
|
| 57 |
|
| 58 |
``` cpp
|
| 59 |
T x init ;
|
| 60 |
```
|
| 61 |
|
|
|
|
| 127 |
its value shall be greater than zero.
|
| 128 |
|
| 129 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 130 |
well-formed (because `n` is the *expression* of a
|
| 131 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 132 |
+
`n` is not a constant expression). Furthermore, `new float[0]` is
|
| 133 |
+
well-formed (because `0` is the *expression* of a
|
| 134 |
+
*noptr-new-declarator*, where a value of zero results in the allocation
|
| 135 |
+
of an array with no elements), but `new float[n][0]` is ill-formed
|
| 136 |
+
(because `0` is the *constant-expression* of a *noptr-new-declarator*,
|
| 137 |
+
where only values greater than zero are allowed). — *end example*]
|
| 138 |
|
| 139 |
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 140 |
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 141 |
object is an array with `n` elements, where `n` is determined from the
|
| 142 |
number of initial elements supplied in the *new-initializer*
|
| 143 |
[[dcl.init.aggr]], [[dcl.init.string]].
|
| 144 |
|
| 145 |
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 146 |
+
implicitly converted to `std::size_t`. The value of the *expression* is
|
| 147 |
+
invalid if
|
| 148 |
|
| 149 |
- the expression is of non-class type and its value before converting to
|
| 150 |
`std::size_t` is less than zero;
|
| 151 |
- the expression is of class type and its value before application of
|
| 152 |
+
the second standard conversion [[over.ics.user]][^21] is less than
|
| 153 |
zero;
|
| 154 |
- its value is such that the size of the allocated object would exceed
|
| 155 |
the *implementation-defined* limit [[implimits]]; or
|
| 156 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 157 |
elements for which initializers are provided (including the
|
| 158 |
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 159 |
number of elements to initialize.
|
| 160 |
|
| 161 |
+
If the value of the *expression* is invalid after converting to
|
| 162 |
+
`std::size_t`:
|
| 163 |
|
| 164 |
- if the *expression* is a potentially-evaluated core constant
|
| 165 |
expression, the program is ill-formed;
|
| 166 |
- otherwise, an allocation function is not called; instead
|
| 167 |
- if the allocation function that would have been called has a
|
|
|
|
| 173 |
`std::bad_array_new_length` [[new.badlength]].
|
| 174 |
|
| 175 |
When the value of the *expression* is zero, the allocation function is
|
| 176 |
called to allocate an array with no elements.
|
| 177 |
|
| 178 |
+
If the allocated type is an array, the *new-initializer* is a
|
| 179 |
+
*braced-init-list*, and the *expression* is potentially-evaluated and
|
| 180 |
+
not a core constant expression, the semantic constraints of
|
| 181 |
+
copy-initializing a hypothetical element of the array from an empty
|
| 182 |
+
initializer list are checked [[dcl.init.list]].
|
| 183 |
+
|
| 184 |
+
[*Note 5*: The array can contain more elements than there are elements
|
| 185 |
+
in the *braced-init-list*, requiring initialization of the remainder of
|
| 186 |
+
the array elements from an empty initializer list. — *end note*]
|
| 187 |
+
|
| 188 |
Objects created by a *new-expression* have dynamic storage duration
|
| 189 |
[[basic.stc.dynamic]].
|
| 190 |
|
| 191 |
+
[*Note 6*: The lifetime of such an object is not necessarily
|
| 192 |
restricted to the scope in which it is created. — *end note*]
|
| 193 |
|
| 194 |
When the allocated type is “array of `N` `T`” (that is, the
|
| 195 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 196 |
denotes an array type), the *new-expression* yields a prvalue of type
|
| 197 |
“pointer to `T`” that points to the initial element (if any) of the
|
| 198 |
array. Otherwise, let `T` be the allocated type; the *new-expression* is
|
| 199 |
a prvalue of type “pointer to T” that points to the object created.
|
| 200 |
|
| 201 |
+
[*Note 7*: Both `new int` and `new int[10]` have type `int*` and the
|
| 202 |
type of `new int[i][10]` is `int (*)[10]`. — *end note*]
|
| 203 |
|
| 204 |
A *new-expression* may obtain storage for the object by calling an
|
| 205 |
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 206 |
*new-expression* terminates by throwing an exception, it may release
|
|
|
|
| 209 |
type, the allocation function’s name is `operator new` and the
|
| 210 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 211 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 212 |
the deallocation function’s name is `operator delete[]`.
|
| 213 |
|
| 214 |
+
[*Note 8*: An implementation is expected to provide default definitions
|
| 215 |
for the global allocation functions
|
| 216 |
[[basic.stc.dynamic]], [[new.delete.single]], [[new.delete.array]]. A
|
| 217 |
C++ program can provide alternative definitions of these functions
|
| 218 |
[[replacement.functions]] and/or class-specific versions [[class.free]].
|
| 219 |
The set of allocation and deallocation functions that can be called by a
|
|
|
|
| 230 |
An implementation is allowed to omit a call to a replaceable global
|
| 231 |
allocation function [[new.delete.single]], [[new.delete.array]]. When it
|
| 232 |
does so, the storage is instead provided by the implementation or
|
| 233 |
provided by extending the allocation of another *new-expression*.
|
| 234 |
|
| 235 |
+
During an evaluation of a constant expression, a call to a replaceable
|
| 236 |
+
allocation function is always omitted [[expr.const]].
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
The implementation may extend the allocation of a *new-expression* `e1`
|
| 239 |
to provide storage for a *new-expression* `e2` if the following would be
|
| 240 |
true were the allocation not extended:
|
| 241 |
|
|
|
|
| 374 |
value of the *new-expression* shall be null.
|
| 375 |
|
| 376 |
[*Note 11*: When the allocation function returns a value other than
|
| 377 |
null, it must be a pointer to a block of storage in which space for the
|
| 378 |
object has been reserved. The block of storage is assumed to be
|
| 379 |
+
appropriately aligned [[basic.align]] and of the requested size. The
|
| 380 |
+
address of the created object will not necessarily be the same as that
|
| 381 |
+
of the block if the object is an array. — *end note*]
|
| 382 |
|
| 383 |
A *new-expression* that creates an object of type `T` initializes that
|
| 384 |
object as follows:
|
| 385 |
|
| 386 |
- If the *new-initializer* is omitted, the object is default-initialized
|
|
|
|
| 392 |
The invocation of the allocation function is sequenced before the
|
| 393 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 394 |
the allocated object is sequenced before the value computation of the
|
| 395 |
*new-expression*.
|
| 396 |
|
| 397 |
+
If the *new-expression* creates an array of objects of class type, the
|
| 398 |
+
destructor is potentially invoked [[class.dtor]].
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
+
If any part of the object initialization described above[^22]
|
| 401 |
|
| 402 |
terminates by throwing an exception and a suitable deallocation function
|
| 403 |
can be found, the deallocation function is called to free the memory in
|
| 404 |
which the object was being constructed, after which the exception
|
| 405 |
continues to propagate in the context of the *new-expression*. If no
|
|
|
|
| 424 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 425 |
a usual deallocation function and that function, considered as a
|
| 426 |
placement deallocation function, would have been selected as a match for
|
| 427 |
the allocation function, the program is ill-formed. For a non-placement
|
| 428 |
allocation function, the normal deallocation function lookup is used to
|
| 429 |
+
find the matching deallocation function [[expr.delete]]. In any case,
|
| 430 |
+
the matching deallocation function (if any) shall be non-deleted and
|
| 431 |
+
accessible from the point where the *new-expression* appears.
|
| 432 |
|
| 433 |
[*Example 7*:
|
| 434 |
|
| 435 |
``` cpp
|
| 436 |
struct S {
|