- tmp/tmpmoemr8c_/{from.md → to.md} +109 -93
tmp/tmpmoemr8c_/{from.md → to.md}
RENAMED
|
@@ -1,24 +1,24 @@
|
|
| 1 |
-
### New <a id="expr.new">[[expr.new]]</a>
|
| 2 |
|
| 3 |
-
The *new-expression* attempts to create an object of the *type-id*
|
| 4 |
-
[[dcl.name]]
|
| 5 |
object is the *allocated type*. This type shall be a complete object
|
| 6 |
type, but not an abstract class type or array thereof (
|
| 7 |
-
[[intro.object]],
|
| 8 |
|
| 9 |
[*Note 1*: Because references are not objects, references cannot be
|
| 10 |
created by *new-expression*s. — *end note*]
|
| 11 |
|
| 12 |
[*Note 2*: The *type-id* may be a cv-qualified type, in which case the
|
| 13 |
object created by the *new-expression* has a cv-qualified
|
| 14 |
type. — *end note*]
|
| 15 |
|
| 16 |
``` bnf
|
| 17 |
new-expression:
|
| 18 |
-
'::'ₒₚₜ
|
| 19 |
-
'::'ₒₚₜ
|
| 20 |
```
|
| 21 |
|
| 22 |
``` bnf
|
| 23 |
new-placement:
|
| 24 |
'(' expression-list ')'
|
|
@@ -35,37 +35,27 @@ new-declarator:
|
|
| 35 |
noptr-new-declarator
|
| 36 |
```
|
| 37 |
|
| 38 |
``` bnf
|
| 39 |
noptr-new-declarator:
|
| 40 |
-
'[' expression ']' attribute-specifier-seqₒₚₜ
|
| 41 |
noptr-new-declarator '[' constant-expression ']' attribute-specifier-seqₒₚₜ
|
| 42 |
```
|
| 43 |
|
| 44 |
``` bnf
|
| 45 |
new-initializer:
|
| 46 |
'(' expression-listₒₚₜ ')'
|
| 47 |
braced-init-list
|
| 48 |
```
|
| 49 |
|
| 50 |
-
|
| 51 |
-
[[basic.stc.dynamic]]).
|
| 52 |
-
|
| 53 |
-
[*Note 3*: The lifetime of such an entity is not necessarily
|
| 54 |
-
restricted to the scope in which it is created. — *end note*]
|
| 55 |
-
|
| 56 |
-
If the entity is a non-array object, the *new-expression* returns a
|
| 57 |
-
pointer to the object created. If it is an array, the *new-expression*
|
| 58 |
-
returns a pointer to the initial element of the array.
|
| 59 |
-
|
| 60 |
-
If a placeholder type ([[dcl.spec.auto]]) appears in the
|
| 61 |
*type-specifier-seq* of a *new-type-id* or *type-id* of a
|
| 62 |
*new-expression*, the allocated type is deduced as follows: Let *init*
|
| 63 |
be the *new-initializer*, if any, and `T` be the *new-type-id* or
|
| 64 |
*type-id* of the *new-expression*, then the allocated type is the type
|
| 65 |
-
deduced for the variable `x` in the invented declaration
|
| 66 |
-
[[dcl.spec.auto]]
|
| 67 |
|
| 68 |
``` cpp
|
| 69 |
T x init ;
|
| 70 |
```
|
| 71 |
|
|
@@ -82,11 +72,11 @@ auto y = new A{1, 2}; // allocated type is A<int>
|
|
| 82 |
— *end example*]
|
| 83 |
|
| 84 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 85 |
of *new-declarator*s.
|
| 86 |
|
| 87 |
-
[*Note
|
| 88 |
`&`, `&&`, `*`, and `[]` and their expression
|
| 89 |
counterparts. — *end note*]
|
| 90 |
|
| 91 |
[*Example 2*:
|
| 92 |
|
|
@@ -96,11 +86,11 @@ new int * i; // syntax error: parsed as (new int*) i, not as
|
|
| 96 |
|
| 97 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 98 |
|
| 99 |
— *end example*]
|
| 100 |
|
| 101 |
-
[*Note
|
| 102 |
|
| 103 |
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 104 |
effects.
|
| 105 |
|
| 106 |
[*Example 3*:
|
|
@@ -114,11 +104,11 @@ is ill-formed because the binding is
|
|
| 114 |
``` cpp
|
| 115 |
(new int) (*[10])(); // error
|
| 116 |
```
|
| 117 |
|
| 118 |
Instead, the explicitly parenthesized version of the `new` operator can
|
| 119 |
-
be used to create objects of compound types
|
| 120 |
|
| 121 |
``` cpp
|
| 122 |
new (int (*[10])());
|
| 123 |
```
|
| 124 |
|
|
@@ -127,10 +117,19 @@ returning `int`).
|
|
| 127 |
|
| 128 |
— *end example*]
|
| 129 |
|
| 130 |
— *end note*]
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
When the allocated object is an array (that is, the
|
| 133 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 134 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 135 |
initial element (if any) of the array.
|
| 136 |
|
|
@@ -139,65 +138,71 @@ type of `new int[i][10]` is `int (*)[10]` — *end note*]
|
|
| 139 |
|
| 140 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 141 |
the associated array type.
|
| 142 |
|
| 143 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 144 |
-
converted constant expression
|
| 145 |
-
|
| 146 |
-
*noptr-new-declarator* is implicitly converted to `std::size_t`.
|
| 147 |
|
| 148 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 149 |
well-formed (because `n` is the *expression* of a
|
| 150 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 151 |
`n` is not a constant expression). — *end example*]
|
| 152 |
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
- the expression is of non-class type and its value before converting to
|
| 156 |
`std::size_t` is less than zero;
|
| 157 |
- the expression is of class type and its value before application of
|
| 158 |
-
the second standard conversion
|
| 159 |
zero;
|
| 160 |
- its value is such that the size of the allocated object would exceed
|
| 161 |
-
the *implementation-defined* limit
|
| 162 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 163 |
elements for which initializers are provided (including the
|
| 164 |
-
terminating `'\0'` in a string
|
| 165 |
number of elements to initialize.
|
| 166 |
|
| 167 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 168 |
|
| 169 |
- if the *expression* is a core constant expression, the program is
|
| 170 |
ill-formed;
|
| 171 |
- otherwise, an allocation function is not called; instead
|
| 172 |
- if the allocation function that would have been called has a
|
| 173 |
-
non-throwing exception specification
|
| 174 |
-
|
| 175 |
result type;
|
| 176 |
- otherwise, the *new-expression* terminates by throwing an exception
|
| 177 |
-
of a type that would match a handler
|
| 178 |
-
`std::bad_array_new_length`
|
| 179 |
|
| 180 |
When the value of the *expression* is zero, the allocation function is
|
| 181 |
called to allocate an array with no elements.
|
| 182 |
|
| 183 |
A *new-expression* may obtain storage for the object by calling an
|
| 184 |
-
allocation function
|
| 185 |
*new-expression* terminates by throwing an exception, it may release
|
| 186 |
-
storage by calling a deallocation function
|
| 187 |
-
[[basic.stc.dynamic.deallocation]]
|
| 188 |
-
|
| 189 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 190 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 191 |
the deallocation function’s name is `operator delete[]`.
|
| 192 |
|
| 193 |
-
[*Note 7*: An implementation
|
| 194 |
-
global allocation functions ([[basic.stc.dynamic]],
|
| 195 |
-
[[new.delete.single]],
|
| 196 |
-
alternative definitions of these functions
|
| 197 |
-
and/or class-specific versions
|
| 198 |
-
|
| 199 |
include functions that do not perform allocation or deallocation; for
|
| 200 |
example, see [[new.delete.placement]]. — *end note*]
|
| 201 |
|
| 202 |
If the *new-expression* begins with a unary `::` operator, the
|
| 203 |
allocation function’s name is looked up in the global scope. Otherwise,
|
|
@@ -207,13 +212,21 @@ lookup fails to find the name, or if the allocated type is not a class
|
|
| 207 |
type, the allocation function’s name is looked up in the global scope.
|
| 208 |
|
| 209 |
An implementation is allowed to omit a call to a replaceable global
|
| 210 |
allocation function ([[new.delete.single]], [[new.delete.array]]). When
|
| 211 |
it does so, the storage is instead provided by the implementation or
|
| 212 |
-
provided by extending the allocation of another *new-expression*.
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
true were the allocation not extended:
|
| 216 |
|
| 217 |
- the evaluation of `e1` is sequenced before the evaluation of `e2`, and
|
| 218 |
- `e2` is evaluated whenever `e1` obtains storage, and
|
| 219 |
- both `e1` and `e2` invoke the same replaceable global allocation
|
|
@@ -228,20 +241,20 @@ true were the allocation not extended:
|
|
| 228 |
`e1`.
|
| 229 |
|
| 230 |
[*Example 5*:
|
| 231 |
|
| 232 |
``` cpp
|
| 233 |
-
|
| 234 |
// These allocations are safe for merging:
|
| 235 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 236 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
| 237 |
std::unique_ptr<char[]> c{new (std::nothrow) char[x]};
|
| 238 |
|
| 239 |
g(a.get(), b.get(), c.get());
|
| 240 |
}
|
| 241 |
|
| 242 |
-
|
| 243 |
std::unique_ptr<char[]> a{new char[8]};
|
| 244 |
try {
|
| 245 |
// Merging this allocation would change its catch handler.
|
| 246 |
std::unique_ptr<char[]> b{new char[x]};
|
| 247 |
} catch (const std::bad_alloc& e) {
|
|
@@ -256,18 +269,19 @@ true were the allocation not extended:
|
|
| 256 |
When a *new-expression* calls an allocation function and that allocation
|
| 257 |
has not been extended, the *new-expression* passes the amount of space
|
| 258 |
requested to the allocation function as the first argument of type
|
| 259 |
`std::size_t`. That argument shall be no less than the size of the
|
| 260 |
object being created; it may be greater than the size of the object
|
| 261 |
-
being created only if the object is an array
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
| 267 |
|
| 268 |
-
[*Note
|
| 269 |
to storage that is appropriately aligned for objects of any type with
|
| 270 |
fundamental alignment, this constraint on array allocation overhead
|
| 271 |
permits the common idiom of allocating character arrays into which
|
| 272 |
objects of other types will later be placed. — *end note*]
|
| 273 |
|
|
@@ -286,14 +300,19 @@ Overload resolution is performed on a function call created by
|
|
| 286 |
assembling an argument list. The first argument is the amount of space
|
| 287 |
requested, and has type `std::size_t`. If the type of the allocated
|
| 288 |
object has new-extended alignment, the next argument is the type’s
|
| 289 |
alignment, and has type `std::align_val_t`. If the *new-placement*
|
| 290 |
syntax is used, the *initializer-clause*s in its *expression-list* are
|
| 291 |
-
the succeeding arguments. If no matching function is found
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
[*Example 6*:
|
| 297 |
|
| 298 |
- `new T` results in one of the following calls:
|
| 299 |
``` cpp
|
|
@@ -318,69 +337,69 @@ again.
|
|
| 318 |
|
| 319 |
Here, each instance of `x` is a non-negative unspecified value
|
| 320 |
representing array allocation overhead; the result of the
|
| 321 |
*new-expression* will be offset by this amount from the value returned
|
| 322 |
by `operator new[]`. This overhead may be applied in all array
|
| 323 |
-
*new-expression*s, including those referencing
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
to another.
|
| 327 |
|
| 328 |
— *end example*]
|
| 329 |
|
| 330 |
-
[*Note
|
| 331 |
-
specification
|
| 332 |
-
|
| 333 |
-
[[basic.stc.dynamic.allocation]],
|
| 334 |
-
|
| 335 |
-
|
| 336 |
failure to allocate storage and a non-null pointer
|
| 337 |
otherwise. — *end note*]
|
| 338 |
|
| 339 |
-
If the allocation function is a non-allocating form
|
| 340 |
-
[[new.delete.placement]]
|
| 341 |
Otherwise, if the allocation function returns null, initialization shall
|
| 342 |
not be done, the deallocation function shall not be called, and the
|
| 343 |
value of the *new-expression* shall be null.
|
| 344 |
|
| 345 |
-
[*Note
|
| 346 |
null, it must be a pointer to a block of storage in which space for the
|
| 347 |
object has been reserved. The block of storage is assumed to be
|
| 348 |
appropriately aligned and of the requested size. The address of the
|
| 349 |
created object will not necessarily be the same as that of the block if
|
| 350 |
the object is an array. — *end note*]
|
| 351 |
|
| 352 |
A *new-expression* that creates an object of type `T` initializes that
|
| 353 |
object as follows:
|
| 354 |
|
| 355 |
-
- If the *new-initializer* is omitted, the object is
|
| 356 |
-
|
| 357 |
-
|
| 358 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 359 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 360 |
|
| 361 |
The invocation of the allocation function is sequenced before the
|
| 362 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 363 |
the allocated object is sequenced before the value computation of the
|
| 364 |
*new-expression*.
|
| 365 |
|
| 366 |
If the *new-expression* creates an object or an array of objects of
|
| 367 |
class type, access and ambiguity control are done for the allocation
|
| 368 |
-
function, the deallocation function
|
| 369 |
-
|
| 370 |
-
of objects of class type, the
|
| 371 |
-
[[class.dtor]]
|
| 372 |
|
| 373 |
-
If any part of the object initialization described above[^
|
| 374 |
by throwing an exception and a suitable deallocation function can be
|
| 375 |
found, the deallocation function is called to free the memory in which
|
| 376 |
the object was being constructed, after which the exception continues to
|
| 377 |
propagate in the context of the *new-expression*. If no unambiguous
|
| 378 |
matching deallocation function can be found, propagating the exception
|
| 379 |
does not cause the object’s memory to be freed.
|
| 380 |
|
| 381 |
-
[*Note
|
| 382 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 383 |
leak. — *end note*]
|
| 384 |
|
| 385 |
If the *new-expression* begins with a unary `::` operator, the
|
| 386 |
deallocation function’s name is looked up in the global scope.
|
|
@@ -390,20 +409,19 @@ thereof, the deallocation function’s name is looked up in the scope of
|
|
| 390 |
not a class type or array thereof, the deallocation function’s name is
|
| 391 |
looked up in the global scope.
|
| 392 |
|
| 393 |
A declaration of a placement deallocation function matches the
|
| 394 |
declaration of a placement allocation function if it has the same number
|
| 395 |
-
of parameters and, after parameter transformations
|
| 396 |
parameter types except the first are identical. If the lookup finds a
|
| 397 |
single matching deallocation function, that function will be called;
|
| 398 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 399 |
-
a usual deallocation function
|
| 400 |
-
[[basic.stc.dynamic.deallocation]]) and that function, considered as a
|
| 401 |
placement deallocation function, would have been selected as a match for
|
| 402 |
the allocation function, the program is ill-formed. For a non-placement
|
| 403 |
allocation function, the normal deallocation function lookup is used to
|
| 404 |
-
find the matching deallocation function
|
| 405 |
|
| 406 |
[*Example 7*:
|
| 407 |
|
| 408 |
``` cpp
|
| 409 |
struct S {
|
|
@@ -412,23 +430,21 @@ struct S {
|
|
| 412 |
|
| 413 |
// Usual (non-placement) deallocation function:
|
| 414 |
static void operator delete(void*, std::size_t);
|
| 415 |
};
|
| 416 |
|
| 417 |
-
S* p = new (0) S; //
|
| 418 |
// placement allocation function
|
| 419 |
```
|
| 420 |
|
| 421 |
— *end example*]
|
| 422 |
|
| 423 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 424 |
returned from the allocation function call as the first argument of type
|
| 425 |
`void*`. If a placement deallocation function is called, it is passed
|
| 426 |
the same additional arguments as were passed to the placement allocation
|
| 427 |
function, that is, the same arguments as those specified with the
|
| 428 |
-
*new-placement* syntax. If the implementation is allowed to
|
| 429 |
-
of any argument as part of the call to
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
call to the allocation function. If the copy is elided in one place, it
|
| 433 |
-
need not be elided in the other.
|
| 434 |
|
|
|
|
| 1 |
+
#### New <a id="expr.new">[[expr.new]]</a>
|
| 2 |
|
| 3 |
+
The *new-expression* attempts to create an object of the *type-id*
|
| 4 |
+
[[dcl.name]] or *new-type-id* to which it is applied. The type of that
|
| 5 |
object is the *allocated type*. This type shall be a complete object
|
| 6 |
type, but not an abstract class type or array thereof (
|
| 7 |
+
[[intro.object]], [[basic.types]], [[class.abstract]]).
|
| 8 |
|
| 9 |
[*Note 1*: Because references are not objects, references cannot be
|
| 10 |
created by *new-expression*s. — *end note*]
|
| 11 |
|
| 12 |
[*Note 2*: The *type-id* may be a cv-qualified type, in which case the
|
| 13 |
object created by the *new-expression* has a cv-qualified
|
| 14 |
type. — *end note*]
|
| 15 |
|
| 16 |
``` bnf
|
| 17 |
new-expression:
|
| 18 |
+
'::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 19 |
+
'::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
| 20 |
```
|
| 21 |
|
| 22 |
``` bnf
|
| 23 |
new-placement:
|
| 24 |
'(' expression-list ')'
|
|
|
|
| 35 |
noptr-new-declarator
|
| 36 |
```
|
| 37 |
|
| 38 |
``` bnf
|
| 39 |
noptr-new-declarator:
|
| 40 |
+
'[' expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 41 |
noptr-new-declarator '[' constant-expression ']' attribute-specifier-seqₒₚₜ
|
| 42 |
```
|
| 43 |
|
| 44 |
``` bnf
|
| 45 |
new-initializer:
|
| 46 |
'(' expression-listₒₚₜ ')'
|
| 47 |
braced-init-list
|
| 48 |
```
|
| 49 |
|
| 50 |
+
If a placeholder type [[dcl.spec.auto]] appears in the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
*type-specifier-seq* of a *new-type-id* or *type-id* of a
|
| 52 |
*new-expression*, the allocated type is deduced as follows: Let *init*
|
| 53 |
be the *new-initializer*, if any, and `T` be the *new-type-id* or
|
| 54 |
*type-id* of the *new-expression*, then the allocated type is the type
|
| 55 |
+
deduced for the variable `x` in the invented declaration
|
| 56 |
+
[[dcl.spec.auto]]:
|
| 57 |
|
| 58 |
``` cpp
|
| 59 |
T x init ;
|
| 60 |
```
|
| 61 |
|
|
|
|
| 72 |
— *end example*]
|
| 73 |
|
| 74 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 75 |
of *new-declarator*s.
|
| 76 |
|
| 77 |
+
[*Note 3*: This prevents ambiguities between the declarator operators
|
| 78 |
`&`, `&&`, `*`, and `[]` and their expression
|
| 79 |
counterparts. — *end note*]
|
| 80 |
|
| 81 |
[*Example 2*:
|
| 82 |
|
|
|
|
| 86 |
|
| 87 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 88 |
|
| 89 |
— *end example*]
|
| 90 |
|
| 91 |
+
[*Note 4*:
|
| 92 |
|
| 93 |
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 94 |
effects.
|
| 95 |
|
| 96 |
[*Example 3*:
|
|
|
|
| 104 |
``` cpp
|
| 105 |
(new int) (*[10])(); // error
|
| 106 |
```
|
| 107 |
|
| 108 |
Instead, the explicitly parenthesized version of the `new` operator can
|
| 109 |
+
be used to create objects of compound types [[basic.compound]]:
|
| 110 |
|
| 111 |
``` cpp
|
| 112 |
new (int (*[10])());
|
| 113 |
```
|
| 114 |
|
|
|
|
| 117 |
|
| 118 |
— *end example*]
|
| 119 |
|
| 120 |
— *end note*]
|
| 121 |
|
| 122 |
+
Objects created by a *new-expression* have dynamic storage duration
|
| 123 |
+
[[basic.stc.dynamic]].
|
| 124 |
+
|
| 125 |
+
[*Note 5*: The lifetime of such an object is not necessarily
|
| 126 |
+
restricted to the scope in which it is created. — *end note*]
|
| 127 |
+
|
| 128 |
+
When the allocated object is not an array, the result of the
|
| 129 |
+
*new-expression* is a pointer to the object created.
|
| 130 |
+
|
| 131 |
When the allocated object is an array (that is, the
|
| 132 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 133 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 134 |
initial element (if any) of the array.
|
| 135 |
|
|
|
|
| 138 |
|
| 139 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 140 |
the associated array type.
|
| 141 |
|
| 142 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 143 |
+
converted constant expression [[expr.const]] of type `std::size_t` and
|
| 144 |
+
its value shall be greater than zero.
|
|
|
|
| 145 |
|
| 146 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 147 |
well-formed (because `n` is the *expression* of a
|
| 148 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 149 |
`n` is not a constant expression). — *end example*]
|
| 150 |
|
| 151 |
+
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 152 |
+
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 153 |
+
object is an array with `n` elements, where `n` is determined from the
|
| 154 |
+
number of initial elements supplied in the *new-initializer* (
|
| 155 |
+
[[dcl.init.aggr]], [[dcl.init.string]]).
|
| 156 |
+
|
| 157 |
+
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 158 |
+
implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
| 159 |
|
| 160 |
- the expression is of non-class type and its value before converting to
|
| 161 |
`std::size_t` is less than zero;
|
| 162 |
- the expression is of class type and its value before application of
|
| 163 |
+
the second standard conversion [[over.ics.user]][^23] is less than
|
| 164 |
zero;
|
| 165 |
- its value is such that the size of the allocated object would exceed
|
| 166 |
+
the *implementation-defined* limit [[implimits]]; or
|
| 167 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 168 |
elements for which initializers are provided (including the
|
| 169 |
+
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 170 |
number of elements to initialize.
|
| 171 |
|
| 172 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 173 |
|
| 174 |
- if the *expression* is a core constant expression, the program is
|
| 175 |
ill-formed;
|
| 176 |
- otherwise, an allocation function is not called; instead
|
| 177 |
- if the allocation function that would have been called has a
|
| 178 |
+
non-throwing exception specification [[except.spec]], the value of
|
| 179 |
+
the *new-expression* is the null pointer value of the required
|
| 180 |
result type;
|
| 181 |
- otherwise, the *new-expression* terminates by throwing an exception
|
| 182 |
+
of a type that would match a handler [[except.handle]] of type
|
| 183 |
+
`std::bad_array_new_length` [[new.badlength]].
|
| 184 |
|
| 185 |
When the value of the *expression* is zero, the allocation function is
|
| 186 |
called to allocate an array with no elements.
|
| 187 |
|
| 188 |
A *new-expression* may obtain storage for the object by calling an
|
| 189 |
+
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 190 |
*new-expression* terminates by throwing an exception, it may release
|
| 191 |
+
storage by calling a deallocation function
|
| 192 |
+
[[basic.stc.dynamic.deallocation]]. If the allocated type is a non-array
|
| 193 |
+
type, the allocation function’s name is `operator new` and the
|
| 194 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 195 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 196 |
the deallocation function’s name is `operator delete[]`.
|
| 197 |
|
| 198 |
+
[*Note 7*: An implementation is required to provide default definitions
|
| 199 |
+
for the global allocation functions ([[basic.stc.dynamic]],
|
| 200 |
+
[[new.delete.single]], [[new.delete.array]]). A C++ program can provide
|
| 201 |
+
alternative definitions of these functions [[replacement.functions]]
|
| 202 |
+
and/or class-specific versions [[class.free]]. The set of allocation and
|
| 203 |
+
deallocation functions that may be called by a *new-expression* may
|
| 204 |
include functions that do not perform allocation or deallocation; for
|
| 205 |
example, see [[new.delete.placement]]. — *end note*]
|
| 206 |
|
| 207 |
If the *new-expression* begins with a unary `::` operator, the
|
| 208 |
allocation function’s name is looked up in the global scope. Otherwise,
|
|
|
|
| 212 |
type, the allocation function’s name is looked up in the global scope.
|
| 213 |
|
| 214 |
An implementation is allowed to omit a call to a replaceable global
|
| 215 |
allocation function ([[new.delete.single]], [[new.delete.array]]). When
|
| 216 |
it does so, the storage is instead provided by the implementation or
|
| 217 |
+
provided by extending the allocation of another *new-expression*.
|
| 218 |
+
|
| 219 |
+
During an evaluation of a constant expression, a call to an allocation
|
| 220 |
+
function is always omitted.
|
| 221 |
+
|
| 222 |
+
[*Note 8*: Only *new-expression*s that would otherwise result in a call
|
| 223 |
+
to a replaceable global allocation function can be evaluated in constant
|
| 224 |
+
expressions [[expr.const]]. — *end note*]
|
| 225 |
+
|
| 226 |
+
The implementation may extend the allocation of a *new-expression* `e1`
|
| 227 |
+
to provide storage for a *new-expression* `e2` if the following would be
|
| 228 |
true were the allocation not extended:
|
| 229 |
|
| 230 |
- the evaluation of `e1` is sequenced before the evaluation of `e2`, and
|
| 231 |
- `e2` is evaluated whenever `e1` obtains storage, and
|
| 232 |
- both `e1` and `e2` invoke the same replaceable global allocation
|
|
|
|
| 241 |
`e1`.
|
| 242 |
|
| 243 |
[*Example 5*:
|
| 244 |
|
| 245 |
``` cpp
|
| 246 |
+
void can_merge(int x) {
|
| 247 |
// These allocations are safe for merging:
|
| 248 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 249 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
| 250 |
std::unique_ptr<char[]> c{new (std::nothrow) char[x]};
|
| 251 |
|
| 252 |
g(a.get(), b.get(), c.get());
|
| 253 |
}
|
| 254 |
|
| 255 |
+
void cannot_merge(int x) {
|
| 256 |
std::unique_ptr<char[]> a{new char[8]};
|
| 257 |
try {
|
| 258 |
// Merging this allocation would change its catch handler.
|
| 259 |
std::unique_ptr<char[]> b{new char[x]};
|
| 260 |
} catch (const std::bad_alloc& e) {
|
|
|
|
| 269 |
When a *new-expression* calls an allocation function and that allocation
|
| 270 |
has not been extended, the *new-expression* passes the amount of space
|
| 271 |
requested to the allocation function as the first argument of type
|
| 272 |
`std::size_t`. That argument shall be no less than the size of the
|
| 273 |
object being created; it may be greater than the size of the object
|
| 274 |
+
being created only if the object is an array and the allocation function
|
| 275 |
+
is not a non-allocating form [[new.delete.placement]]. For arrays of
|
| 276 |
+
`char`, `unsigned char`, and `std::byte`, the difference between the
|
| 277 |
+
result of the *new-expression* and the address returned by the
|
| 278 |
+
allocation function shall be an integral multiple of the strictest
|
| 279 |
+
fundamental alignment requirement [[basic.align]] of any object type
|
| 280 |
+
whose size is no greater than the size of the array being created.
|
| 281 |
|
| 282 |
+
[*Note 9*: Because allocation functions are assumed to return pointers
|
| 283 |
to storage that is appropriately aligned for objects of any type with
|
| 284 |
fundamental alignment, this constraint on array allocation overhead
|
| 285 |
permits the common idiom of allocating character arrays into which
|
| 286 |
objects of other types will later be placed. — *end note*]
|
| 287 |
|
|
|
|
| 300 |
assembling an argument list. The first argument is the amount of space
|
| 301 |
requested, and has type `std::size_t`. If the type of the allocated
|
| 302 |
object has new-extended alignment, the next argument is the type’s
|
| 303 |
alignment, and has type `std::align_val_t`. If the *new-placement*
|
| 304 |
syntax is used, the *initializer-clause*s in its *expression-list* are
|
| 305 |
+
the succeeding arguments. If no matching function is found then
|
| 306 |
+
|
| 307 |
+
- if the allocated object type has new-extended alignment, the alignment
|
| 308 |
+
argument is removed from the argument list;
|
| 309 |
+
- otherwise, an argument that is the type’s alignment and has type
|
| 310 |
+
`std::align_val_t` is added into the argument list immediately after
|
| 311 |
+
the first argument;
|
| 312 |
+
|
| 313 |
+
and then overload resolution is performed again.
|
| 314 |
|
| 315 |
[*Example 6*:
|
| 316 |
|
| 317 |
- `new T` results in one of the following calls:
|
| 318 |
``` cpp
|
|
|
|
| 337 |
|
| 338 |
Here, each instance of `x` is a non-negative unspecified value
|
| 339 |
representing array allocation overhead; the result of the
|
| 340 |
*new-expression* will be offset by this amount from the value returned
|
| 341 |
by `operator new[]`. This overhead may be applied in all array
|
| 342 |
+
*new-expression*s, including those referencing a placement allocation
|
| 343 |
+
function, except when referencing the library function
|
| 344 |
+
`operator new[](std::size_t, void*)`. The amount of overhead may vary
|
| 345 |
+
from one invocation of `new` to another.
|
| 346 |
|
| 347 |
— *end example*]
|
| 348 |
|
| 349 |
+
[*Note 10*: Unless an allocation function has a non-throwing exception
|
| 350 |
+
specification [[except.spec]], it indicates failure to allocate storage
|
| 351 |
+
by throwing a `std::bad_alloc` exception (
|
| 352 |
+
[[basic.stc.dynamic.allocation]], [[except]], [[bad.alloc]]); it returns
|
| 353 |
+
a non-null pointer otherwise. If the allocation function has a
|
| 354 |
+
non-throwing exception specification, it returns null to indicate
|
| 355 |
failure to allocate storage and a non-null pointer
|
| 356 |
otherwise. — *end note*]
|
| 357 |
|
| 358 |
+
If the allocation function is a non-allocating form
|
| 359 |
+
[[new.delete.placement]] that returns null, the behavior is undefined.
|
| 360 |
Otherwise, if the allocation function returns null, initialization shall
|
| 361 |
not be done, the deallocation function shall not be called, and the
|
| 362 |
value of the *new-expression* shall be null.
|
| 363 |
|
| 364 |
+
[*Note 11*: When the allocation function returns a value other than
|
| 365 |
null, it must be a pointer to a block of storage in which space for the
|
| 366 |
object has been reserved. The block of storage is assumed to be
|
| 367 |
appropriately aligned and of the requested size. The address of the
|
| 368 |
created object will not necessarily be the same as that of the block if
|
| 369 |
the object is an array. — *end note*]
|
| 370 |
|
| 371 |
A *new-expression* that creates an object of type `T` initializes that
|
| 372 |
object as follows:
|
| 373 |
|
| 374 |
+
- If the *new-initializer* is omitted, the object is default-initialized
|
| 375 |
+
[[dcl.init]]. \[*Note 12*: If no initialization is performed, the
|
| 376 |
+
object has an indeterminate value. — *end note*]
|
| 377 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 378 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 379 |
|
| 380 |
The invocation of the allocation function is sequenced before the
|
| 381 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 382 |
the allocated object is sequenced before the value computation of the
|
| 383 |
*new-expression*.
|
| 384 |
|
| 385 |
If the *new-expression* creates an object or an array of objects of
|
| 386 |
class type, access and ambiguity control are done for the allocation
|
| 387 |
+
function, the deallocation function [[class.free]], and the constructor
|
| 388 |
+
[[class.ctor]] selected for the initialization (if any). If the
|
| 389 |
+
*new-expression* creates an array of objects of class type, the
|
| 390 |
+
destructor is potentially invoked [[class.dtor]].
|
| 391 |
|
| 392 |
+
If any part of the object initialization described above[^24] terminates
|
| 393 |
by throwing an exception and a suitable deallocation function can be
|
| 394 |
found, the deallocation function is called to free the memory in which
|
| 395 |
the object was being constructed, after which the exception continues to
|
| 396 |
propagate in the context of the *new-expression*. If no unambiguous
|
| 397 |
matching deallocation function can be found, propagating the exception
|
| 398 |
does not cause the object’s memory to be freed.
|
| 399 |
|
| 400 |
+
[*Note 13*: This is appropriate when the called allocation function
|
| 401 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 402 |
leak. — *end note*]
|
| 403 |
|
| 404 |
If the *new-expression* begins with a unary `::` operator, the
|
| 405 |
deallocation function’s name is looked up in the global scope.
|
|
|
|
| 409 |
not a class type or array thereof, the deallocation function’s name is
|
| 410 |
looked up in the global scope.
|
| 411 |
|
| 412 |
A declaration of a placement deallocation function matches the
|
| 413 |
declaration of a placement allocation function if it has the same number
|
| 414 |
+
of parameters and, after parameter transformations [[dcl.fct]], all
|
| 415 |
parameter types except the first are identical. If the lookup finds a
|
| 416 |
single matching deallocation function, that function will be called;
|
| 417 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 418 |
+
a usual deallocation function and that function, considered as a
|
|
|
|
| 419 |
placement deallocation function, would have been selected as a match for
|
| 420 |
the allocation function, the program is ill-formed. For a non-placement
|
| 421 |
allocation function, the normal deallocation function lookup is used to
|
| 422 |
+
find the matching deallocation function [[expr.delete]].
|
| 423 |
|
| 424 |
[*Example 7*:
|
| 425 |
|
| 426 |
``` cpp
|
| 427 |
struct S {
|
|
|
|
| 430 |
|
| 431 |
// Usual (non-placement) deallocation function:
|
| 432 |
static void operator delete(void*, std::size_t);
|
| 433 |
};
|
| 434 |
|
| 435 |
+
S* p = new (0) S; // error: non-placement deallocation function matches
|
| 436 |
// placement allocation function
|
| 437 |
```
|
| 438 |
|
| 439 |
— *end example*]
|
| 440 |
|
| 441 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 442 |
returned from the allocation function call as the first argument of type
|
| 443 |
`void*`. If a placement deallocation function is called, it is passed
|
| 444 |
the same additional arguments as were passed to the placement allocation
|
| 445 |
function, that is, the same arguments as those specified with the
|
| 446 |
+
*new-placement* syntax. If the implementation is allowed to introduce a
|
| 447 |
+
temporary object or make a copy of any argument as part of the call to
|
| 448 |
+
the allocation function, it is unspecified whether the same object is
|
| 449 |
+
used in the call to both the allocation and deallocation functions.
|
|
|
|
|
|
|
| 450 |
|