- tmp/tmpq7dej5ho/{from.md → to.md} +213 -165
tmp/tmpq7dej5ho/{from.md → to.md}
RENAMED
|
@@ -1,15 +1,19 @@
|
|
| 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 |
-
'-
|
| 11 |
await-expression
|
| 12 |
sizeof unary-expression
|
| 13 |
sizeof '(' type-id ')'
|
| 14 |
sizeof '...' '(' identifier ')'
|
| 15 |
alignof '(' type-id ')'
|
|
@@ -17,41 +21,43 @@ unary-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*
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
function to which the
|
| 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 |
-
|
| 40 |
|
| 41 |
-
The
|
|
|
|
| 42 |
|
| 43 |
- If the operand is a *qualified-id* naming a non-static or variant
|
| 44 |
-
member `m` of some class `C`
|
| 45 |
-
“pointer to member of class `C` of type
|
| 46 |
-
|
| 47 |
-
- Otherwise,
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
\[*Note 2*: In particular, taking
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
[*Example 1*:
|
| 55 |
|
| 56 |
``` cpp
|
| 57 |
struct A { int i; };
|
|
@@ -84,44 +90,53 @@ the *unqualified-id*’s class. — *end note*]
|
|
| 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
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 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.
|
| 101 |
|
| 102 |
The operand of the unary `-` operator shall have arithmetic or unscoped
|
| 103 |
-
enumeration type and the result is the
|
| 104 |
promotion is performed on integral or enumeration operands. The negative
|
| 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
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 120 |
-
|
| 121 |
|
| 122 |
-
[*Note
|
| 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>
|
|
@@ -135,12 +150,12 @@ 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 `
|
| 141 |
-
`1`. The requirements on the operand of prefix `
|
| 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 |
|
|
@@ -156,30 +171,30 @@ await-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
|
| 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
|
| 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 |
-
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
the
|
| 179 |
-
|
| 180 |
-
|
| 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
|
|
@@ -207,25 +222,31 @@ and the *await-ready* expression, then:
|
|
| 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
|
| 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
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
| 224 |
- If the result of *await-ready* is `true`, or when the coroutine is
|
| 225 |
-
resumed
|
| 226 |
-
the
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
[*Example 1*:
|
| 229 |
|
| 230 |
``` cpp
|
| 231 |
template <typename T>
|
|
@@ -252,11 +273,11 @@ auto operator co_await(std::chrono::duration<Rep, Period> d) {
|
|
| 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 |
|
|
@@ -269,33 +290,40 @@ int a[] = { co_await h() }; // error: await-expression outside of function s
|
|
| 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 |
-
[[
|
| 275 |
-
not be applied to an expression that has function or
|
| 276 |
-
the parenthesized name of such types, or to a
|
| 277 |
-
bit-field. The result of `sizeof` applied to
|
| 278 |
-
types is `1`. The result of `sizeof` applied
|
| 279 |
-
type [[basic.fundamental]] is
|
|
|
|
| 280 |
|
| 281 |
-
[*Note 1*:
|
|
|
|
|
|
|
| 282 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 283 |
-
implementation-defined.[^21]
|
|
|
|
|
|
|
| 284 |
|
| 285 |
[*Note 2*: See [[intro.memory]] for the definition of byte and
|
| 286 |
-
[[
|
| 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.
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
| 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.
|
|
@@ -319,11 +347,11 @@ struct count {
|
|
| 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>`
|
| 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
|
|
@@ -331,21 +359,21 @@ 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>`
|
| 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 [[
|
| 346 |
-
exception [[except.throw]].
|
| 347 |
|
| 348 |
``` bnf
|
| 349 |
noexcept-expression:
|
| 350 |
noexcept '(' expression ')'
|
| 351 |
```
|
|
@@ -361,17 +389,17 @@ is potentially-throwing [[except.spec]].
|
|
| 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
|
| 367 |
-
[[
|
| 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*
|
| 373 |
object created by the *new-expression* has a cv-qualified
|
| 374 |
type. — *end note*]
|
| 375 |
|
| 376 |
``` bnf
|
| 377 |
new-expression:
|
|
@@ -477,27 +505,10 @@ returning `int`).
|
|
| 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 |
-
|
| 496 |
-
[*Note 6*: Both `new int` and `new int[10]` have type `int*` and the
|
| 497 |
-
type of `new int[i][10]` is `int (*)[10]` — *end note*]
|
| 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
|
|
@@ -509,12 +520,12 @@ well-formed (because `n` is the *expression* of a
|
|
| 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
|
|
@@ -529,12 +540,12 @@ implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
|
| 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
|
| 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;
|
|
@@ -543,10 +554,26 @@ If the *expression* is erroneous after converting to `std::size_t`:
|
|
| 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
|
|
@@ -554,28 +581,28 @@ 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
|
| 560 |
-
[[new.delete.single]], [[new.delete.array]]
|
| 561 |
-
alternative definitions of these functions
|
| 562 |
-
and/or class-specific versions [[class.free]].
|
| 563 |
-
deallocation functions that
|
| 564 |
-
include functions that do not perform allocation or
|
| 565 |
-
example, see [[new.delete.placement]]. — *end note*]
|
| 566 |
|
| 567 |
-
If the *new-expression*
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
|
| 574 |
An implementation is allowed to omit a call to a replaceable global
|
| 575 |
-
allocation function
|
| 576 |
-
|
| 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 |
|
|
@@ -706,12 +733,12 @@ 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]]
|
| 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 |
|
|
@@ -742,34 +769,33 @@ 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 [[
|
| 748 |
-
[[class.ctor]] selected for the initialization (if
|
| 749 |
-
*new-expression* creates an array of objects of class type,
|
| 750 |
-
destructor is potentially invoked [[class.dtor]].
|
| 751 |
|
| 752 |
-
If any part of the object initialization described above[^24]
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
the
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
|
|
|
| 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*
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 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
|
|
@@ -820,26 +846,32 @@ delete-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]
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
type
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 836 |
-
pointer to a subobject
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -847,17 +879,17 @@ match the type of the object allocated by `new`, not the syntax of the
|
|
| 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
|
| 853 |
-
deallocation function (see below) is not a destroying operator
|
| 854 |
-
the static type shall be a base class of the dynamic type of the
|
| 855 |
-
to be deleted and the static type shall have a virtual destructor
|
| 856 |
-
behavior is undefined. In an array delete expression, if the
|
| 857 |
-
type of the object to be deleted
|
| 858 |
-
behavior is undefined.
|
| 859 |
|
| 860 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 861 |
exactly once.
|
| 862 |
|
| 863 |
If the object being deleted has incomplete class type at the point of
|
|
@@ -898,61 +930,78 @@ exception. — *end note*]
|
|
| 898 |
|
| 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 |
-
|
| 904 |
-
|
| 905 |
-
|
| 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 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
global scope.
|
| 916 |
|
| 917 |
-
If
|
| 918 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 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 |
-
|
| 940 |
-
destructor, and
|
| 941 |
|
| 942 |
-
[*Note
|
| 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
|
| 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
|
|
@@ -961,15 +1010,14 @@ 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
|
| 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
|
| 971 |
-
[[new.delete.array]]). — *end note*]
|
| 972 |
|
| 973 |
Access and ambiguity control are done for both the deallocation function
|
| 974 |
-
and the destructor
|
| 975 |
|
|
|
|
| 1 |
### Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 2 |
|
| 3 |
+
#### General <a id="expr.unary.general">[[expr.unary.general]]</a>
|
| 4 |
+
|
| 5 |
Expressions with unary operators group right-to-left.
|
| 6 |
|
| 7 |
``` bnf
|
| 8 |
+
%% Ed. note: character protrusion would misalign operators.
|
| 9 |
+
|
| 10 |
unary-expression:
|
| 11 |
postfix-expression
|
| 12 |
unary-operator cast-expression
|
| 13 |
'++' cast-expression
|
| 14 |
+
'--' cast-expression
|
| 15 |
await-expression
|
| 16 |
sizeof unary-expression
|
| 17 |
sizeof '(' type-id ')'
|
| 18 |
sizeof '...' '(' identifier ')'
|
| 19 |
alignof '(' type-id ')'
|
|
|
|
| 21 |
new-expression
|
| 22 |
delete-expression
|
| 23 |
```
|
| 24 |
|
| 25 |
``` bnf
|
| 26 |
+
%% Ed. note: character protrusion would misalign operators.
|
| 27 |
+
|
| 28 |
unary-operator: one of
|
| 29 |
'* & + - ! ~'
|
| 30 |
```
|
| 31 |
|
| 32 |
#### Unary operators <a id="expr.unary.op">[[expr.unary.op]]</a>
|
| 33 |
|
| 34 |
+
The unary `*` operator performs *indirection*. Its operand shall be a
|
| 35 |
+
prvalue of type “pointer to `T`”, where `T` is an object or function
|
| 36 |
+
type. The operator yields an lvalue of type `T` denoting the object or
|
| 37 |
+
function to which the operand points.
|
|
|
|
| 38 |
|
| 39 |
[*Note 1*: Indirection through a pointer to an incomplete type (other
|
| 40 |
than cv `void`) is valid. The lvalue thus obtained can be used in
|
| 41 |
limited ways (to initialize a reference, for example); this lvalue must
|
| 42 |
not be converted to a prvalue, see [[conv.lval]]. — *end note*]
|
| 43 |
|
| 44 |
+
Each of the following unary operators yields a prvalue.
|
| 45 |
|
| 46 |
+
The operand of the unary `&` operator shall be an lvalue of some type
|
| 47 |
+
`T`. The result is a prvalue.
|
| 48 |
|
| 49 |
- If the operand is a *qualified-id* naming a non-static or variant
|
| 50 |
+
member `m` of some class `C`, other than an explicit object member
|
| 51 |
+
function, the result has type “pointer to member of class `C` of type
|
| 52 |
+
`T`” and designates `C::m`.
|
| 53 |
+
- Otherwise, the result has type “pointer to `T`” and points to the
|
| 54 |
+
designated object [[intro.memory]] or function [[basic.compound]]. If
|
| 55 |
+
the operand names an explicit object member function [[dcl.fct]], the
|
| 56 |
+
operand shall be a *qualified-id*. \[*Note 2*: In particular, taking
|
| 57 |
+
the address of a variable of type “cv `T`” yields a pointer of type
|
| 58 |
+
“pointer to cv `T`”. — *end note*]
|
| 59 |
|
| 60 |
[*Example 1*:
|
| 61 |
|
| 62 |
``` cpp
|
| 63 |
struct A { int i; };
|
|
|
|
| 90 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 91 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 92 |
the built-in meaning or the operator function is called. The operand of
|
| 93 |
`&` shall not be a bit-field.
|
| 94 |
|
| 95 |
+
[*Note 5*: The address of an overload set [[over]] can be taken only in
|
| 96 |
+
a context that uniquely determines which function is referred to (see
|
| 97 |
+
[[over.over]]). Since the context can affect whether the operand is a
|
| 98 |
+
static or non-static member function, the context can also affect
|
| 99 |
+
whether the expression has type “pointer to function” or “pointer to
|
| 100 |
+
member function”. — *end note*]
|
|
|
|
| 101 |
|
| 102 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 103 |
enumeration, or pointer type and the result is the value of the
|
| 104 |
argument. Integral promotion is performed on integral or enumeration
|
| 105 |
operands. The type of the result is the type of the promoted operand.
|
| 106 |
|
| 107 |
The operand of the unary `-` operator shall have arithmetic or unscoped
|
| 108 |
+
enumeration type and the result is the negative of its operand. Integral
|
| 109 |
promotion is performed on integral or enumeration operands. The negative
|
| 110 |
of an unsigned quantity is computed by subtracting its value from 2ⁿ,
|
| 111 |
where n is the number of bits in the promoted operand. The type of the
|
| 112 |
result is the type of the promoted operand.
|
| 113 |
|
| 114 |
+
[*Note 6*: The result is the two’s complement of the operand (where
|
| 115 |
+
operand and result are considered as unsigned). — *end note*]
|
| 116 |
+
|
| 117 |
The operand of the logical negation operator `!` is contextually
|
| 118 |
converted to `bool` [[conv]]; its value is `true` if the converted
|
| 119 |
operand is `false` and `false` otherwise. The type of the result is
|
| 120 |
`bool`.
|
| 121 |
|
| 122 |
+
The operand of the `~` operator shall have integral or unscoped
|
| 123 |
+
enumeration type. Integral promotions are performed. The type of the
|
| 124 |
+
result is the type of the promoted operand. Given the coefficients `xᵢ`
|
| 125 |
+
of the base-2 representation [[basic.fundamental]] of the promoted
|
| 126 |
+
operand `x`, the coefficient `rᵢ` of the base-2 representation of the
|
| 127 |
+
result `r` is 1 if `xᵢ` is 0, and 0 otherwise.
|
| 128 |
+
|
| 129 |
+
[*Note 7*: The result is the ones’ complement of the operand (where
|
| 130 |
+
operand and result are considered as unsigned). — *end note*]
|
| 131 |
+
|
| 132 |
There is an ambiguity in the grammar when `~` is followed by a
|
| 133 |
*type-name* or *decltype-specifier*. The ambiguity is resolved by
|
| 134 |
+
treating `~` as the operator rather than as the start of an
|
| 135 |
+
*unqualified-id* naming a destructor.
|
| 136 |
|
| 137 |
+
[*Note 8*: Because the grammar does not permit an operator to follow
|
| 138 |
the `.`, `->`, or `::` tokens, a `~` followed by a *type-name* or
|
| 139 |
*decltype-specifier* in a member access expression or *qualified-id* is
|
| 140 |
unambiguously parsed as a destructor name. — *end note*]
|
| 141 |
|
| 142 |
#### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
|
|
|
| 150 |
bit-field. The expression `++x` is equivalent to `x+=1`.
|
| 151 |
|
| 152 |
[*Note 1*: See the discussions of addition [[expr.add]] and assignment
|
| 153 |
operators [[expr.ass]] for information on conversions. — *end note*]
|
| 154 |
|
| 155 |
+
The operand of prefix `--` is modified [[defns.access]] by subtracting
|
| 156 |
+
`1`. The requirements on the operand of prefix `--` and the properties
|
| 157 |
of its result are otherwise the same as those of prefix `++`.
|
| 158 |
|
| 159 |
[*Note 2*: For postfix increment and decrement, see
|
| 160 |
[[expr.post.incr]]. — *end note*]
|
| 161 |
|
|
|
|
| 171 |
```
|
| 172 |
|
| 173 |
An *await-expression* shall appear only in a potentially-evaluated
|
| 174 |
expression within the *compound-statement* of a *function-body* outside
|
| 175 |
of a *handler* [[except.pre]]. In a *declaration-statement* or in the
|
| 176 |
+
*simple-declaration* (if any) of an *init-statement*, an
|
| 177 |
*await-expression* shall appear only in an *initializer* of that
|
| 178 |
*declaration-statement* or *simple-declaration*. An *await-expression*
|
| 179 |
shall not appear in a default argument [[dcl.fct.default]]. An
|
| 180 |
+
*await-expression* shall not appear in the initializer of a block
|
| 181 |
variable with static or thread storage duration. A context within a
|
| 182 |
function where an *await-expression* can appear is called a *suspension
|
| 183 |
context* of the function.
|
| 184 |
|
| 185 |
Evaluation of an *await-expression* involves the following auxiliary
|
| 186 |
types, expressions, and objects:
|
| 187 |
|
| 188 |
- *p* is an lvalue naming the promise object [[dcl.fct.def.coroutine]]
|
| 189 |
of the enclosing coroutine and `P` is the type of that object.
|
| 190 |
+
- Unless the *await-expression* was implicitly produced by a
|
| 191 |
+
*yield-expression* [[expr.yield]], an initial await expression, or a
|
| 192 |
+
final await expression [[dcl.fct.def.coroutine]], a search is
|
| 193 |
+
performed for the name `await_transform` in the scope of `P`
|
| 194 |
+
[[class.member.lookup]]. If this search is performed and finds at
|
| 195 |
+
least one declaration, then *a* is
|
| 196 |
*p*`.await_transform(`*cast-expression*`)`; otherwise, *a* is the
|
| 197 |
*cast-expression*.
|
| 198 |
- *o* is determined by enumerating the applicable `operator co_await`
|
| 199 |
functions for an argument *a* [[over.match.oper]], and choosing the
|
| 200 |
best one through overload resolution [[over.match]]. If overload
|
|
|
|
| 222 |
- If the result of *await-ready* is `false`, the coroutine is considered
|
| 223 |
suspended. Then:
|
| 224 |
- If the type of *await-suspend* is `std::coroutine_handle<Z>`,
|
| 225 |
*await-suspend*`.resume()` is evaluated. \[*Note 1*: This resumes
|
| 226 |
the coroutine referred to by the result of *await-suspend*. Any
|
| 227 |
+
number of coroutines can be successively resumed in this fashion,
|
| 228 |
eventually returning control flow to the current coroutine caller or
|
| 229 |
resumer [[dcl.fct.def.coroutine]]. — *end note*]
|
| 230 |
- Otherwise, if the type of *await-suspend* is `bool`, *await-suspend*
|
| 231 |
is evaluated, and the coroutine is resumed if the result is `false`.
|
| 232 |
- Otherwise, *await-suspend* is evaluated.
|
| 233 |
|
| 234 |
If the evaluation of *await-suspend* exits via an exception, the
|
| 235 |
exception is caught, the coroutine is resumed, and the exception is
|
| 236 |
+
immediately rethrown [[except.throw]]. Otherwise, control flow returns
|
| 237 |
+
to the current coroutine caller or resumer [[dcl.fct.def.coroutine]]
|
| 238 |
+
without exiting any scopes [[stmt.jump]]. The point in the coroutine
|
| 239 |
+
immediately prior to control returning to its caller or resumer is a
|
| 240 |
+
coroutine *suspend point*.
|
| 241 |
- If the result of *await-ready* is `true`, or when the coroutine is
|
| 242 |
+
resumed other than by rethrowing an exception from *await-suspend*,
|
| 243 |
+
the *await-resume* expression is evaluated, and its result is the
|
| 244 |
+
result of the *await-expression*.
|
| 245 |
+
|
| 246 |
+
[*Note 2*: With respect to sequencing, an *await-expression* is
|
| 247 |
+
indivisible [[intro.execution]]. — *end note*]
|
| 248 |
|
| 249 |
[*Example 1*:
|
| 250 |
|
| 251 |
``` cpp
|
| 252 |
template <typename T>
|
|
|
|
| 273 |
using namespace std::chrono;
|
| 274 |
|
| 275 |
my_future<int> h();
|
| 276 |
|
| 277 |
my_future<void> g() {
|
| 278 |
+
std::cout << "just about to go to sleep...\n";
|
| 279 |
co_await 10ms;
|
| 280 |
std::cout << "resumed\n";
|
| 281 |
co_await h();
|
| 282 |
}
|
| 283 |
|
|
|
|
| 290 |
#### Sizeof <a id="expr.sizeof">[[expr.sizeof]]</a>
|
| 291 |
|
| 292 |
The `sizeof` operator yields the number of bytes occupied by a
|
| 293 |
non-potentially-overlapping object of the type of its operand. The
|
| 294 |
operand is either an expression, which is an unevaluated operand
|
| 295 |
+
[[term.unevaluated.operand]], or a parenthesized *type-id*. The `sizeof`
|
| 296 |
+
operator shall not be applied to an expression that has function or
|
| 297 |
+
incomplete type, to the parenthesized name of such types, or to a
|
| 298 |
+
glvalue that designates a bit-field. The result of `sizeof` applied to
|
| 299 |
+
any of the narrow character types is `1`. The result of `sizeof` applied
|
| 300 |
+
to any other fundamental type [[basic.fundamental]] is
|
| 301 |
+
*implementation-defined*.
|
| 302 |
|
| 303 |
+
[*Note 1*:
|
| 304 |
+
|
| 305 |
+
In particular, the values of `sizeof(bool)`, `sizeof(char16_t)`,
|
| 306 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 307 |
+
implementation-defined.[^21]
|
| 308 |
+
|
| 309 |
+
— *end note*]
|
| 310 |
|
| 311 |
[*Note 2*: See [[intro.memory]] for the definition of byte and
|
| 312 |
+
[[term.object.representation]] for the definition of object
|
| 313 |
representation. — *end note*]
|
| 314 |
|
| 315 |
When applied to a reference type, the result is the size of the
|
| 316 |
referenced type. When applied to a class, the result is the number of
|
| 317 |
bytes in an object of that class including any padding required for
|
| 318 |
placing objects of that type in an array. The result of applying
|
| 319 |
`sizeof` to a potentially-overlapping subobject is the size of the type,
|
| 320 |
+
not the size of the subobject.[^22]
|
| 321 |
+
|
| 322 |
+
When applied to an array, the result is the total number of bytes in the
|
| 323 |
+
array. This implies that the size of an array of n elements is n times
|
| 324 |
+
the size of an element.
|
| 325 |
|
| 326 |
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 327 |
function-to-pointer [[conv.func]] standard conversions are not applied
|
| 328 |
to the operand of `sizeof`. If the operand is a prvalue, the temporary
|
| 329 |
materialization conversion [[conv.rval]] is applied.
|
|
|
|
| 347 |
The result of `sizeof` and `sizeof...` is a prvalue of type
|
| 348 |
`std::size_t`.
|
| 349 |
|
| 350 |
[*Note 3*: A `sizeof` expression is an integral constant expression
|
| 351 |
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 352 |
+
`<cstddef>` [[cstddef.syn]], [[support.types.layout]]. — *end note*]
|
| 353 |
|
| 354 |
#### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
| 355 |
|
| 356 |
An `alignof` expression yields the alignment requirement of its operand
|
| 357 |
type. The operand shall be a *type-id* representing a complete object
|
|
|
|
| 359 |
|
| 360 |
The result is a prvalue of type `std::size_t`.
|
| 361 |
|
| 362 |
[*Note 1*: An `alignof` expression is an integral constant expression
|
| 363 |
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 364 |
+
`<cstddef>` [[cstddef.syn]], [[support.types.layout]]. — *end note*]
|
| 365 |
|
| 366 |
When `alignof` is applied to a reference type, the result is the
|
| 367 |
alignment of the referenced type. When `alignof` is applied to an array
|
| 368 |
type, the result is the alignment of the element type.
|
| 369 |
|
| 370 |
#### `noexcept` operator <a id="expr.unary.noexcept">[[expr.unary.noexcept]]</a>
|
| 371 |
|
| 372 |
The `noexcept` operator determines whether the evaluation of its
|
| 373 |
+
operand, which is an unevaluated operand [[term.unevaluated.operand]],
|
| 374 |
+
can throw an exception [[except.throw]].
|
| 375 |
|
| 376 |
``` bnf
|
| 377 |
noexcept-expression:
|
| 378 |
noexcept '(' expression ')'
|
| 379 |
```
|
|
|
|
| 389 |
#### New <a id="expr.new">[[expr.new]]</a>
|
| 390 |
|
| 391 |
The *new-expression* attempts to create an object of the *type-id*
|
| 392 |
[[dcl.name]] or *new-type-id* to which it is applied. The type of that
|
| 393 |
object is the *allocated type*. This type shall be a complete object
|
| 394 |
+
type [[term.incomplete.type]], but not an abstract class type
|
| 395 |
+
[[class.abstract]] or array thereof [[intro.object]].
|
| 396 |
|
| 397 |
[*Note 1*: Because references are not objects, references cannot be
|
| 398 |
created by *new-expression*s. — *end note*]
|
| 399 |
|
| 400 |
+
[*Note 2*: The *type-id* can be a cv-qualified type, in which case the
|
| 401 |
object created by the *new-expression* has a cv-qualified
|
| 402 |
type. — *end note*]
|
| 403 |
|
| 404 |
``` bnf
|
| 405 |
new-expression:
|
|
|
|
| 505 |
|
| 506 |
— *end example*]
|
| 507 |
|
| 508 |
— *end note*]
|
| 509 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 511 |
the associated array type.
|
| 512 |
|
| 513 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 514 |
converted constant expression [[expr.const]] of type `std::size_t` and
|
|
|
|
| 520 |
`n` is not a constant expression). — *end example*]
|
| 521 |
|
| 522 |
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 523 |
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 524 |
object is an array with `n` elements, where `n` is determined from the
|
| 525 |
+
number of initial elements supplied in the *new-initializer*
|
| 526 |
+
[[dcl.init.aggr]], [[dcl.init.string]].
|
| 527 |
|
| 528 |
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 529 |
implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
| 530 |
|
| 531 |
- the expression is of non-class type and its value before converting to
|
|
|
|
| 540 |
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 541 |
number of elements to initialize.
|
| 542 |
|
| 543 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 544 |
|
| 545 |
+
- if the *expression* is a potentially-evaluated core constant
|
| 546 |
+
expression, the program is ill-formed;
|
| 547 |
- otherwise, an allocation function is not called; instead
|
| 548 |
- if the allocation function that would have been called has a
|
| 549 |
non-throwing exception specification [[except.spec]], the value of
|
| 550 |
the *new-expression* is the null pointer value of the required
|
| 551 |
result type;
|
|
|
|
| 554 |
`std::bad_array_new_length` [[new.badlength]].
|
| 555 |
|
| 556 |
When the value of the *expression* is zero, the allocation function is
|
| 557 |
called to allocate an array with no elements.
|
| 558 |
|
| 559 |
+
Objects created by a *new-expression* have dynamic storage duration
|
| 560 |
+
[[basic.stc.dynamic]].
|
| 561 |
+
|
| 562 |
+
[*Note 5*: The lifetime of such an object is not necessarily
|
| 563 |
+
restricted to the scope in which it is created. — *end note*]
|
| 564 |
+
|
| 565 |
+
When the allocated type is “array of `N` `T`” (that is, the
|
| 566 |
+
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 567 |
+
denotes an array type), the *new-expression* yields a prvalue of type
|
| 568 |
+
“pointer to `T`” that points to the initial element (if any) of the
|
| 569 |
+
array. Otherwise, let `T` be the allocated type; the *new-expression* is
|
| 570 |
+
a prvalue of type “pointer to T” that points to the object created.
|
| 571 |
+
|
| 572 |
+
[*Note 6*: Both `new int` and `new int[10]` have type `int*` and the
|
| 573 |
+
type of `new int[i][10]` is `int (*)[10]`. — *end note*]
|
| 574 |
+
|
| 575 |
A *new-expression* may obtain storage for the object by calling an
|
| 576 |
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 577 |
*new-expression* terminates by throwing an exception, it may release
|
| 578 |
storage by calling a deallocation function
|
| 579 |
[[basic.stc.dynamic.deallocation]]. If the allocated type is a non-array
|
|
|
|
| 581 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 582 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 583 |
the deallocation function’s name is `operator delete[]`.
|
| 584 |
|
| 585 |
[*Note 7*: An implementation is required to provide default definitions
|
| 586 |
+
for the global allocation functions
|
| 587 |
+
[[basic.stc.dynamic]], [[new.delete.single]], [[new.delete.array]]. A
|
| 588 |
+
C++ program can provide alternative definitions of these functions
|
| 589 |
+
[[replacement.functions]] and/or class-specific versions [[class.free]].
|
| 590 |
+
The set of allocation and deallocation functions that can be called by a
|
| 591 |
+
*new-expression* can include functions that do not perform allocation or
|
| 592 |
+
deallocation; for example, see [[new.delete.placement]]. — *end note*]
|
| 593 |
|
| 594 |
+
If the *new-expression* does not begin with a unary `::` operator and
|
| 595 |
+
the allocated type is a class type `T` or array thereof, a search is
|
| 596 |
+
performed for the allocation function’s name in the scope of `T`
|
| 597 |
+
[[class.member.lookup]]. Otherwise, or if nothing is found, the
|
| 598 |
+
allocation function’s name is looked up by searching for it in the
|
| 599 |
+
global scope.
|
| 600 |
|
| 601 |
An implementation is allowed to omit a call to a replaceable global
|
| 602 |
+
allocation function [[new.delete.single]], [[new.delete.array]]. When it
|
| 603 |
+
does so, the storage is instead provided by the implementation or
|
| 604 |
provided by extending the allocation of another *new-expression*.
|
| 605 |
|
| 606 |
During an evaluation of a constant expression, a call to an allocation
|
| 607 |
function is always omitted.
|
| 608 |
|
|
|
|
| 733 |
|
| 734 |
— *end example*]
|
| 735 |
|
| 736 |
[*Note 10*: Unless an allocation function has a non-throwing exception
|
| 737 |
specification [[except.spec]], it indicates failure to allocate storage
|
| 738 |
+
by throwing a `std::bad_alloc` exception
|
| 739 |
+
[[basic.stc.dynamic.allocation]], [[except]], [[bad.alloc]]; it returns
|
| 740 |
a non-null pointer otherwise. If the allocation function has a
|
| 741 |
non-throwing exception specification, it returns null to indicate
|
| 742 |
failure to allocate storage and a non-null pointer
|
| 743 |
otherwise. — *end note*]
|
| 744 |
|
|
|
|
| 769 |
the allocated object is sequenced before the value computation of the
|
| 770 |
*new-expression*.
|
| 771 |
|
| 772 |
If the *new-expression* creates an object or an array of objects of
|
| 773 |
class type, access and ambiguity control are done for the allocation
|
| 774 |
+
function, the deallocation function [[basic.stc.dynamic.deallocation]],
|
| 775 |
+
and the constructor [[class.ctor]] selected for the initialization (if
|
| 776 |
+
any). If the *new-expression* creates an array of objects of class type,
|
| 777 |
+
the destructor is potentially invoked [[class.dtor]].
|
| 778 |
|
| 779 |
+
If any part of the object initialization described above[^24]
|
| 780 |
+
|
| 781 |
+
terminates by throwing an exception and a suitable deallocation function
|
| 782 |
+
can be found, the deallocation function is called to free the memory in
|
| 783 |
+
which the object was being constructed, after which the exception
|
| 784 |
+
continues to propagate in the context of the *new-expression*. If no
|
| 785 |
+
unambiguous matching deallocation function can be found, propagating the
|
| 786 |
+
exception does not cause the object’s memory to be freed.
|
| 787 |
|
| 788 |
[*Note 13*: This is appropriate when the called allocation function
|
| 789 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 790 |
leak. — *end note*]
|
| 791 |
|
| 792 |
+
If the *new-expression* does not begin with a unary `::` operator and
|
| 793 |
+
the allocated type is a class type `T` or an array thereof, a search is
|
| 794 |
+
performed for the deallocation function’s name in the scope of `T`.
|
| 795 |
+
Otherwise, or if nothing is found, the deallocation function’s name is
|
| 796 |
+
looked up by searching for it in the global scope.
|
|
|
|
|
|
|
| 797 |
|
| 798 |
A declaration of a placement deallocation function matches the
|
| 799 |
declaration of a placement allocation function if it has the same number
|
| 800 |
of parameters and, after parameter transformations [[dcl.fct]], all
|
| 801 |
parameter types except the first are identical. If the lookup finds a
|
|
|
|
| 846 |
```
|
| 847 |
|
| 848 |
The first alternative is a *single-object delete expression*, and the
|
| 849 |
second is an *array delete expression*. Whenever the `delete` keyword is
|
| 850 |
immediately followed by empty square brackets, it shall be interpreted
|
| 851 |
+
as the second alternative.[^25]
|
| 852 |
+
|
| 853 |
+
The operand shall be of pointer to object type or of class type. If of
|
| 854 |
+
class type, the operand is contextually implicitly converted [[conv]] to
|
| 855 |
+
a pointer to object type.[^26]
|
| 856 |
+
|
| 857 |
+
The *delete-expression* has type `void`.
|
| 858 |
|
| 859 |
If the operand has a class type, the operand is converted to a pointer
|
| 860 |
type by calling the above-mentioned conversion function, and the
|
| 861 |
converted operand is used in place of the original operand for the
|
| 862 |
remainder of this subclause. In a single-object delete expression, the
|
| 863 |
value of the operand of `delete` may be a null pointer value, a pointer
|
| 864 |
+
value that resulted from a previous non-array *new-expression*, or a
|
| 865 |
+
pointer to a base class subobject of an object created by such a
|
| 866 |
+
*new-expression*. If not, the behavior is undefined. In an array delete
|
| 867 |
+
expression, the value of the operand of `delete` may be a null pointer
|
| 868 |
+
value or a pointer value that resulted from a previous array
|
| 869 |
+
*new-expression* whose allocation function was not a non-allocating form
|
| 870 |
+
[[new.delete.placement]].[^27]
|
| 871 |
+
|
| 872 |
+
If not, the behavior is undefined.
|
| 873 |
|
| 874 |
[*Note 1*: This means that the syntax of the *delete-expression* must
|
| 875 |
match the type of the object allocated by `new`, not the syntax of the
|
| 876 |
*new-expression*. — *end note*]
|
| 877 |
|
|
|
|
| 879 |
*delete-expression*; it is not necessary to cast away the constness
|
| 880 |
[[expr.const.cast]] of the pointer expression before it is used as the
|
| 881 |
operand of the *delete-expression*. — *end note*]
|
| 882 |
|
| 883 |
In a single-object delete expression, if the static type of the object
|
| 884 |
+
to be deleted is not similar [[conv.qual]] to its dynamic type and the
|
| 885 |
+
selected deallocation function (see below) is not a destroying operator
|
| 886 |
+
delete, the static type shall be a base class of the dynamic type of the
|
| 887 |
+
object to be deleted and the static type shall have a virtual destructor
|
| 888 |
+
or the behavior is undefined. In an array delete expression, if the
|
| 889 |
+
dynamic type of the object to be deleted is not similar to its static
|
| 890 |
+
type, the behavior is undefined.
|
| 891 |
|
| 892 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 893 |
exactly once.
|
| 894 |
|
| 895 |
If the object being deleted has incomplete class type at the point of
|
|
|
|
| 930 |
|
| 931 |
If the value of the operand of the *delete-expression* is a null pointer
|
| 932 |
value, it is unspecified whether a deallocation function will be called
|
| 933 |
as described above.
|
| 934 |
|
| 935 |
+
If a deallocation function is called, it is `operator delete` for a
|
| 936 |
+
single-object delete expression or `operator delete[]` for an array
|
| 937 |
+
delete expression.
|
|
|
|
|
|
|
|
|
|
| 938 |
|
| 939 |
+
[*Note 4*: An implementation provides default definitions of the
|
| 940 |
+
global deallocation functions
|
| 941 |
+
[[new.delete.single]], [[new.delete.array]]. A C++ program can provide
|
| 942 |
+
alternative definitions of these functions [[replacement.functions]],
|
| 943 |
+
and/or class-specific versions [[class.free]]. — *end note*]
|
|
|
|
| 944 |
|
| 945 |
+
If the keyword `delete` in a *delete-expression* is not preceded by the
|
| 946 |
+
unary `::` operator and the type of the operand is a pointer to a
|
| 947 |
+
(possibly cv-qualified) class type `T` or (possibly multidimensional)
|
| 948 |
+
array thereof:
|
| 949 |
+
|
| 950 |
+
- For a single-object delete expression, if the operand is a pointer to
|
| 951 |
+
cv `T` and `T` has a virtual destructor, the deallocation function is
|
| 952 |
+
the one selected at the point of definition of the dynamic type’s
|
| 953 |
+
virtual destructor [[class.dtor]].
|
| 954 |
+
- Otherwise, a search is performed for the deallocation function’s name
|
| 955 |
+
in the scope of `T`.
|
| 956 |
+
|
| 957 |
+
Otherwise, or if nothing is found, the deallocation function’s name is
|
| 958 |
+
looked up by searching for it in the global scope. In any case, any
|
| 959 |
+
declarations other than of usual deallocation functions
|
| 960 |
+
[[basic.stc.dynamic.deallocation]] are discarded.
|
| 961 |
+
|
| 962 |
+
[*Note 5*: If only a placement deallocation function is found in a
|
| 963 |
+
class, the program is ill-formed because the lookup set is empty
|
| 964 |
+
[[basic.lookup]]. — *end note*]
|
| 965 |
+
|
| 966 |
+
If more than one deallocation function is found, the function to be
|
| 967 |
+
called is selected as follows:
|
| 968 |
|
| 969 |
- If any of the deallocation functions is a destroying operator delete,
|
| 970 |
all deallocation functions that are not destroying operator deletes
|
| 971 |
are eliminated from further consideration.
|
| 972 |
- If the type has new-extended alignment, a function with a parameter of
|
| 973 |
type `std::align_val_t` is preferred; otherwise a function without
|
| 974 |
such a parameter is preferred. If any preferred functions are found,
|
| 975 |
all non-preferred functions are eliminated from further consideration.
|
| 976 |
- If exactly one function remains, that function is selected and the
|
| 977 |
selection process terminates.
|
| 978 |
+
- If the deallocation functions belong to a class scope, the one without
|
| 979 |
+
a parameter of type `std::size_t` is selected.
|
| 980 |
- If the type is complete and if, for an array delete expression only,
|
| 981 |
the operand is a pointer to a class type with a non-trivial destructor
|
| 982 |
+
or a (possibly multidimensional) array thereof, the function with a
|
| 983 |
parameter of type `std::size_t` is selected.
|
| 984 |
- Otherwise, it is unspecified whether a deallocation function with a
|
| 985 |
parameter of type `std::size_t` is selected.
|
| 986 |
|
| 987 |
For a single-object delete expression, the deleted object is the object
|
| 988 |
+
A pointed to by the operand if the static type of A does not have a
|
| 989 |
+
virtual destructor, and the most-derived object of A otherwise.
|
| 990 |
|
| 991 |
+
[*Note 6*: If the deallocation function is not a destroying operator
|
| 992 |
delete and the deleted object is not the most derived object in the
|
| 993 |
former case, the behavior is undefined, as stated above. — *end note*]
|
| 994 |
|
| 995 |
For an array delete expression, the deleted object is the array object.
|
| 996 |
When a *delete-expression* is executed, the selected deallocation
|
| 997 |
function shall be called with the address of the deleted object in a
|
| 998 |
single-object delete expression, or the address of the deleted object
|
| 999 |
suitably adjusted for the array allocation overhead [[expr.new]] in an
|
| 1000 |
array delete expression, as its first argument.
|
| 1001 |
|
| 1002 |
+
[*Note 7*: Any cv-qualifiers in the type of the deleted object are
|
| 1003 |
ignored when forming this argument. — *end note*]
|
| 1004 |
|
| 1005 |
If a destroying operator delete is used, an unspecified value is passed
|
| 1006 |
as the argument corresponding to the parameter of type
|
| 1007 |
`std::destroying_delete_t`. If a deallocation function with a parameter
|
|
|
|
| 1010 |
deallocation function with a parameter of type `std::size_t` is used,
|
| 1011 |
the size of the deleted object in a single-object delete expression, or
|
| 1012 |
of the array plus allocation overhead in an array delete expression, is
|
| 1013 |
passed as the corresponding argument.
|
| 1014 |
|
| 1015 |
+
[*Note 8*: If this results in a call to a replaceable deallocation
|
| 1016 |
function, and either the first argument was not the result of a prior
|
| 1017 |
call to a replaceable allocation function or the second or third
|
| 1018 |
argument was not the corresponding argument in said call, the behavior
|
| 1019 |
+
is undefined [[new.delete.single]], [[new.delete.array]]. — *end note*]
|
|
|
|
| 1020 |
|
| 1021 |
Access and ambiguity control are done for both the deallocation function
|
| 1022 |
+
and the destructor [[class.dtor]], [[class.free]].
|
| 1023 |
|