- tmp/tmps6vx5yfd/{from.md → to.md} +438 -281
tmp/tmps6vx5yfd/{from.md → to.md}
RENAMED
|
@@ -1,60 +1,57 @@
|
|
| 1 |
-
## Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 2 |
|
| 3 |
Expressions with unary operators group right-to-left.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
unary-expression:
|
| 7 |
postfix-expression
|
|
|
|
| 8 |
'++' cast-expression
|
| 9 |
'-{-}' cast-expression
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
noexcept-expression
|
| 16 |
new-expression
|
| 17 |
delete-expression
|
| 18 |
```
|
| 19 |
|
| 20 |
``` bnf
|
| 21 |
unary-operator: one of
|
| 22 |
'* & + - ! ~'
|
| 23 |
```
|
| 24 |
|
| 25 |
-
### Unary operators <a id="expr.unary.op">[[expr.unary.op]]</a>
|
| 26 |
|
| 27 |
The unary `*` operator performs *indirection*: the expression to which
|
| 28 |
it is applied shall be a pointer to an object type, or a pointer to a
|
| 29 |
function type and the result is an lvalue referring to the object or
|
| 30 |
function to which the expression points. If the type of the expression
|
| 31 |
is “pointer to `T`”, the type of the result is “`T`”.
|
| 32 |
|
| 33 |
[*Note 1*: Indirection through a pointer to an incomplete type (other
|
| 34 |
-
than
|
| 35 |
limited ways (to initialize a reference, for example); this lvalue must
|
| 36 |
not be converted to a prvalue, see [[conv.lval]]. — *end note*]
|
| 37 |
|
| 38 |
The result of each of the following unary operators is a prvalue.
|
| 39 |
|
| 40 |
-
The result of the unary `&` operator is a pointer to its operand.
|
| 41 |
-
operand shall be an lvalue or a *qualified-id*. If the operand is a
|
| 42 |
-
*qualified-id* naming a non-static or variant member `m` of some class
|
| 43 |
-
`C` with type `T`, the result has type “pointer to member of class `C`
|
| 44 |
-
of type `T`” and is a prvalue designating `C::m`. Otherwise, if the type
|
| 45 |
-
of the expression is `T`, the result has type “pointer to `T`” and is a
|
| 46 |
-
prvalue that is the address of the designated object ([[intro.memory]])
|
| 47 |
-
or a pointer to the designated function.
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
[*Example 1*:
|
| 58 |
|
| 59 |
``` cpp
|
| 60 |
struct A { int i; };
|
|
@@ -67,38 +64,37 @@ bool b = p2 > p1; // defined behavior, with value true
|
|
| 67 |
```
|
| 68 |
|
| 69 |
— *end example*]
|
| 70 |
|
| 71 |
[*Note 3*: A pointer to member formed from a `mutable` non-static data
|
| 72 |
-
member
|
| 73 |
-
|
| 74 |
|
| 75 |
A pointer to member is only formed when an explicit `&` is used and its
|
| 76 |
operand is a *qualified-id* not enclosed in parentheses.
|
| 77 |
|
| 78 |
[*Note 4*: That is, the expression `&(qualified-id)`, where the
|
| 79 |
*qualified-id* is enclosed in parentheses, does not form an expression
|
| 80 |
of type “pointer to member”. Neither does `qualified-id`, because there
|
| 81 |
is no implicit conversion from a *qualified-id* for a non-static member
|
| 82 |
function to the type “pointer to member function” as there is from an
|
| 83 |
-
lvalue of function type to the type “pointer to function”
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 88 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 89 |
the built-in meaning or the operator function is called. The operand of
|
| 90 |
`&` shall not be a bit-field.
|
| 91 |
|
| 92 |
-
The address of an overloaded function
|
| 93 |
only in a context that uniquely determines which version of the
|
| 94 |
-
overloaded function is referred to (see [[over.over]]).
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
member function”. — *end note*]
|
| 100 |
|
| 101 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 102 |
enumeration, or pointer type and the result is the value of the
|
| 103 |
argument. Integral promotion is performed on integral or enumeration
|
| 104 |
operands. The type of the result is the type of the promoted operand.
|
|
@@ -109,90 +105,207 @@ promotion is performed on integral or enumeration operands. The negative
|
|
| 109 |
of an unsigned quantity is computed by subtracting its value from 2ⁿ,
|
| 110 |
where n is the number of bits in the promoted operand. The type of the
|
| 111 |
result is the type of the promoted operand.
|
| 112 |
|
| 113 |
The operand of the logical negation operator `!` is contextually
|
| 114 |
-
converted to `bool`
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
The operand of `~` shall have integral or unscoped enumeration type; the
|
| 119 |
result is the ones’ complement of its operand. Integral promotions are
|
| 120 |
performed. The type of the result is the type of the promoted operand.
|
| 121 |
There is an ambiguity in the grammar when `~` is followed by a
|
| 122 |
-
*
|
| 123 |
treating `~` as the unary complement operator rather than as the start
|
| 124 |
of an *unqualified-id* naming a destructor.
|
| 125 |
|
| 126 |
[*Note 6*: Because the grammar does not permit an operator to follow
|
| 127 |
-
the `.`, `->`, or `::` tokens, a `~` followed by a *
|
| 128 |
*decltype-specifier* in a member access expression or *qualified-id* is
|
| 129 |
unambiguously parsed as a destructor name. — *end note*]
|
| 130 |
|
| 131 |
-
### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
| 132 |
|
| 133 |
-
The operand of prefix `++` is modified by adding `1`.
|
| 134 |
-
be a modifiable lvalue. The type of the operand shall
|
| 135 |
-
type other than cv `bool`, or a pointer to a
|
| 136 |
-
type.
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
-
[*Note 1*: See the discussions of addition
|
| 141 |
-
|
| 142 |
-
conversions. — *end note*]
|
| 143 |
|
| 144 |
-
The operand of prefix `\dcr` is modified by subtracting
|
| 145 |
-
requirements on the operand of prefix `\dcr` and the properties
|
| 146 |
-
result are otherwise the same as those of prefix `++`.
|
| 147 |
|
| 148 |
[*Note 2*: For postfix increment and decrement, see
|
| 149 |
[[expr.post.incr]]. — *end note*]
|
| 150 |
|
| 151 |
-
###
|
| 152 |
-
|
| 153 |
-
The `
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
`
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
[*Note 1*: In particular, `sizeof(bool)`, `sizeof(char16_t)`,
|
| 164 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 165 |
-
implementation-defined.[^
|
| 166 |
|
| 167 |
-
[*Note 2*: See [[intro.memory]] for the definition of
|
| 168 |
-
[[basic.types]] for the definition of
|
| 169 |
-
representation
|
| 170 |
|
| 171 |
-
When applied to a reference
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
*n* elements is *n* times the size of an element.
|
| 180 |
|
| 181 |
-
The
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
The
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
is applied.
|
| 189 |
-
|
| 190 |
-
The identifier in a `sizeof...` expression shall name a parameter pack.
|
| 191 |
-
The `sizeof...` operator yields the number of arguments provided for the
|
| 192 |
-
parameter pack *identifier*. A `sizeof...` expression is a pack
|
| 193 |
-
expansion ([[temp.variadic]]).
|
| 194 |
|
| 195 |
[*Example 1*:
|
| 196 |
|
| 197 |
``` cpp
|
| 198 |
template<class... Types>
|
|
@@ -201,35 +314,71 @@ struct count {
|
|
| 201 |
};
|
| 202 |
```
|
| 203 |
|
| 204 |
— *end example*]
|
| 205 |
|
| 206 |
-
The result of `sizeof` and `sizeof...` is a
|
| 207 |
`std::size_t`.
|
| 208 |
|
| 209 |
-
[*Note 3*:
|
|
|
|
| 210 |
`<cstddef>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 211 |
|
| 212 |
-
###
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
object is the *allocated type*. This type shall be a complete object
|
| 217 |
type, but not an abstract class type or array thereof (
|
| 218 |
-
[[intro.object]],
|
| 219 |
|
| 220 |
[*Note 1*: Because references are not objects, references cannot be
|
| 221 |
created by *new-expression*s. — *end note*]
|
| 222 |
|
| 223 |
[*Note 2*: The *type-id* may be a cv-qualified type, in which case the
|
| 224 |
object created by the *new-expression* has a cv-qualified
|
| 225 |
type. — *end note*]
|
| 226 |
|
| 227 |
``` bnf
|
| 228 |
new-expression:
|
| 229 |
-
'::'ₒₚₜ
|
| 230 |
-
'::'ₒₚₜ
|
| 231 |
```
|
| 232 |
|
| 233 |
``` bnf
|
| 234 |
new-placement:
|
| 235 |
'(' expression-list ')'
|
|
@@ -246,37 +395,27 @@ new-declarator:
|
|
| 246 |
noptr-new-declarator
|
| 247 |
```
|
| 248 |
|
| 249 |
``` bnf
|
| 250 |
noptr-new-declarator:
|
| 251 |
-
'[' expression ']' attribute-specifier-seqₒₚₜ
|
| 252 |
noptr-new-declarator '[' constant-expression ']' attribute-specifier-seqₒₚₜ
|
| 253 |
```
|
| 254 |
|
| 255 |
``` bnf
|
| 256 |
new-initializer:
|
| 257 |
'(' expression-listₒₚₜ ')'
|
| 258 |
braced-init-list
|
| 259 |
```
|
| 260 |
|
| 261 |
-
|
| 262 |
-
[[basic.stc.dynamic]]).
|
| 263 |
-
|
| 264 |
-
[*Note 3*: The lifetime of such an entity is not necessarily
|
| 265 |
-
restricted to the scope in which it is created. — *end note*]
|
| 266 |
-
|
| 267 |
-
If the entity is a non-array object, the *new-expression* returns a
|
| 268 |
-
pointer to the object created. If it is an array, the *new-expression*
|
| 269 |
-
returns a pointer to the initial element of the array.
|
| 270 |
-
|
| 271 |
-
If a placeholder type ([[dcl.spec.auto]]) appears in the
|
| 272 |
*type-specifier-seq* of a *new-type-id* or *type-id* of a
|
| 273 |
*new-expression*, the allocated type is deduced as follows: Let *init*
|
| 274 |
be the *new-initializer*, if any, and `T` be the *new-type-id* or
|
| 275 |
*type-id* of the *new-expression*, then the allocated type is the type
|
| 276 |
-
deduced for the variable `x` in the invented declaration
|
| 277 |
-
[[dcl.spec.auto]]
|
| 278 |
|
| 279 |
``` cpp
|
| 280 |
T x init ;
|
| 281 |
```
|
| 282 |
|
|
@@ -293,11 +432,11 @@ auto y = new A{1, 2}; // allocated type is A<int>
|
|
| 293 |
— *end example*]
|
| 294 |
|
| 295 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 296 |
of *new-declarator*s.
|
| 297 |
|
| 298 |
-
[*Note
|
| 299 |
`&`, `&&`, `*`, and `[]` and their expression
|
| 300 |
counterparts. — *end note*]
|
| 301 |
|
| 302 |
[*Example 2*:
|
| 303 |
|
|
@@ -307,11 +446,11 @@ new int * i; // syntax error: parsed as (new int*) i, not as
|
|
| 307 |
|
| 308 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 309 |
|
| 310 |
— *end example*]
|
| 311 |
|
| 312 |
-
[*Note
|
| 313 |
|
| 314 |
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 315 |
effects.
|
| 316 |
|
| 317 |
[*Example 3*:
|
|
@@ -325,11 +464,11 @@ is ill-formed because the binding is
|
|
| 325 |
``` cpp
|
| 326 |
(new int) (*[10])(); // error
|
| 327 |
```
|
| 328 |
|
| 329 |
Instead, the explicitly parenthesized version of the `new` operator can
|
| 330 |
-
be used to create objects of compound types
|
| 331 |
|
| 332 |
``` cpp
|
| 333 |
new (int (*[10])());
|
| 334 |
```
|
| 335 |
|
|
@@ -338,10 +477,19 @@ returning `int`).
|
|
| 338 |
|
| 339 |
— *end example*]
|
| 340 |
|
| 341 |
— *end note*]
|
| 342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
When the allocated object is an array (that is, the
|
| 344 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 345 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 346 |
initial element (if any) of the array.
|
| 347 |
|
|
@@ -350,65 +498,71 @@ type of `new int[i][10]` is `int (*)[10]` — *end note*]
|
|
| 350 |
|
| 351 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 352 |
the associated array type.
|
| 353 |
|
| 354 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 355 |
-
converted constant expression
|
| 356 |
-
|
| 357 |
-
*noptr-new-declarator* is implicitly converted to `std::size_t`.
|
| 358 |
|
| 359 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 360 |
well-formed (because `n` is the *expression* of a
|
| 361 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 362 |
`n` is not a constant expression). — *end example*]
|
| 363 |
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
- the expression is of non-class type and its value before converting to
|
| 367 |
`std::size_t` is less than zero;
|
| 368 |
- the expression is of class type and its value before application of
|
| 369 |
-
the second standard conversion
|
| 370 |
zero;
|
| 371 |
- its value is such that the size of the allocated object would exceed
|
| 372 |
-
the *implementation-defined* limit
|
| 373 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 374 |
elements for which initializers are provided (including the
|
| 375 |
-
terminating `'\0'` in a string
|
| 376 |
number of elements to initialize.
|
| 377 |
|
| 378 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 379 |
|
| 380 |
- if the *expression* is a core constant expression, the program is
|
| 381 |
ill-formed;
|
| 382 |
- otherwise, an allocation function is not called; instead
|
| 383 |
- if the allocation function that would have been called has a
|
| 384 |
-
non-throwing exception specification
|
| 385 |
-
|
| 386 |
result type;
|
| 387 |
- otherwise, the *new-expression* terminates by throwing an exception
|
| 388 |
-
of a type that would match a handler
|
| 389 |
-
`std::bad_array_new_length`
|
| 390 |
|
| 391 |
When the value of the *expression* is zero, the allocation function is
|
| 392 |
called to allocate an array with no elements.
|
| 393 |
|
| 394 |
A *new-expression* may obtain storage for the object by calling an
|
| 395 |
-
allocation function
|
| 396 |
*new-expression* terminates by throwing an exception, it may release
|
| 397 |
-
storage by calling a deallocation function
|
| 398 |
-
[[basic.stc.dynamic.deallocation]]
|
| 399 |
-
|
| 400 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 401 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 402 |
the deallocation function’s name is `operator delete[]`.
|
| 403 |
|
| 404 |
-
[*Note 7*: An implementation
|
| 405 |
-
global allocation functions ([[basic.stc.dynamic]],
|
| 406 |
-
[[new.delete.single]],
|
| 407 |
-
alternative definitions of these functions
|
| 408 |
-
and/or class-specific versions
|
| 409 |
-
|
| 410 |
include functions that do not perform allocation or deallocation; for
|
| 411 |
example, see [[new.delete.placement]]. — *end note*]
|
| 412 |
|
| 413 |
If the *new-expression* begins with a unary `::` operator, the
|
| 414 |
allocation function’s name is looked up in the global scope. Otherwise,
|
|
@@ -418,13 +572,21 @@ lookup fails to find the name, or if the allocated type is not a class
|
|
| 418 |
type, the allocation function’s name is looked up in the global scope.
|
| 419 |
|
| 420 |
An implementation is allowed to omit a call to a replaceable global
|
| 421 |
allocation function ([[new.delete.single]], [[new.delete.array]]). When
|
| 422 |
it does so, the storage is instead provided by the implementation or
|
| 423 |
-
provided by extending the allocation of another *new-expression*.
|
| 424 |
-
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
true were the allocation not extended:
|
| 427 |
|
| 428 |
- the evaluation of `e1` is sequenced before the evaluation of `e2`, and
|
| 429 |
- `e2` is evaluated whenever `e1` obtains storage, and
|
| 430 |
- both `e1` and `e2` invoke the same replaceable global allocation
|
|
@@ -439,20 +601,20 @@ true were the allocation not extended:
|
|
| 439 |
`e1`.
|
| 440 |
|
| 441 |
[*Example 5*:
|
| 442 |
|
| 443 |
``` cpp
|
| 444 |
-
|
| 445 |
// These allocations are safe for merging:
|
| 446 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 447 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
| 448 |
std::unique_ptr<char[]> c{new (std::nothrow) char[x]};
|
| 449 |
|
| 450 |
g(a.get(), b.get(), c.get());
|
| 451 |
}
|
| 452 |
|
| 453 |
-
|
| 454 |
std::unique_ptr<char[]> a{new char[8]};
|
| 455 |
try {
|
| 456 |
// Merging this allocation would change its catch handler.
|
| 457 |
std::unique_ptr<char[]> b{new char[x]};
|
| 458 |
} catch (const std::bad_alloc& e) {
|
|
@@ -467,18 +629,19 @@ true were the allocation not extended:
|
|
| 467 |
When a *new-expression* calls an allocation function and that allocation
|
| 468 |
has not been extended, the *new-expression* passes the amount of space
|
| 469 |
requested to the allocation function as the first argument of type
|
| 470 |
`std::size_t`. That argument shall be no less than the size of the
|
| 471 |
object being created; it may be greater than the size of the object
|
| 472 |
-
being created only if the object is an array
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
|
|
|
| 478 |
|
| 479 |
-
[*Note
|
| 480 |
to storage that is appropriately aligned for objects of any type with
|
| 481 |
fundamental alignment, this constraint on array allocation overhead
|
| 482 |
permits the common idiom of allocating character arrays into which
|
| 483 |
objects of other types will later be placed. — *end note*]
|
| 484 |
|
|
@@ -497,14 +660,19 @@ Overload resolution is performed on a function call created by
|
|
| 497 |
assembling an argument list. The first argument is the amount of space
|
| 498 |
requested, and has type `std::size_t`. If the type of the allocated
|
| 499 |
object has new-extended alignment, the next argument is the type’s
|
| 500 |
alignment, and has type `std::align_val_t`. If the *new-placement*
|
| 501 |
syntax is used, the *initializer-clause*s in its *expression-list* are
|
| 502 |
-
the succeeding arguments. If no matching function is found
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
|
| 507 |
[*Example 6*:
|
| 508 |
|
| 509 |
- `new T` results in one of the following calls:
|
| 510 |
``` cpp
|
|
@@ -529,69 +697,69 @@ again.
|
|
| 529 |
|
| 530 |
Here, each instance of `x` is a non-negative unspecified value
|
| 531 |
representing array allocation overhead; the result of the
|
| 532 |
*new-expression* will be offset by this amount from the value returned
|
| 533 |
by `operator new[]`. This overhead may be applied in all array
|
| 534 |
-
*new-expression*s, including those referencing
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
to another.
|
| 538 |
|
| 539 |
— *end example*]
|
| 540 |
|
| 541 |
-
[*Note
|
| 542 |
-
specification
|
| 543 |
-
|
| 544 |
-
[[basic.stc.dynamic.allocation]],
|
| 545 |
-
|
| 546 |
-
|
| 547 |
failure to allocate storage and a non-null pointer
|
| 548 |
otherwise. — *end note*]
|
| 549 |
|
| 550 |
-
If the allocation function is a non-allocating form
|
| 551 |
-
[[new.delete.placement]]
|
| 552 |
Otherwise, if the allocation function returns null, initialization shall
|
| 553 |
not be done, the deallocation function shall not be called, and the
|
| 554 |
value of the *new-expression* shall be null.
|
| 555 |
|
| 556 |
-
[*Note
|
| 557 |
null, it must be a pointer to a block of storage in which space for the
|
| 558 |
object has been reserved. The block of storage is assumed to be
|
| 559 |
appropriately aligned and of the requested size. The address of the
|
| 560 |
created object will not necessarily be the same as that of the block if
|
| 561 |
the object is an array. — *end note*]
|
| 562 |
|
| 563 |
A *new-expression* that creates an object of type `T` initializes that
|
| 564 |
object as follows:
|
| 565 |
|
| 566 |
-
- If the *new-initializer* is omitted, the object is
|
| 567 |
-
|
| 568 |
-
|
| 569 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 570 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 571 |
|
| 572 |
The invocation of the allocation function is sequenced before the
|
| 573 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 574 |
the allocated object is sequenced before the value computation of the
|
| 575 |
*new-expression*.
|
| 576 |
|
| 577 |
If the *new-expression* creates an object or an array of objects of
|
| 578 |
class type, access and ambiguity control are done for the allocation
|
| 579 |
-
function, the deallocation function
|
| 580 |
-
|
| 581 |
-
of objects of class type, the
|
| 582 |
-
[[class.dtor]]
|
| 583 |
|
| 584 |
-
If any part of the object initialization described above[^
|
| 585 |
by throwing an exception and a suitable deallocation function can be
|
| 586 |
found, the deallocation function is called to free the memory in which
|
| 587 |
the object was being constructed, after which the exception continues to
|
| 588 |
propagate in the context of the *new-expression*. If no unambiguous
|
| 589 |
matching deallocation function can be found, propagating the exception
|
| 590 |
does not cause the object’s memory to be freed.
|
| 591 |
|
| 592 |
-
[*Note
|
| 593 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 594 |
leak. — *end note*]
|
| 595 |
|
| 596 |
If the *new-expression* begins with a unary `::` operator, the
|
| 597 |
deallocation function’s name is looked up in the global scope.
|
|
@@ -601,20 +769,19 @@ thereof, the deallocation function’s name is looked up in the scope of
|
|
| 601 |
not a class type or array thereof, the deallocation function’s name is
|
| 602 |
looked up in the global scope.
|
| 603 |
|
| 604 |
A declaration of a placement deallocation function matches the
|
| 605 |
declaration of a placement allocation function if it has the same number
|
| 606 |
-
of parameters and, after parameter transformations
|
| 607 |
parameter types except the first are identical. If the lookup finds a
|
| 608 |
single matching deallocation function, that function will be called;
|
| 609 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 610 |
-
a usual deallocation function
|
| 611 |
-
[[basic.stc.dynamic.deallocation]]) and that function, considered as a
|
| 612 |
placement deallocation function, would have been selected as a match for
|
| 613 |
the allocation function, the program is ill-formed. For a non-placement
|
| 614 |
allocation function, the normal deallocation function lookup is used to
|
| 615 |
-
find the matching deallocation function
|
| 616 |
|
| 617 |
[*Example 7*:
|
| 618 |
|
| 619 |
``` cpp
|
| 620 |
struct S {
|
|
@@ -623,74 +790,72 @@ struct S {
|
|
| 623 |
|
| 624 |
// Usual (non-placement) deallocation function:
|
| 625 |
static void operator delete(void*, std::size_t);
|
| 626 |
};
|
| 627 |
|
| 628 |
-
S* p = new (0) S; //
|
| 629 |
// placement allocation function
|
| 630 |
```
|
| 631 |
|
| 632 |
— *end example*]
|
| 633 |
|
| 634 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 635 |
returned from the allocation function call as the first argument of type
|
| 636 |
`void*`. If a placement deallocation function is called, it is passed
|
| 637 |
the same additional arguments as were passed to the placement allocation
|
| 638 |
function, that is, the same arguments as those specified with the
|
| 639 |
-
*new-placement* syntax. If the implementation is allowed to
|
| 640 |
-
of any argument as part of the call to
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
call to the allocation function. If the copy is elided in one place, it
|
| 644 |
-
need not be elided in the other.
|
| 645 |
|
| 646 |
-
### Delete <a id="expr.delete">[[expr.delete]]</a>
|
| 647 |
|
| 648 |
-
The *delete-expression* operator destroys a most derived object
|
| 649 |
-
[[intro.object]]
|
| 650 |
|
| 651 |
``` bnf
|
| 652 |
delete-expression:
|
| 653 |
-
'::'ₒₚₜ
|
| 654 |
-
'::'ₒₚₜ
|
| 655 |
```
|
| 656 |
|
| 657 |
-
The first alternative is
|
| 658 |
-
|
| 659 |
-
square brackets, it shall be interpreted
|
| 660 |
-
The operand shall be of pointer to
|
| 661 |
-
class type
|
| 662 |
-
[[conv]]
|
| 663 |
-
result has type `void`.
|
| 664 |
|
| 665 |
If the operand has a class type, the operand is converted to a pointer
|
| 666 |
type by calling the above-mentioned conversion function, and the
|
| 667 |
converted operand is used in place of the original operand for the
|
| 668 |
-
remainder of this
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
behavior is undefined.
|
| 677 |
|
| 678 |
[*Note 1*: This means that the syntax of the *delete-expression* must
|
| 679 |
match the type of the object allocated by `new`, not the syntax of the
|
| 680 |
*new-expression*. — *end note*]
|
| 681 |
|
| 682 |
[*Note 2*: A pointer to a `const` type can be the operand of a
|
| 683 |
-
*delete-expression*; it is not necessary to cast away the constness
|
| 684 |
-
[[expr.const.cast]]
|
| 685 |
operand of the *delete-expression*. — *end note*]
|
| 686 |
|
| 687 |
-
In
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
|
|
|
| 692 |
type of the object to be deleted differs from its static type, the
|
| 693 |
behavior is undefined.
|
| 694 |
|
| 695 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 696 |
exactly once.
|
|
@@ -698,25 +863,26 @@ exactly once.
|
|
| 698 |
If the object being deleted has incomplete class type at the point of
|
| 699 |
deletion and the complete class has a non-trivial destructor or a
|
| 700 |
deallocation function, the behavior is undefined.
|
| 701 |
|
| 702 |
If the value of the operand of the *delete-expression* is not a null
|
| 703 |
-
pointer value
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
|
|
|
| 708 |
|
| 709 |
If the value of the operand of the *delete-expression* is not a null
|
| 710 |
pointer value, then:
|
| 711 |
|
| 712 |
- If the allocation call for the *new-expression* for the object to be
|
| 713 |
-
deleted was not omitted and the allocation was not extended
|
| 714 |
-
[[expr.new]]
|
| 715 |
-
function
|
| 716 |
-
|
| 717 |
-
|
| 718 |
- Otherwise, if the allocation was extended or was provided by extending
|
| 719 |
the allocation of another *new-expression*, and the
|
| 720 |
*delete-expression* for every other pointer value produced by a
|
| 721 |
*new-expression* that had storage provided by the extended
|
| 722 |
*new-expression* has been evaluated, the *delete-expression* shall
|
|
@@ -733,86 +899,77 @@ exception. — *end note*]
|
|
| 733 |
If the value of the operand of the *delete-expression* is a null pointer
|
| 734 |
value, it is unspecified whether a deallocation function will be called
|
| 735 |
as described above.
|
| 736 |
|
| 737 |
[*Note 4*: An implementation provides default definitions of the global
|
| 738 |
-
deallocation functions `operator delete` for non-arrays
|
| 739 |
-
[[new.delete.single]]
|
| 740 |
-
[[new.delete.array]]
|
| 741 |
-
of these functions
|
| 742 |
-
versions
|
| 743 |
|
| 744 |
When the keyword `delete` in a *delete-expression* is preceded by the
|
| 745 |
unary `::` operator, the deallocation function’s name is looked up in
|
| 746 |
global scope. Otherwise, the lookup considers class-specific
|
| 747 |
-
deallocation functions
|
| 748 |
-
|
| 749 |
-
|
| 750 |
|
| 751 |
If deallocation function lookup finds more than one usual deallocation
|
| 752 |
function, the function to be called is selected as follows:
|
| 753 |
|
|
|
|
|
|
|
|
|
|
| 754 |
- If the type has new-extended alignment, a function with a parameter of
|
| 755 |
type `std::align_val_t` is preferred; otherwise a function without
|
| 756 |
-
such a parameter is preferred. If
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
- If the deallocation functions have class scope, the one without a
|
| 761 |
parameter of type `std::size_t` is selected.
|
| 762 |
-
- If the type is complete and if, for
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
selected.
|
| 767 |
- Otherwise, it is unspecified whether a deallocation function with a
|
| 768 |
parameter of type `std::size_t` is selected.
|
| 769 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
When a *delete-expression* is executed, the selected deallocation
|
| 771 |
-
function shall be called with the address of the
|
| 772 |
-
|
| 773 |
-
for the array allocation overhead
|
| 774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
of type `std::align_val_t` is used, the alignment of the type of the
|
| 776 |
-
|
| 777 |
deallocation function with a parameter of type `std::size_t` is used,
|
| 778 |
-
the size of the
|
| 779 |
-
|
|
|
|
| 780 |
|
| 781 |
-
[*Note
|
| 782 |
-
and either the first argument was not the result of a prior
|
| 783 |
-
|
| 784 |
-
corresponding argument in said call, the behavior
|
| 785 |
-
[[new.delete.single]],
|
|
|
|
| 786 |
|
| 787 |
Access and ambiguity control are done for both the deallocation function
|
| 788 |
-
and the destructor ([[class.dtor]],
|
| 789 |
-
|
| 790 |
-
### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
| 791 |
-
|
| 792 |
-
An `alignof` expression yields the alignment requirement of its operand
|
| 793 |
-
type. The operand shall be a *type-id* representing a complete object
|
| 794 |
-
type, or an array thereof, or a reference to one of those types.
|
| 795 |
-
|
| 796 |
-
The result is an integral constant of type `std::size_t`.
|
| 797 |
-
|
| 798 |
-
When `alignof` is applied to a reference type, the result is the
|
| 799 |
-
alignment of the referenced type. When `alignof` is applied to an array
|
| 800 |
-
type, the result is the alignment of the element type.
|
| 801 |
-
|
| 802 |
-
### `noexcept` operator <a id="expr.unary.noexcept">[[expr.unary.noexcept]]</a>
|
| 803 |
-
|
| 804 |
-
The `noexcept` operator determines whether the evaluation of its
|
| 805 |
-
operand, which is an unevaluated operand (Clause [[expr]]), can throw
|
| 806 |
-
an exception ([[except.throw]]).
|
| 807 |
-
|
| 808 |
-
``` bnf
|
| 809 |
-
noexcept-expression:
|
| 810 |
-
'noexcept' '(' expression ')'
|
| 811 |
-
```
|
| 812 |
-
|
| 813 |
-
The result of the `noexcept` operator is a constant of type `bool` and
|
| 814 |
-
is a prvalue.
|
| 815 |
-
|
| 816 |
-
The result of the `noexcept` operator is `true` unless the *expression*
|
| 817 |
-
is potentially-throwing ([[except.spec]]).
|
| 818 |
|
|
|
|
| 1 |
+
### Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 2 |
|
| 3 |
Expressions with unary operators group right-to-left.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
unary-expression:
|
| 7 |
postfix-expression
|
| 8 |
+
unary-operator cast-expression
|
| 9 |
'++' cast-expression
|
| 10 |
'-{-}' cast-expression
|
| 11 |
+
await-expression
|
| 12 |
+
sizeof unary-expression
|
| 13 |
+
sizeof '(' type-id ')'
|
| 14 |
+
sizeof '...' '(' identifier ')'
|
| 15 |
+
alignof '(' type-id ')'
|
| 16 |
noexcept-expression
|
| 17 |
new-expression
|
| 18 |
delete-expression
|
| 19 |
```
|
| 20 |
|
| 21 |
``` bnf
|
| 22 |
unary-operator: one of
|
| 23 |
'* & + - ! ~'
|
| 24 |
```
|
| 25 |
|
| 26 |
+
#### Unary operators <a id="expr.unary.op">[[expr.unary.op]]</a>
|
| 27 |
|
| 28 |
The unary `*` operator performs *indirection*: the expression to which
|
| 29 |
it is applied shall be a pointer to an object type, or a pointer to a
|
| 30 |
function type and the result is an lvalue referring to the object or
|
| 31 |
function to which the expression points. If the type of the expression
|
| 32 |
is “pointer to `T`”, the type of the result is “`T`”.
|
| 33 |
|
| 34 |
[*Note 1*: Indirection through a pointer to an incomplete type (other
|
| 35 |
+
than cv `void`) is valid. The lvalue thus obtained can be used in
|
| 36 |
limited ways (to initialize a reference, for example); this lvalue must
|
| 37 |
not be converted to a prvalue, see [[conv.lval]]. — *end note*]
|
| 38 |
|
| 39 |
The result of each of the following unary operators is a prvalue.
|
| 40 |
|
| 41 |
+
The result of the unary `&` operator is a pointer to its operand.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
- If the operand is a *qualified-id* naming a non-static or variant
|
| 44 |
+
member `m` of some class `C` with type `T`, the result has type
|
| 45 |
+
“pointer to member of class `C` of type `T`” and is a prvalue
|
| 46 |
+
designating `C::m`.
|
| 47 |
+
- Otherwise, if the operand is an lvalue of type `T`, the resulting
|
| 48 |
+
expression is a prvalue of type “pointer to `T`” whose result is a
|
| 49 |
+
pointer to the designated object [[intro.memory]] or function.
|
| 50 |
+
\[*Note 2*: In particular, taking the address of a variable of type
|
| 51 |
+
“cv `T`” yields a pointer of type “pointer to cv `T`”. — *end note*]
|
| 52 |
+
- Otherwise, the program is ill-formed.
|
| 53 |
|
| 54 |
[*Example 1*:
|
| 55 |
|
| 56 |
``` cpp
|
| 57 |
struct A { int i; };
|
|
|
|
| 64 |
```
|
| 65 |
|
| 66 |
— *end example*]
|
| 67 |
|
| 68 |
[*Note 3*: A pointer to member formed from a `mutable` non-static data
|
| 69 |
+
member [[dcl.stc]] does not reflect the `mutable` specifier associated
|
| 70 |
+
with the non-static data member. — *end note*]
|
| 71 |
|
| 72 |
A pointer to member is only formed when an explicit `&` is used and its
|
| 73 |
operand is a *qualified-id* not enclosed in parentheses.
|
| 74 |
|
| 75 |
[*Note 4*: That is, the expression `&(qualified-id)`, where the
|
| 76 |
*qualified-id* is enclosed in parentheses, does not form an expression
|
| 77 |
of type “pointer to member”. Neither does `qualified-id`, because there
|
| 78 |
is no implicit conversion from a *qualified-id* for a non-static member
|
| 79 |
function to the type “pointer to member function” as there is from an
|
| 80 |
+
lvalue of function type to the type “pointer to function” [[conv.func]].
|
| 81 |
+
Nor is `&unqualified-id` a pointer to member, even within the scope of
|
| 82 |
+
the *unqualified-id*’s class. — *end note*]
|
| 83 |
|
| 84 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 85 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 86 |
the built-in meaning or the operator function is called. The operand of
|
| 87 |
`&` shall not be a bit-field.
|
| 88 |
|
| 89 |
+
[*Note 5*: The address of an overloaded function [[over]] can be taken
|
| 90 |
only in a context that uniquely determines which version of the
|
| 91 |
+
overloaded function is referred to (see [[over.over]]). Since the
|
| 92 |
+
context might determine whether the operand is a static or non-static
|
| 93 |
+
member function, the context can also affect whether the expression has
|
| 94 |
+
type “pointer to function” or “pointer to member
|
| 95 |
+
function”. — *end note*]
|
|
|
|
| 96 |
|
| 97 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 98 |
enumeration, or pointer type and the result is the value of the
|
| 99 |
argument. Integral promotion is performed on integral or enumeration
|
| 100 |
operands. The type of the result is the type of the promoted operand.
|
|
|
|
| 105 |
of an unsigned quantity is computed by subtracting its value from 2ⁿ,
|
| 106 |
where n is the number of bits in the promoted operand. The type of the
|
| 107 |
result is the type of the promoted operand.
|
| 108 |
|
| 109 |
The operand of the logical negation operator `!` is contextually
|
| 110 |
+
converted to `bool` [[conv]]; its value is `true` if the converted
|
| 111 |
+
operand is `false` and `false` otherwise. The type of the result is
|
| 112 |
+
`bool`.
|
| 113 |
|
| 114 |
The operand of `~` shall have integral or unscoped enumeration type; the
|
| 115 |
result is the ones’ complement of its operand. Integral promotions are
|
| 116 |
performed. The type of the result is the type of the promoted operand.
|
| 117 |
There is an ambiguity in the grammar when `~` is followed by a
|
| 118 |
+
*type-name* or *decltype-specifier*. The ambiguity is resolved by
|
| 119 |
treating `~` as the unary complement operator rather than as the start
|
| 120 |
of an *unqualified-id* naming a destructor.
|
| 121 |
|
| 122 |
[*Note 6*: Because the grammar does not permit an operator to follow
|
| 123 |
+
the `.`, `->`, or `::` tokens, a `~` followed by a *type-name* or
|
| 124 |
*decltype-specifier* in a member access expression or *qualified-id* is
|
| 125 |
unambiguously parsed as a destructor name. — *end note*]
|
| 126 |
|
| 127 |
+
#### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
| 128 |
|
| 129 |
+
The operand of prefix `++` is modified [[defns.access]] by adding `1`.
|
| 130 |
+
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 131 |
+
be an arithmetic type other than cv `bool`, or a pointer to a
|
| 132 |
+
completely-defined object type. An operand with volatile-qualified type
|
| 133 |
+
is deprecated; see [[depr.volatile.type]]. The result is the updated
|
| 134 |
+
operand; it is an lvalue, and it is a bit-field if the operand is a
|
| 135 |
+
bit-field. The expression `++x` is equivalent to `x+=1`.
|
| 136 |
|
| 137 |
+
[*Note 1*: See the discussions of addition [[expr.add]] and assignment
|
| 138 |
+
operators [[expr.ass]] for information on conversions. — *end note*]
|
|
|
|
| 139 |
|
| 140 |
+
The operand of prefix `\dcr` is modified [[defns.access]] by subtracting
|
| 141 |
+
`1`. The requirements on the operand of prefix `\dcr` and the properties
|
| 142 |
+
of its result are otherwise the same as those of prefix `++`.
|
| 143 |
|
| 144 |
[*Note 2*: For postfix increment and decrement, see
|
| 145 |
[[expr.post.incr]]. — *end note*]
|
| 146 |
|
| 147 |
+
#### Await <a id="expr.await">[[expr.await]]</a>
|
| 148 |
+
|
| 149 |
+
The `co_await` expression is used to suspend evaluation of a coroutine
|
| 150 |
+
[[dcl.fct.def.coroutine]] while awaiting completion of the computation
|
| 151 |
+
represented by the operand expression.
|
| 152 |
+
|
| 153 |
+
``` bnf
|
| 154 |
+
await-expression:
|
| 155 |
+
'co_await' cast-expression
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
An *await-expression* shall appear only in a potentially-evaluated
|
| 159 |
+
expression within the *compound-statement* of a *function-body* outside
|
| 160 |
+
of a *handler* [[except.pre]]. In a *declaration-statement* or in the
|
| 161 |
+
*simple-declaration* (if any) of a *for-init-statement*, an
|
| 162 |
+
*await-expression* shall appear only in an *initializer* of that
|
| 163 |
+
*declaration-statement* or *simple-declaration*. An *await-expression*
|
| 164 |
+
shall not appear in a default argument [[dcl.fct.default]]. An
|
| 165 |
+
*await-expression* shall not appear in the initializer of a block-scope
|
| 166 |
+
variable with static or thread storage duration. A context within a
|
| 167 |
+
function where an *await-expression* can appear is called a *suspension
|
| 168 |
+
context* of the function.
|
| 169 |
+
|
| 170 |
+
Evaluation of an *await-expression* involves the following auxiliary
|
| 171 |
+
types, expressions, and objects:
|
| 172 |
+
|
| 173 |
+
- *p* is an lvalue naming the promise object [[dcl.fct.def.coroutine]]
|
| 174 |
+
of the enclosing coroutine and `P` is the type of that object.
|
| 175 |
+
- *a* is the *cast-expression* if the *await-expression* was implicitly
|
| 176 |
+
produced by a *yield-expression* [[expr.yield]], an initial suspend
|
| 177 |
+
point, or a final suspend point [[dcl.fct.def.coroutine]]. Otherwise,
|
| 178 |
+
the *unqualified-id* `await_transform` is looked up within the scope
|
| 179 |
+
of `P` by class member access lookup [[basic.lookup.classref]], and if
|
| 180 |
+
this lookup finds at least one declaration, then *a* is
|
| 181 |
+
*p*`.await_transform(`*cast-expression*`)`; otherwise, *a* is the
|
| 182 |
+
*cast-expression*.
|
| 183 |
+
- *o* is determined by enumerating the applicable `operator co_await`
|
| 184 |
+
functions for an argument *a* [[over.match.oper]], and choosing the
|
| 185 |
+
best one through overload resolution [[over.match]]. If overload
|
| 186 |
+
resolution is ambiguous, the program is ill-formed. If no viable
|
| 187 |
+
functions are found, *o* is *a*. Otherwise, *o* is a call to the
|
| 188 |
+
selected function with the argument *a*. If *o* would be a prvalue,
|
| 189 |
+
the temporary materialization conversion [[conv.rval]] is applied.
|
| 190 |
+
- *e* is an lvalue referring to the result of evaluating the
|
| 191 |
+
(possibly-converted) *o*.
|
| 192 |
+
- *h* is an object of type `std::coroutine_handle<P>` referring to the
|
| 193 |
+
enclosing coroutine.
|
| 194 |
+
- *await-ready* is the expression *e*`.await_ready()`, contextually
|
| 195 |
+
converted to `bool`.
|
| 196 |
+
- *await-suspend* is the expression *e*`.await_suspend(`*h*`)`, which
|
| 197 |
+
shall be a prvalue of type `void`, `bool`, or
|
| 198 |
+
`std::coroutine_handle<Z>` for some type `Z`.
|
| 199 |
+
- *await-resume* is the expression *e*`.await_resume()`.
|
| 200 |
+
|
| 201 |
+
The *await-expression* has the same type and value category as the
|
| 202 |
+
*await-resume* expression.
|
| 203 |
+
|
| 204 |
+
The *await-expression* evaluates the (possibly-converted) *o* expression
|
| 205 |
+
and the *await-ready* expression, then:
|
| 206 |
+
|
| 207 |
+
- If the result of *await-ready* is `false`, the coroutine is considered
|
| 208 |
+
suspended. Then:
|
| 209 |
+
- If the type of *await-suspend* is `std::coroutine_handle<Z>`,
|
| 210 |
+
*await-suspend*`.resume()` is evaluated. \[*Note 1*: This resumes
|
| 211 |
+
the coroutine referred to by the result of *await-suspend*. Any
|
| 212 |
+
number of coroutines may be successively resumed in this fashion,
|
| 213 |
+
eventually returning control flow to the current coroutine caller or
|
| 214 |
+
resumer [[dcl.fct.def.coroutine]]. — *end note*]
|
| 215 |
+
- Otherwise, if the type of *await-suspend* is `bool`, *await-suspend*
|
| 216 |
+
is evaluated, and the coroutine is resumed if the result is `false`.
|
| 217 |
+
- Otherwise, *await-suspend* is evaluated.
|
| 218 |
+
|
| 219 |
+
If the evaluation of *await-suspend* exits via an exception, the
|
| 220 |
+
exception is caught, the coroutine is resumed, and the exception is
|
| 221 |
+
immediately re-thrown [[except.throw]]. Otherwise, control flow
|
| 222 |
+
returns to the current coroutine caller or resumer
|
| 223 |
+
[[dcl.fct.def.coroutine]] without exiting any scopes [[stmt.jump]].
|
| 224 |
+
- If the result of *await-ready* is `true`, or when the coroutine is
|
| 225 |
+
resumed, the *await-resume* expression is evaluated, and its result is
|
| 226 |
+
the result of the *await-expression*.
|
| 227 |
+
|
| 228 |
+
[*Example 1*:
|
| 229 |
+
|
| 230 |
+
``` cpp
|
| 231 |
+
template <typename T>
|
| 232 |
+
struct my_future {
|
| 233 |
+
...
|
| 234 |
+
bool await_ready();
|
| 235 |
+
void await_suspend(std::coroutine_handle<>);
|
| 236 |
+
T await_resume();
|
| 237 |
+
};
|
| 238 |
+
|
| 239 |
+
template <class Rep, class Period>
|
| 240 |
+
auto operator co_await(std::chrono::duration<Rep, Period> d) {
|
| 241 |
+
struct awaiter {
|
| 242 |
+
std::chrono::system_clock::duration duration;
|
| 243 |
+
...
|
| 244 |
+
awaiter(std::chrono::system_clock::duration d) : duration(d) {}
|
| 245 |
+
bool await_ready() const { return duration.count() <= 0; }
|
| 246 |
+
void await_resume() {}
|
| 247 |
+
void await_suspend(std::coroutine_handle<> h) { ... }
|
| 248 |
+
};
|
| 249 |
+
return awaiter{d};
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
using namespace std::chrono;
|
| 253 |
+
|
| 254 |
+
my_future<int> h();
|
| 255 |
+
|
| 256 |
+
my_future<void> g() {
|
| 257 |
+
std::cout << "just about go to sleep...\n";
|
| 258 |
+
co_await 10ms;
|
| 259 |
+
std::cout << "resumed\n";
|
| 260 |
+
co_await h();
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
auto f(int x = co_await h()); // error: await-expression outside of function suspension context
|
| 264 |
+
int a[] = { co_await h() }; // error: await-expression outside of function suspension context
|
| 265 |
+
```
|
| 266 |
+
|
| 267 |
+
— *end example*]
|
| 268 |
+
|
| 269 |
+
#### Sizeof <a id="expr.sizeof">[[expr.sizeof]]</a>
|
| 270 |
+
|
| 271 |
+
The `sizeof` operator yields the number of bytes occupied by a
|
| 272 |
+
non-potentially-overlapping object of the type of its operand. The
|
| 273 |
+
operand is either an expression, which is an unevaluated operand
|
| 274 |
+
[[expr.prop]], or a parenthesized *type-id*. The `sizeof` operator shall
|
| 275 |
+
not be applied to an expression that has function or incomplete type, to
|
| 276 |
+
the parenthesized name of such types, or to a glvalue that designates a
|
| 277 |
+
bit-field. The result of `sizeof` applied to any of the narrow character
|
| 278 |
+
types is `1`. The result of `sizeof` applied to any other fundamental
|
| 279 |
+
type [[basic.fundamental]] is *implementation-defined*.
|
| 280 |
|
| 281 |
[*Note 1*: In particular, `sizeof(bool)`, `sizeof(char16_t)`,
|
| 282 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 283 |
+
implementation-defined.[^21] — *end note*]
|
| 284 |
|
| 285 |
+
[*Note 2*: See [[intro.memory]] for the definition of byte and
|
| 286 |
+
[[basic.types]] for the definition of object
|
| 287 |
+
representation. — *end note*]
|
| 288 |
|
| 289 |
+
When applied to a reference type, the result is the size of the
|
| 290 |
+
referenced type. When applied to a class, the result is the number of
|
| 291 |
+
bytes in an object of that class including any padding required for
|
| 292 |
+
placing objects of that type in an array. The result of applying
|
| 293 |
+
`sizeof` to a potentially-overlapping subobject is the size of the type,
|
| 294 |
+
not the size of the subobject. [^22] When applied to an array, the
|
| 295 |
+
result is the total number of bytes in the array. This implies that the
|
| 296 |
+
size of an array of n elements is n times the size of an element.
|
|
|
|
| 297 |
|
| 298 |
+
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 299 |
+
function-to-pointer [[conv.func]] standard conversions are not applied
|
| 300 |
+
to the operand of `sizeof`. If the operand is a prvalue, the temporary
|
| 301 |
+
materialization conversion [[conv.rval]] is applied.
|
| 302 |
|
| 303 |
+
The identifier in a `sizeof...` expression shall name a pack. The
|
| 304 |
+
`sizeof...` operator yields the number of elements in the pack
|
| 305 |
+
[[temp.variadic]]. A `sizeof...` expression is a pack expansion
|
| 306 |
+
[[temp.variadic]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
[*Example 1*:
|
| 309 |
|
| 310 |
``` cpp
|
| 311 |
template<class... Types>
|
|
|
|
| 314 |
};
|
| 315 |
```
|
| 316 |
|
| 317 |
— *end example*]
|
| 318 |
|
| 319 |
+
The result of `sizeof` and `sizeof...` is a prvalue of type
|
| 320 |
`std::size_t`.
|
| 321 |
|
| 322 |
+
[*Note 3*: A `sizeof` expression is an integral constant expression
|
| 323 |
+
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 324 |
`<cstddef>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 325 |
|
| 326 |
+
#### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
| 327 |
|
| 328 |
+
An `alignof` expression yields the alignment requirement of its operand
|
| 329 |
+
type. The operand shall be a *type-id* representing a complete object
|
| 330 |
+
type, or an array thereof, or a reference to one of those types.
|
| 331 |
+
|
| 332 |
+
The result is a prvalue of type `std::size_t`.
|
| 333 |
+
|
| 334 |
+
[*Note 1*: An `alignof` expression is an integral constant expression
|
| 335 |
+
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 336 |
+
`<cstddef>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 337 |
+
|
| 338 |
+
When `alignof` is applied to a reference type, the result is the
|
| 339 |
+
alignment of the referenced type. When `alignof` is applied to an array
|
| 340 |
+
type, the result is the alignment of the element type.
|
| 341 |
+
|
| 342 |
+
#### `noexcept` operator <a id="expr.unary.noexcept">[[expr.unary.noexcept]]</a>
|
| 343 |
+
|
| 344 |
+
The `noexcept` operator determines whether the evaluation of its
|
| 345 |
+
operand, which is an unevaluated operand [[expr.prop]], can throw an
|
| 346 |
+
exception [[except.throw]].
|
| 347 |
+
|
| 348 |
+
``` bnf
|
| 349 |
+
noexcept-expression:
|
| 350 |
+
noexcept '(' expression ')'
|
| 351 |
+
```
|
| 352 |
+
|
| 353 |
+
The result of the `noexcept` operator is a prvalue of type `bool`.
|
| 354 |
+
|
| 355 |
+
[*Note 1*: A *noexcept-expression* is an integral constant expression
|
| 356 |
+
[[expr.const]]. — *end note*]
|
| 357 |
+
|
| 358 |
+
The result of the `noexcept` operator is `true` unless the *expression*
|
| 359 |
+
is potentially-throwing [[except.spec]].
|
| 360 |
+
|
| 361 |
+
#### New <a id="expr.new">[[expr.new]]</a>
|
| 362 |
+
|
| 363 |
+
The *new-expression* attempts to create an object of the *type-id*
|
| 364 |
+
[[dcl.name]] or *new-type-id* to which it is applied. The type of that
|
| 365 |
object is the *allocated type*. This type shall be a complete object
|
| 366 |
type, but not an abstract class type or array thereof (
|
| 367 |
+
[[intro.object]], [[basic.types]], [[class.abstract]]).
|
| 368 |
|
| 369 |
[*Note 1*: Because references are not objects, references cannot be
|
| 370 |
created by *new-expression*s. — *end note*]
|
| 371 |
|
| 372 |
[*Note 2*: The *type-id* may be a cv-qualified type, in which case the
|
| 373 |
object created by the *new-expression* has a cv-qualified
|
| 374 |
type. — *end note*]
|
| 375 |
|
| 376 |
``` bnf
|
| 377 |
new-expression:
|
| 378 |
+
'::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 379 |
+
'::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
| 380 |
```
|
| 381 |
|
| 382 |
``` bnf
|
| 383 |
new-placement:
|
| 384 |
'(' expression-list ')'
|
|
|
|
| 395 |
noptr-new-declarator
|
| 396 |
```
|
| 397 |
|
| 398 |
``` bnf
|
| 399 |
noptr-new-declarator:
|
| 400 |
+
'[' expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 401 |
noptr-new-declarator '[' constant-expression ']' attribute-specifier-seqₒₚₜ
|
| 402 |
```
|
| 403 |
|
| 404 |
``` bnf
|
| 405 |
new-initializer:
|
| 406 |
'(' expression-listₒₚₜ ')'
|
| 407 |
braced-init-list
|
| 408 |
```
|
| 409 |
|
| 410 |
+
If a placeholder type [[dcl.spec.auto]] appears in the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
*type-specifier-seq* of a *new-type-id* or *type-id* of a
|
| 412 |
*new-expression*, the allocated type is deduced as follows: Let *init*
|
| 413 |
be the *new-initializer*, if any, and `T` be the *new-type-id* or
|
| 414 |
*type-id* of the *new-expression*, then the allocated type is the type
|
| 415 |
+
deduced for the variable `x` in the invented declaration
|
| 416 |
+
[[dcl.spec.auto]]:
|
| 417 |
|
| 418 |
``` cpp
|
| 419 |
T x init ;
|
| 420 |
```
|
| 421 |
|
|
|
|
| 432 |
— *end example*]
|
| 433 |
|
| 434 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 435 |
of *new-declarator*s.
|
| 436 |
|
| 437 |
+
[*Note 3*: This prevents ambiguities between the declarator operators
|
| 438 |
`&`, `&&`, `*`, and `[]` and their expression
|
| 439 |
counterparts. — *end note*]
|
| 440 |
|
| 441 |
[*Example 2*:
|
| 442 |
|
|
|
|
| 446 |
|
| 447 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 448 |
|
| 449 |
— *end example*]
|
| 450 |
|
| 451 |
+
[*Note 4*:
|
| 452 |
|
| 453 |
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 454 |
effects.
|
| 455 |
|
| 456 |
[*Example 3*:
|
|
|
|
| 464 |
``` cpp
|
| 465 |
(new int) (*[10])(); // error
|
| 466 |
```
|
| 467 |
|
| 468 |
Instead, the explicitly parenthesized version of the `new` operator can
|
| 469 |
+
be used to create objects of compound types [[basic.compound]]:
|
| 470 |
|
| 471 |
``` cpp
|
| 472 |
new (int (*[10])());
|
| 473 |
```
|
| 474 |
|
|
|
|
| 477 |
|
| 478 |
— *end example*]
|
| 479 |
|
| 480 |
— *end note*]
|
| 481 |
|
| 482 |
+
Objects created by a *new-expression* have dynamic storage duration
|
| 483 |
+
[[basic.stc.dynamic]].
|
| 484 |
+
|
| 485 |
+
[*Note 5*: The lifetime of such an object is not necessarily
|
| 486 |
+
restricted to the scope in which it is created. — *end note*]
|
| 487 |
+
|
| 488 |
+
When the allocated object is not an array, the result of the
|
| 489 |
+
*new-expression* is a pointer to the object created.
|
| 490 |
+
|
| 491 |
When the allocated object is an array (that is, the
|
| 492 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 493 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 494 |
initial element (if any) of the array.
|
| 495 |
|
|
|
|
| 498 |
|
| 499 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 500 |
the associated array type.
|
| 501 |
|
| 502 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 503 |
+
converted constant expression [[expr.const]] of type `std::size_t` and
|
| 504 |
+
its value shall be greater than zero.
|
|
|
|
| 505 |
|
| 506 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 507 |
well-formed (because `n` is the *expression* of a
|
| 508 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 509 |
`n` is not a constant expression). — *end example*]
|
| 510 |
|
| 511 |
+
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 512 |
+
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 513 |
+
object is an array with `n` elements, where `n` is determined from the
|
| 514 |
+
number of initial elements supplied in the *new-initializer* (
|
| 515 |
+
[[dcl.init.aggr]], [[dcl.init.string]]).
|
| 516 |
+
|
| 517 |
+
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 518 |
+
implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
| 519 |
|
| 520 |
- the expression is of non-class type and its value before converting to
|
| 521 |
`std::size_t` is less than zero;
|
| 522 |
- the expression is of class type and its value before application of
|
| 523 |
+
the second standard conversion [[over.ics.user]][^23] is less than
|
| 524 |
zero;
|
| 525 |
- its value is such that the size of the allocated object would exceed
|
| 526 |
+
the *implementation-defined* limit [[implimits]]; or
|
| 527 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 528 |
elements for which initializers are provided (including the
|
| 529 |
+
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 530 |
number of elements to initialize.
|
| 531 |
|
| 532 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 533 |
|
| 534 |
- if the *expression* is a core constant expression, the program is
|
| 535 |
ill-formed;
|
| 536 |
- otherwise, an allocation function is not called; instead
|
| 537 |
- if the allocation function that would have been called has a
|
| 538 |
+
non-throwing exception specification [[except.spec]], the value of
|
| 539 |
+
the *new-expression* is the null pointer value of the required
|
| 540 |
result type;
|
| 541 |
- otherwise, the *new-expression* terminates by throwing an exception
|
| 542 |
+
of a type that would match a handler [[except.handle]] of type
|
| 543 |
+
`std::bad_array_new_length` [[new.badlength]].
|
| 544 |
|
| 545 |
When the value of the *expression* is zero, the allocation function is
|
| 546 |
called to allocate an array with no elements.
|
| 547 |
|
| 548 |
A *new-expression* may obtain storage for the object by calling an
|
| 549 |
+
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 550 |
*new-expression* terminates by throwing an exception, it may release
|
| 551 |
+
storage by calling a deallocation function
|
| 552 |
+
[[basic.stc.dynamic.deallocation]]. If the allocated type is a non-array
|
| 553 |
+
type, the allocation function’s name is `operator new` and the
|
| 554 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 555 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 556 |
the deallocation function’s name is `operator delete[]`.
|
| 557 |
|
| 558 |
+
[*Note 7*: An implementation is required to provide default definitions
|
| 559 |
+
for the global allocation functions ([[basic.stc.dynamic]],
|
| 560 |
+
[[new.delete.single]], [[new.delete.array]]). A C++ program can provide
|
| 561 |
+
alternative definitions of these functions [[replacement.functions]]
|
| 562 |
+
and/or class-specific versions [[class.free]]. The set of allocation and
|
| 563 |
+
deallocation functions that may be called by a *new-expression* may
|
| 564 |
include functions that do not perform allocation or deallocation; for
|
| 565 |
example, see [[new.delete.placement]]. — *end note*]
|
| 566 |
|
| 567 |
If the *new-expression* begins with a unary `::` operator, the
|
| 568 |
allocation function’s name is looked up in the global scope. Otherwise,
|
|
|
|
| 572 |
type, the allocation function’s name is looked up in the global scope.
|
| 573 |
|
| 574 |
An implementation is allowed to omit a call to a replaceable global
|
| 575 |
allocation function ([[new.delete.single]], [[new.delete.array]]). When
|
| 576 |
it does so, the storage is instead provided by the implementation or
|
| 577 |
+
provided by extending the allocation of another *new-expression*.
|
| 578 |
+
|
| 579 |
+
During an evaluation of a constant expression, a call to an allocation
|
| 580 |
+
function is always omitted.
|
| 581 |
+
|
| 582 |
+
[*Note 8*: Only *new-expression*s that would otherwise result in a call
|
| 583 |
+
to a replaceable global allocation function can be evaluated in constant
|
| 584 |
+
expressions [[expr.const]]. — *end note*]
|
| 585 |
+
|
| 586 |
+
The implementation may extend the allocation of a *new-expression* `e1`
|
| 587 |
+
to provide storage for a *new-expression* `e2` if the following would be
|
| 588 |
true were the allocation not extended:
|
| 589 |
|
| 590 |
- the evaluation of `e1` is sequenced before the evaluation of `e2`, and
|
| 591 |
- `e2` is evaluated whenever `e1` obtains storage, and
|
| 592 |
- both `e1` and `e2` invoke the same replaceable global allocation
|
|
|
|
| 601 |
`e1`.
|
| 602 |
|
| 603 |
[*Example 5*:
|
| 604 |
|
| 605 |
``` cpp
|
| 606 |
+
void can_merge(int x) {
|
| 607 |
// These allocations are safe for merging:
|
| 608 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 609 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
| 610 |
std::unique_ptr<char[]> c{new (std::nothrow) char[x]};
|
| 611 |
|
| 612 |
g(a.get(), b.get(), c.get());
|
| 613 |
}
|
| 614 |
|
| 615 |
+
void cannot_merge(int x) {
|
| 616 |
std::unique_ptr<char[]> a{new char[8]};
|
| 617 |
try {
|
| 618 |
// Merging this allocation would change its catch handler.
|
| 619 |
std::unique_ptr<char[]> b{new char[x]};
|
| 620 |
} catch (const std::bad_alloc& e) {
|
|
|
|
| 629 |
When a *new-expression* calls an allocation function and that allocation
|
| 630 |
has not been extended, the *new-expression* passes the amount of space
|
| 631 |
requested to the allocation function as the first argument of type
|
| 632 |
`std::size_t`. That argument shall be no less than the size of the
|
| 633 |
object being created; it may be greater than the size of the object
|
| 634 |
+
being created only if the object is an array and the allocation function
|
| 635 |
+
is not a non-allocating form [[new.delete.placement]]. For arrays of
|
| 636 |
+
`char`, `unsigned char`, and `std::byte`, the difference between the
|
| 637 |
+
result of the *new-expression* and the address returned by the
|
| 638 |
+
allocation function shall be an integral multiple of the strictest
|
| 639 |
+
fundamental alignment requirement [[basic.align]] of any object type
|
| 640 |
+
whose size is no greater than the size of the array being created.
|
| 641 |
|
| 642 |
+
[*Note 9*: Because allocation functions are assumed to return pointers
|
| 643 |
to storage that is appropriately aligned for objects of any type with
|
| 644 |
fundamental alignment, this constraint on array allocation overhead
|
| 645 |
permits the common idiom of allocating character arrays into which
|
| 646 |
objects of other types will later be placed. — *end note*]
|
| 647 |
|
|
|
|
| 660 |
assembling an argument list. The first argument is the amount of space
|
| 661 |
requested, and has type `std::size_t`. If the type of the allocated
|
| 662 |
object has new-extended alignment, the next argument is the type’s
|
| 663 |
alignment, and has type `std::align_val_t`. If the *new-placement*
|
| 664 |
syntax is used, the *initializer-clause*s in its *expression-list* are
|
| 665 |
+
the succeeding arguments. If no matching function is found then
|
| 666 |
+
|
| 667 |
+
- if the allocated object type has new-extended alignment, the alignment
|
| 668 |
+
argument is removed from the argument list;
|
| 669 |
+
- otherwise, an argument that is the type’s alignment and has type
|
| 670 |
+
`std::align_val_t` is added into the argument list immediately after
|
| 671 |
+
the first argument;
|
| 672 |
+
|
| 673 |
+
and then overload resolution is performed again.
|
| 674 |
|
| 675 |
[*Example 6*:
|
| 676 |
|
| 677 |
- `new T` results in one of the following calls:
|
| 678 |
``` cpp
|
|
|
|
| 697 |
|
| 698 |
Here, each instance of `x` is a non-negative unspecified value
|
| 699 |
representing array allocation overhead; the result of the
|
| 700 |
*new-expression* will be offset by this amount from the value returned
|
| 701 |
by `operator new[]`. This overhead may be applied in all array
|
| 702 |
+
*new-expression*s, including those referencing a placement allocation
|
| 703 |
+
function, except when referencing the library function
|
| 704 |
+
`operator new[](std::size_t, void*)`. The amount of overhead may vary
|
| 705 |
+
from one invocation of `new` to another.
|
| 706 |
|
| 707 |
— *end example*]
|
| 708 |
|
| 709 |
+
[*Note 10*: Unless an allocation function has a non-throwing exception
|
| 710 |
+
specification [[except.spec]], it indicates failure to allocate storage
|
| 711 |
+
by throwing a `std::bad_alloc` exception (
|
| 712 |
+
[[basic.stc.dynamic.allocation]], [[except]], [[bad.alloc]]); it returns
|
| 713 |
+
a non-null pointer otherwise. If the allocation function has a
|
| 714 |
+
non-throwing exception specification, it returns null to indicate
|
| 715 |
failure to allocate storage and a non-null pointer
|
| 716 |
otherwise. — *end note*]
|
| 717 |
|
| 718 |
+
If the allocation function is a non-allocating form
|
| 719 |
+
[[new.delete.placement]] that returns null, the behavior is undefined.
|
| 720 |
Otherwise, if the allocation function returns null, initialization shall
|
| 721 |
not be done, the deallocation function shall not be called, and the
|
| 722 |
value of the *new-expression* shall be null.
|
| 723 |
|
| 724 |
+
[*Note 11*: When the allocation function returns a value other than
|
| 725 |
null, it must be a pointer to a block of storage in which space for the
|
| 726 |
object has been reserved. The block of storage is assumed to be
|
| 727 |
appropriately aligned and of the requested size. The address of the
|
| 728 |
created object will not necessarily be the same as that of the block if
|
| 729 |
the object is an array. — *end note*]
|
| 730 |
|
| 731 |
A *new-expression* that creates an object of type `T` initializes that
|
| 732 |
object as follows:
|
| 733 |
|
| 734 |
+
- If the *new-initializer* is omitted, the object is default-initialized
|
| 735 |
+
[[dcl.init]]. \[*Note 12*: If no initialization is performed, the
|
| 736 |
+
object has an indeterminate value. — *end note*]
|
| 737 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 738 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 739 |
|
| 740 |
The invocation of the allocation function is sequenced before the
|
| 741 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 742 |
the allocated object is sequenced before the value computation of the
|
| 743 |
*new-expression*.
|
| 744 |
|
| 745 |
If the *new-expression* creates an object or an array of objects of
|
| 746 |
class type, access and ambiguity control are done for the allocation
|
| 747 |
+
function, the deallocation function [[class.free]], and the constructor
|
| 748 |
+
[[class.ctor]] selected for the initialization (if any). If the
|
| 749 |
+
*new-expression* creates an array of objects of class type, the
|
| 750 |
+
destructor is potentially invoked [[class.dtor]].
|
| 751 |
|
| 752 |
+
If any part of the object initialization described above[^24] terminates
|
| 753 |
by throwing an exception and a suitable deallocation function can be
|
| 754 |
found, the deallocation function is called to free the memory in which
|
| 755 |
the object was being constructed, after which the exception continues to
|
| 756 |
propagate in the context of the *new-expression*. If no unambiguous
|
| 757 |
matching deallocation function can be found, propagating the exception
|
| 758 |
does not cause the object’s memory to be freed.
|
| 759 |
|
| 760 |
+
[*Note 13*: This is appropriate when the called allocation function
|
| 761 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 762 |
leak. — *end note*]
|
| 763 |
|
| 764 |
If the *new-expression* begins with a unary `::` operator, the
|
| 765 |
deallocation function’s name is looked up in the global scope.
|
|
|
|
| 769 |
not a class type or array thereof, the deallocation function’s name is
|
| 770 |
looked up in the global scope.
|
| 771 |
|
| 772 |
A declaration of a placement deallocation function matches the
|
| 773 |
declaration of a placement allocation function if it has the same number
|
| 774 |
+
of parameters and, after parameter transformations [[dcl.fct]], all
|
| 775 |
parameter types except the first are identical. If the lookup finds a
|
| 776 |
single matching deallocation function, that function will be called;
|
| 777 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 778 |
+
a usual deallocation function and that function, considered as a
|
|
|
|
| 779 |
placement deallocation function, would have been selected as a match for
|
| 780 |
the allocation function, the program is ill-formed. For a non-placement
|
| 781 |
allocation function, the normal deallocation function lookup is used to
|
| 782 |
+
find the matching deallocation function [[expr.delete]].
|
| 783 |
|
| 784 |
[*Example 7*:
|
| 785 |
|
| 786 |
``` cpp
|
| 787 |
struct S {
|
|
|
|
| 790 |
|
| 791 |
// Usual (non-placement) deallocation function:
|
| 792 |
static void operator delete(void*, std::size_t);
|
| 793 |
};
|
| 794 |
|
| 795 |
+
S* p = new (0) S; // error: non-placement deallocation function matches
|
| 796 |
// placement allocation function
|
| 797 |
```
|
| 798 |
|
| 799 |
— *end example*]
|
| 800 |
|
| 801 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 802 |
returned from the allocation function call as the first argument of type
|
| 803 |
`void*`. If a placement deallocation function is called, it is passed
|
| 804 |
the same additional arguments as were passed to the placement allocation
|
| 805 |
function, that is, the same arguments as those specified with the
|
| 806 |
+
*new-placement* syntax. If the implementation is allowed to introduce a
|
| 807 |
+
temporary object or make a copy of any argument as part of the call to
|
| 808 |
+
the allocation function, it is unspecified whether the same object is
|
| 809 |
+
used in the call to both the allocation and deallocation functions.
|
|
|
|
|
|
|
| 810 |
|
| 811 |
+
#### Delete <a id="expr.delete">[[expr.delete]]</a>
|
| 812 |
|
| 813 |
+
The *delete-expression* operator destroys a most derived object
|
| 814 |
+
[[intro.object]] or array created by a *new-expression*.
|
| 815 |
|
| 816 |
``` bnf
|
| 817 |
delete-expression:
|
| 818 |
+
'::'ₒₚₜ delete cast-expression
|
| 819 |
+
'::'ₒₚₜ delete '[' ']' cast-expression
|
| 820 |
```
|
| 821 |
|
| 822 |
+
The first alternative is a *single-object delete expression*, and the
|
| 823 |
+
second is an *array delete expression*. Whenever the `delete` keyword is
|
| 824 |
+
immediately followed by empty square brackets, it shall be interpreted
|
| 825 |
+
as the second alternative.[^25] The operand shall be of pointer to
|
| 826 |
+
object type or of class type. If of class type, the operand is
|
| 827 |
+
contextually implicitly converted [[conv]] to a pointer to object
|
| 828 |
+
type.[^26] The *delete-expression*’s result has type `void`.
|
| 829 |
|
| 830 |
If the operand has a class type, the operand is converted to a pointer
|
| 831 |
type by calling the above-mentioned conversion function, and the
|
| 832 |
converted operand is used in place of the original operand for the
|
| 833 |
+
remainder of this subclause. In a single-object delete expression, the
|
| 834 |
+
value of the operand of `delete` may be a null pointer value, a pointer
|
| 835 |
+
to a non-array object created by a previous *new-expression*, or a
|
| 836 |
+
pointer to a subobject [[intro.object]] representing a base class of
|
| 837 |
+
such an object [[class.derived]]. If not, the behavior is undefined. In
|
| 838 |
+
an array delete expression, the value of the operand of `delete` may be
|
| 839 |
+
a null pointer value or a pointer value that resulted from a previous
|
| 840 |
+
array *new-expression*.[^27] If not, the behavior is undefined.
|
|
|
|
| 841 |
|
| 842 |
[*Note 1*: This means that the syntax of the *delete-expression* must
|
| 843 |
match the type of the object allocated by `new`, not the syntax of the
|
| 844 |
*new-expression*. — *end note*]
|
| 845 |
|
| 846 |
[*Note 2*: A pointer to a `const` type can be the operand of a
|
| 847 |
+
*delete-expression*; it is not necessary to cast away the constness
|
| 848 |
+
[[expr.const.cast]] of the pointer expression before it is used as the
|
| 849 |
operand of the *delete-expression*. — *end note*]
|
| 850 |
|
| 851 |
+
In a single-object delete expression, if the static type of the object
|
| 852 |
+
to be deleted is different from its dynamic type and the selected
|
| 853 |
+
deallocation function (see below) is not a destroying operator delete,
|
| 854 |
+
the static type shall be a base class of the dynamic type of the object
|
| 855 |
+
to be deleted and the static type shall have a virtual destructor or the
|
| 856 |
+
behavior is undefined. In an array delete expression, if the dynamic
|
| 857 |
type of the object to be deleted differs from its static type, the
|
| 858 |
behavior is undefined.
|
| 859 |
|
| 860 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 861 |
exactly once.
|
|
|
|
| 863 |
If the object being deleted has incomplete class type at the point of
|
| 864 |
deletion and the complete class has a non-trivial destructor or a
|
| 865 |
deallocation function, the behavior is undefined.
|
| 866 |
|
| 867 |
If the value of the operand of the *delete-expression* is not a null
|
| 868 |
+
pointer value and the selected deallocation function (see below) is not
|
| 869 |
+
a destroying operator delete, the *delete-expression* will invoke the
|
| 870 |
+
destructor (if any) for the object or the elements of the array being
|
| 871 |
+
deleted. In the case of an array, the elements will be destroyed in
|
| 872 |
+
order of decreasing address (that is, in reverse order of the completion
|
| 873 |
+
of their constructor; see [[class.base.init]]).
|
| 874 |
|
| 875 |
If the value of the operand of the *delete-expression* is not a null
|
| 876 |
pointer value, then:
|
| 877 |
|
| 878 |
- If the allocation call for the *new-expression* for the object to be
|
| 879 |
+
deleted was not omitted and the allocation was not extended
|
| 880 |
+
[[expr.new]], the *delete-expression* shall call a deallocation
|
| 881 |
+
function [[basic.stc.dynamic.deallocation]]. The value returned from
|
| 882 |
+
the allocation call of the *new-expression* shall be passed as the
|
| 883 |
+
first argument to the deallocation function.
|
| 884 |
- Otherwise, if the allocation was extended or was provided by extending
|
| 885 |
the allocation of another *new-expression*, and the
|
| 886 |
*delete-expression* for every other pointer value produced by a
|
| 887 |
*new-expression* that had storage provided by the extended
|
| 888 |
*new-expression* has been evaluated, the *delete-expression* shall
|
|
|
|
| 899 |
If the value of the operand of the *delete-expression* is a null pointer
|
| 900 |
value, it is unspecified whether a deallocation function will be called
|
| 901 |
as described above.
|
| 902 |
|
| 903 |
[*Note 4*: An implementation provides default definitions of the global
|
| 904 |
+
deallocation functions `operator delete` for non-arrays
|
| 905 |
+
[[new.delete.single]] and `operator delete[]` for arrays
|
| 906 |
+
[[new.delete.array]]. A C++ program can provide alternative definitions
|
| 907 |
+
of these functions [[replacement.functions]], and/or class-specific
|
| 908 |
+
versions [[class.free]]. — *end note*]
|
| 909 |
|
| 910 |
When the keyword `delete` in a *delete-expression* is preceded by the
|
| 911 |
unary `::` operator, the deallocation function’s name is looked up in
|
| 912 |
global scope. Otherwise, the lookup considers class-specific
|
| 913 |
+
deallocation functions [[class.free]]. If no class-specific deallocation
|
| 914 |
+
function is found, the deallocation function’s name is looked up in
|
| 915 |
+
global scope.
|
| 916 |
|
| 917 |
If deallocation function lookup finds more than one usual deallocation
|
| 918 |
function, the function to be called is selected as follows:
|
| 919 |
|
| 920 |
+
- If any of the deallocation functions is a destroying operator delete,
|
| 921 |
+
all deallocation functions that are not destroying operator deletes
|
| 922 |
+
are eliminated from further consideration.
|
| 923 |
- If the type has new-extended alignment, a function with a parameter of
|
| 924 |
type `std::align_val_t` is preferred; otherwise a function without
|
| 925 |
+
such a parameter is preferred. If any preferred functions are found,
|
| 926 |
+
all non-preferred functions are eliminated from further consideration.
|
| 927 |
+
- If exactly one function remains, that function is selected and the
|
| 928 |
+
selection process terminates.
|
| 929 |
- If the deallocation functions have class scope, the one without a
|
| 930 |
parameter of type `std::size_t` is selected.
|
| 931 |
+
- If the type is complete and if, for an array delete expression only,
|
| 932 |
+
the operand is a pointer to a class type with a non-trivial destructor
|
| 933 |
+
or a (possibly multi-dimensional) array thereof, the function with a
|
| 934 |
+
parameter of type `std::size_t` is selected.
|
|
|
|
| 935 |
- Otherwise, it is unspecified whether a deallocation function with a
|
| 936 |
parameter of type `std::size_t` is selected.
|
| 937 |
|
| 938 |
+
For a single-object delete expression, the deleted object is the object
|
| 939 |
+
denoted by the operand if its static type does not have a virtual
|
| 940 |
+
destructor, and its most-derived object otherwise.
|
| 941 |
+
|
| 942 |
+
[*Note 5*: If the deallocation function is not a destroying operator
|
| 943 |
+
delete and the deleted object is not the most derived object in the
|
| 944 |
+
former case, the behavior is undefined, as stated above. — *end note*]
|
| 945 |
+
|
| 946 |
+
For an array delete expression, the deleted object is the array object.
|
| 947 |
When a *delete-expression* is executed, the selected deallocation
|
| 948 |
+
function shall be called with the address of the deleted object in a
|
| 949 |
+
single-object delete expression, or the address of the deleted object
|
| 950 |
+
suitably adjusted for the array allocation overhead [[expr.new]] in an
|
| 951 |
+
array delete expression, as its first argument.
|
| 952 |
+
|
| 953 |
+
[*Note 6*: Any cv-qualifiers in the type of the deleted object are
|
| 954 |
+
ignored when forming this argument. — *end note*]
|
| 955 |
+
|
| 956 |
+
If a destroying operator delete is used, an unspecified value is passed
|
| 957 |
+
as the argument corresponding to the parameter of type
|
| 958 |
+
`std::destroying_delete_t`. If a deallocation function with a parameter
|
| 959 |
of type `std::align_val_t` is used, the alignment of the type of the
|
| 960 |
+
deleted object is passed as the corresponding argument. If a
|
| 961 |
deallocation function with a parameter of type `std::size_t` is used,
|
| 962 |
+
the size of the deleted object in a single-object delete expression, or
|
| 963 |
+
of the array plus allocation overhead in an array delete expression, is
|
| 964 |
+
passed as the corresponding argument.
|
| 965 |
|
| 966 |
+
[*Note 7*: If this results in a call to a replaceable deallocation
|
| 967 |
+
function, and either the first argument was not the result of a prior
|
| 968 |
+
call to a replaceable allocation function or the second or third
|
| 969 |
+
argument was not the corresponding argument in said call, the behavior
|
| 970 |
+
is undefined ([[new.delete.single]],
|
| 971 |
+
[[new.delete.array]]). — *end note*]
|
| 972 |
|
| 973 |
Access and ambiguity control are done for both the deallocation function
|
| 974 |
+
and the destructor ([[class.dtor]], [[class.free]]).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
|