- tmp/tmp20tzspwe/{from.md → to.md} +191 -117
tmp/tmp20tzspwe/{from.md → to.md}
RENAMED
|
@@ -2,16 +2,18 @@
|
|
| 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 |
-
[
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
cv-qualified type
|
|
|
|
|
|
|
| 13 |
|
| 14 |
``` bnf
|
| 15 |
new-expression:
|
| 16 |
'::'ₒₚₜ 'new' new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 17 |
'::'ₒₚₜ 'new' new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
|
@@ -44,50 +46,67 @@ new-initializer:
|
|
| 44 |
'(' expression-listₒₚₜ ')'
|
| 45 |
braced-init-list
|
| 46 |
```
|
| 47 |
|
| 48 |
Entities created by a *new-expression* have dynamic storage duration (
|
| 49 |
-
[[basic.stc.dynamic]]).
|
| 50 |
-
necessarily restricted to the scope in which it is created. If the
|
| 51 |
-
entity is a non-array object, the *new-expression* returns a pointer to
|
| 52 |
-
the object created. If it is an array, the *new-expression* returns a
|
| 53 |
-
pointer to the initial element of the array.
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
-
*
|
| 64 |
-
allocated type is
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
``` cpp
|
| 68 |
-
T x
|
| 69 |
```
|
| 70 |
|
|
|
|
|
|
|
| 71 |
``` cpp
|
| 72 |
new auto(1); // allocated type is int
|
| 73 |
auto x = new auto('a'); // allocated type is char, x is of type char*
|
|
|
|
|
|
|
|
|
|
| 74 |
```
|
| 75 |
|
|
|
|
|
|
|
| 76 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 77 |
-
of *new-declarator*s.
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
``` cpp
|
| 81 |
new int * i; // syntax error: parsed as (new int*) i, not as (new int)*i
|
| 82 |
```
|
| 83 |
|
| 84 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
effects.
|
| 88 |
|
|
|
|
|
|
|
| 89 |
``` cpp
|
| 90 |
new int(*[10])(); // error
|
| 91 |
```
|
| 92 |
|
| 93 |
is ill-formed because the binding is
|
|
@@ -102,66 +121,85 @@ be used to create objects of compound types ([[basic.compound]]):
|
|
| 102 |
``` cpp
|
| 103 |
new (int (*[10])());
|
| 104 |
```
|
| 105 |
|
| 106 |
allocates an array of `10` pointers to functions (taking no argument and
|
| 107 |
-
returning `int`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
When the allocated object is an array (that is, the
|
| 110 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 111 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 112 |
-
initial element (if any) of the array.
|
| 113 |
-
|
| 114 |
-
*
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 118 |
converted constant expression ([[expr.const]]) of type `std::size_t`
|
| 119 |
and shall evaluate to a strictly positive value. The *expression* in a
|
| 120 |
-
*noptr-new-declarator*is implicitly converted to `std::size_t`.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
The *expression* in a *noptr-new-declarator* is erroneous if:
|
| 127 |
|
| 128 |
- the expression is of non-class type and its value before converting to
|
| 129 |
`std::size_t` is less than zero;
|
| 130 |
- the expression is of class type and its value before application of
|
| 131 |
the second standard conversion ([[over.ics.user]])[^18] is less than
|
| 132 |
zero;
|
| 133 |
- its value is such that the size of the allocated object would exceed
|
| 134 |
-
the implementation-defined limit (
|
| 135 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 136 |
elements for which initializers are provided (including the
|
| 137 |
terminating `'\0'` in a string literal ([[lex.string]])) exceeds the
|
| 138 |
number of elements to initialize.
|
| 139 |
|
| 140 |
-
If the *expression*
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
A *new-expression* may obtain storage for the object by calling an
|
| 150 |
-
|
| 151 |
*new-expression* terminates by throwing an exception, it may release
|
| 152 |
storage by calling a deallocation function (
|
| 153 |
[[basic.stc.dynamic.deallocation]]). If the allocated type is a
|
| 154 |
non-array type, the allocation function’s name is `operator new` and the
|
| 155 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 156 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 157 |
-
the deallocation function’s name is `operator delete[]`.
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
[[
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
If the *new-expression* begins with a unary `::` operator, the
|
| 165 |
allocation function’s name is looked up in the global scope. Otherwise,
|
| 166 |
if the allocated type is a class type `T` or array thereof, the
|
| 167 |
allocation function’s name is looked up in the scope of `T`. If this
|
|
@@ -187,10 +225,12 @@ true were the allocation not extended:
|
|
| 187 |
*delete-expression*s, and
|
| 188 |
- the evaluation of `e2` is sequenced before the evaluation of the
|
| 189 |
*delete-expression* whose operand is the pointer value produced by
|
| 190 |
`e1`.
|
| 191 |
|
|
|
|
|
|
|
| 192 |
``` cpp
|
| 193 |
void mergeable(int x) {
|
| 194 |
// These allocations are safe for merging:
|
| 195 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 196 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
|
@@ -209,110 +249,140 @@ void mergeable(int x) {
|
|
| 209 |
throw;
|
| 210 |
}
|
| 211 |
}
|
| 212 |
```
|
| 213 |
|
|
|
|
|
|
|
| 214 |
When a *new-expression* calls an allocation function and that allocation
|
| 215 |
has not been extended, the *new-expression* passes the amount of space
|
| 216 |
requested to the allocation function as the first argument of type
|
| 217 |
`std::size_t`. That argument shall be no less than the size of the
|
| 218 |
object being created; it may be greater than the size of the object
|
| 219 |
-
being created only if the object is an array. For arrays of `char`
|
| 220 |
-
`unsigned char`, the difference between the result of
|
| 221 |
-
*new-expression* and the address returned by the allocation function
|
| 222 |
shall be an integral multiple of the strictest fundamental alignment
|
| 223 |
requirement ([[basic.align]]) of any object type whose size is no
|
| 224 |
-
greater than the size of the array being created.
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
| 230 |
|
| 231 |
When a *new-expression* calls an allocation function and that allocation
|
| 232 |
has been extended, the size argument to the allocation call shall be no
|
| 233 |
greater than the sum of the sizes for the omitted calls as specified
|
| 234 |
above, plus the size for the extended call had it not been extended,
|
| 235 |
plus any padding necessary to align the allocated objects within the
|
| 236 |
allocated memory.
|
| 237 |
|
| 238 |
The *new-placement* syntax is used to supply additional arguments to an
|
| 239 |
-
allocation function
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
`std::size_t`
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
[
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
A *new-expression* that creates an object of type `T` initializes that
|
| 282 |
object as follows:
|
| 283 |
|
| 284 |
- If the *new-initializer* is omitted, the object is
|
| 285 |
-
default-initialized ([[dcl.init]]). If no initialization
|
| 286 |
-
performed, the object has an indeterminate value.
|
| 287 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 288 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 289 |
|
| 290 |
-
The invocation of the allocation function is
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
expressions in the *new-initializer* are evaluated if the allocation
|
| 295 |
-
function returns the null pointer or exits using an exception.
|
| 296 |
|
| 297 |
If the *new-expression* creates an object or an array of objects of
|
| 298 |
class type, access and ambiguity control are done for the allocation
|
| 299 |
function, the deallocation function ([[class.free]]), and the
|
| 300 |
constructor ([[class.ctor]]). If the *new-expression* creates an array
|
| 301 |
of objects of class type, the destructor is potentially invoked (
|
| 302 |
[[class.dtor]]).
|
| 303 |
|
| 304 |
If any part of the object initialization described above[^19] terminates
|
| 305 |
-
by throwing an exception
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
| 312 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 313 |
-
leak.
|
| 314 |
|
| 315 |
If the *new-expression* begins with a unary `::` operator, the
|
| 316 |
deallocation function’s name is looked up in the global scope.
|
| 317 |
Otherwise, if the allocated type is a class type `T` or an array
|
| 318 |
thereof, the deallocation function’s name is looked up in the scope of
|
|
@@ -324,17 +394,19 @@ A declaration of a placement deallocation function matches the
|
|
| 324 |
declaration of a placement allocation function if it has the same number
|
| 325 |
of parameters and, after parameter transformations ([[dcl.fct]]), all
|
| 326 |
parameter types except the first are identical. If the lookup finds a
|
| 327 |
single matching deallocation function, that function will be called;
|
| 328 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 329 |
-
|
| 330 |
[[basic.stc.dynamic.deallocation]]) and that function, considered as a
|
| 331 |
placement deallocation function, would have been selected as a match for
|
| 332 |
the allocation function, the program is ill-formed. For a non-placement
|
| 333 |
allocation function, the normal deallocation function lookup is used to
|
| 334 |
find the matching deallocation function ([[expr.delete]])
|
| 335 |
|
|
|
|
|
|
|
| 336 |
``` cpp
|
| 337 |
struct S {
|
| 338 |
// Placement allocation function:
|
| 339 |
static void* operator new(std::size_t, std::size_t);
|
| 340 |
|
|
@@ -344,10 +416,12 @@ struct S {
|
|
| 344 |
|
| 345 |
S* p = new (0) S; // ill-formed: non-placement deallocation function matches
|
| 346 |
// placement allocation function
|
| 347 |
```
|
| 348 |
|
|
|
|
|
|
|
| 349 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 350 |
returned from the allocation function call as the first argument of type
|
| 351 |
`void*`. If a placement deallocation function is called, it is passed
|
| 352 |
the same additional arguments as were passed to the placement allocation
|
| 353 |
function, that is, the same arguments as those specified with the
|
|
|
|
| 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ₒₚₜ
|
|
|
|
| 46 |
'(' expression-listₒₚₜ ')'
|
| 47 |
braced-init-list
|
| 48 |
```
|
| 49 |
|
| 50 |
Entities created by a *new-expression* have dynamic storage duration (
|
| 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 |
|
| 72 |
+
[*Example 1*:
|
| 73 |
+
|
| 74 |
``` cpp
|
| 75 |
new auto(1); // allocated type is int
|
| 76 |
auto x = new auto('a'); // allocated type is char, x is of type char*
|
| 77 |
+
|
| 78 |
+
template<class T> struct A { A(T, T); };
|
| 79 |
+
auto y = new A{1, 2}; // allocated type is A<int>
|
| 80 |
```
|
| 81 |
|
| 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 4*: This prevents ambiguities between the declarator operators
|
| 88 |
+
`&`, `&&`, `*`, and `[]` and their expression
|
| 89 |
+
counterparts. — *end note*]
|
| 90 |
+
|
| 91 |
+
[*Example 2*:
|
| 92 |
|
| 93 |
``` cpp
|
| 94 |
new int * i; // syntax error: parsed as (new int*) i, not as (new int)*i
|
| 95 |
```
|
| 96 |
|
| 97 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 98 |
|
| 99 |
+
— *end example*]
|
| 100 |
+
|
| 101 |
+
[*Note 5*:
|
| 102 |
+
|
| 103 |
+
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 104 |
effects.
|
| 105 |
|
| 106 |
+
[*Example 3*:
|
| 107 |
+
|
| 108 |
``` cpp
|
| 109 |
new int(*[10])(); // error
|
| 110 |
```
|
| 111 |
|
| 112 |
is ill-formed because the binding is
|
|
|
|
| 121 |
``` cpp
|
| 122 |
new (int (*[10])());
|
| 123 |
```
|
| 124 |
|
| 125 |
allocates an array of `10` pointers to functions (taking no argument and
|
| 126 |
+
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 |
+
|
| 137 |
+
[*Note 6*: Both `new int` and `new int[10]` have type `int*` and the
|
| 138 |
+
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 ([[expr.const]]) of type `std::size_t`
|
| 145 |
and shall evaluate to a strictly positive value. The *expression* in a
|
| 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 |
The *expression* in a *noptr-new-declarator* is erroneous if:
|
| 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 ([[over.ics.user]])[^18] is less than
|
| 159 |
zero;
|
| 160 |
- its value is such that the size of the allocated object would exceed
|
| 161 |
+
the *implementation-defined* limit (Annex [[implimits]]); or
|
| 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 literal ([[lex.string]])) exceeds the
|
| 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 ([[except.spec]]), the value
|
| 174 |
+
of the *new-expression* is the null pointer value of the required
|
| 175 |
+
result type;
|
| 176 |
+
- otherwise, the *new-expression* terminates by throwing an exception
|
| 177 |
+
of a type that would match a handler ([[except.handle]]) of type
|
| 178 |
+
`std::bad_array_new_length` ([[new.badlength]]).
|
| 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 ([[basic.stc.dynamic.allocation]]). If the
|
| 185 |
*new-expression* terminates by throwing an exception, it may release
|
| 186 |
storage by calling a deallocation function (
|
| 187 |
[[basic.stc.dynamic.deallocation]]). If the allocated type is a
|
| 188 |
non-array type, the allocation function’s name is `operator new` and the
|
| 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 shall provide default definitions for the
|
| 194 |
+
global allocation functions ([[basic.stc.dynamic]],
|
| 195 |
+
[[new.delete.single]], [[new.delete.array]]). A C++program can provide
|
| 196 |
+
alternative definitions of these functions ([[replacement.functions]])
|
| 197 |
+
and/or class-specific versions ([[class.free]]). The set of allocation
|
| 198 |
+
and deallocation functions that may be called by a *new-expression* may
|
| 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,
|
| 204 |
if the allocated type is a class type `T` or array thereof, the
|
| 205 |
allocation function’s name is looked up in the scope of `T`. If this
|
|
|
|
| 225 |
*delete-expression*s, and
|
| 226 |
- the evaluation of `e2` is sequenced before the evaluation of the
|
| 227 |
*delete-expression* whose operand is the pointer value produced by
|
| 228 |
`e1`.
|
| 229 |
|
| 230 |
+
[*Example 5*:
|
| 231 |
+
|
| 232 |
``` cpp
|
| 233 |
void mergeable(int x) {
|
| 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]};
|
|
|
|
| 249 |
throw;
|
| 250 |
}
|
| 251 |
}
|
| 252 |
```
|
| 253 |
|
| 254 |
+
— *end example*]
|
| 255 |
+
|
| 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. For arrays of `char`,
|
| 262 |
+
`unsigned char`, and `std::byte`, the difference between the result of
|
| 263 |
+
the *new-expression* and the address returned by the allocation function
|
| 264 |
shall be an integral multiple of the strictest fundamental alignment
|
| 265 |
requirement ([[basic.align]]) of any object type whose size is no
|
| 266 |
+
greater than the size of the array being created.
|
| 267 |
+
|
| 268 |
+
[*Note 8*: Because allocation functions are assumed to return pointers
|
| 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 |
|
| 274 |
When a *new-expression* calls an allocation function and that allocation
|
| 275 |
has been extended, the size argument to the allocation call shall be no
|
| 276 |
greater than the sum of the sizes for the omitted calls as specified
|
| 277 |
above, plus the size for the extended call had it not been extended,
|
| 278 |
plus any padding necessary to align the allocated objects within the
|
| 279 |
allocated memory.
|
| 280 |
|
| 281 |
The *new-placement* syntax is used to supply additional arguments to an
|
| 282 |
+
allocation function; such an expression is called a *placement
|
| 283 |
+
*new-expression**.
|
| 284 |
+
|
| 285 |
+
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 and the
|
| 292 |
+
allocated object type has new-extended alignment, the alignment argument
|
| 293 |
+
is removed from the argument list, and overload resolution is performed
|
| 294 |
+
again.
|
| 295 |
+
|
| 296 |
+
[*Example 6*:
|
| 297 |
+
|
| 298 |
+
- `new T` results in one of the following calls:
|
| 299 |
+
``` cpp
|
| 300 |
+
operator new(sizeof(T))
|
| 301 |
+
operator new(sizeof(T), std::align_val_t(alignof(T)))
|
| 302 |
+
```
|
| 303 |
+
- `new(2,f) T` results in one of the following calls:
|
| 304 |
+
``` cpp
|
| 305 |
+
operator new(sizeof(T), 2, f)
|
| 306 |
+
operator new(sizeof(T), std::align_val_t(alignof(T)), 2, f)
|
| 307 |
+
```
|
| 308 |
+
- `new T[5]` results in one of the following calls:
|
| 309 |
+
``` cpp
|
| 310 |
+
operator new[](sizeof(T) * 5 + x)
|
| 311 |
+
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)))
|
| 312 |
+
```
|
| 313 |
+
- `new(2,f) T[5]` results in one of the following calls:
|
| 314 |
+
``` cpp
|
| 315 |
+
operator new[](sizeof(T) * 5 + x, 2, f)
|
| 316 |
+
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)), 2, f)
|
| 317 |
+
```
|
| 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 the library function
|
| 324 |
+
`operator new[](std::size_t, void*)` and other placement allocation
|
| 325 |
+
functions. The amount of overhead may vary from one invocation of `new`
|
| 326 |
+
to another.
|
| 327 |
+
|
| 328 |
+
— *end example*]
|
| 329 |
+
|
| 330 |
+
[*Note 9*: Unless an allocation function has a non-throwing exception
|
| 331 |
+
specification ([[except.spec]]), it indicates failure to allocate
|
| 332 |
+
storage by throwing a `std::bad_alloc` exception (
|
| 333 |
+
[[basic.stc.dynamic.allocation]], Clause [[except]], [[bad.alloc]]);
|
| 334 |
+
it returns a non-null pointer otherwise. If the allocation function has
|
| 335 |
+
a non-throwing exception specification, it returns null to indicate
|
| 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]]) that returns null, the behavior is undefined.
|
| 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 10*: When the allocation function returns a value other than
|
| 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 |
+
default-initialized ([[dcl.init]]). \[*Note 11*: If no initialization
|
| 357 |
+
is performed, the object has an indeterminate value. — *end note*]
|
| 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 ([[class.free]]), and the
|
| 369 |
constructor ([[class.ctor]]). If the *new-expression* creates an array
|
| 370 |
of objects of class type, the destructor is potentially invoked (
|
| 371 |
[[class.dtor]]).
|
| 372 |
|
| 373 |
If any part of the object initialization described above[^19] terminates
|
| 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 12*: This is appropriate when the called allocation function
|
| 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.
|
| 387 |
Otherwise, if the allocated type is a class type `T` or an array
|
| 388 |
thereof, the deallocation function’s name is looked up in the scope of
|
|
|
|
| 394 |
declaration of a placement allocation function if it has the same number
|
| 395 |
of parameters and, after parameter transformations ([[dcl.fct]]), all
|
| 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 with a parameter of type `std::size_t` (
|
| 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 ([[expr.delete]])
|
| 405 |
|
| 406 |
+
[*Example 7*:
|
| 407 |
+
|
| 408 |
``` cpp
|
| 409 |
struct S {
|
| 410 |
// Placement allocation function:
|
| 411 |
static void* operator new(std::size_t, std::size_t);
|
| 412 |
|
|
|
|
| 416 |
|
| 417 |
S* p = new (0) S; // ill-formed: non-placement deallocation function matches
|
| 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
|