- tmp/tmpxj9_dich/{from.md → to.md} +1407 -884
tmp/tmpxj9_dich/{from.md → to.md}
RENAMED
|
@@ -1,13 +1,19 @@
|
|
| 1 |
# Expressions <a id="expr">[[expr]]</a>
|
| 2 |
|
| 3 |
## Preamble <a id="expr.pre">[[expr.pre]]</a>
|
| 4 |
|
| 5 |
-
[*Note 1*:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
[*Note 2*: Operators can be overloaded, that is, given meaning when
|
| 11 |
applied to expressions of class type [[class]] or enumeration type
|
| 12 |
[[dcl.enum]]. Uses of overloaded operators are transformed into function
|
| 13 |
calls as described in [[over.oper]]. Overloaded operators obey the
|
|
@@ -37,13 +43,15 @@ its type, the behavior is undefined.
|
|
| 37 |
zero divisor, and all floating-point exceptions varies among machines,
|
| 38 |
and is sometimes adjustable by a library function. — *end note*]
|
| 39 |
|
| 40 |
[*Note 4*:
|
| 41 |
|
| 42 |
-
The implementation
|
| 43 |
mathematical rules only where the operators really are associative or
|
| 44 |
-
commutative.[^2]
|
|
|
|
|
|
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
int a, b;
|
| 48 |
...
|
| 49 |
a = a + 32760 + b + 5;
|
|
@@ -103,18 +111,16 @@ Expressions are categorized according to the taxonomy in Figure
|
|
| 103 |
<a id="fig:basic.lval"></a>
|
| 104 |
|
| 105 |
![Expression category taxonomy \[fig:basic.lval\]](images/valuecategories.svg)
|
| 106 |
|
| 107 |
- A *glvalue* is an expression whose evaluation determines the identity
|
| 108 |
-
of an object
|
| 109 |
- A *prvalue* is an expression whose evaluation initializes an object or
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
resources can be reused (usually because it is near the end of its
|
| 115 |
-
lifetime).
|
| 116 |
- An *lvalue* is a glvalue that is not an xvalue.
|
| 117 |
- An *rvalue* is a prvalue or an xvalue.
|
| 118 |
|
| 119 |
Every expression belongs to exactly one of the fundamental
|
| 120 |
classifications in this taxonomy: lvalue, xvalue, or prvalue. This
|
|
@@ -138,15 +144,15 @@ values. — *end note*]
|
|
| 138 |
|
| 139 |
[*Note 3*:
|
| 140 |
|
| 141 |
An expression is an xvalue if it is:
|
| 142 |
|
|
|
|
| 143 |
- the result of calling a function, whether implicitly or explicitly,
|
| 144 |
whose return type is an rvalue reference to object type [[expr.call]],
|
| 145 |
-
- a cast to an rvalue reference to object type
|
| 146 |
-
[[expr.dynamic.cast]], [[expr.static.cast]] [[expr.reinterpret.cast]],
|
| 147 |
-
[[expr.const.cast]], [[expr.cast]]),
|
| 148 |
- a subscripting operation with an xvalue array operand [[expr.sub]],
|
| 149 |
- a class member access expression designating a non-static data member
|
| 150 |
of non-reference type in which the object expression is an xvalue
|
| 151 |
[[expr.ref]], or
|
| 152 |
- a `.*` pointer-to-member expression in which the first operand is an
|
|
@@ -216,26 +222,26 @@ glvalue for that operand, the temporary materialization conversion
|
|
| 216 |
|
| 217 |
The discussion of reference initialization in [[dcl.init.ref]] and of
|
| 218 |
temporaries in [[class.temporary]] indicates the behavior of lvalues
|
| 219 |
and rvalues in other significant contexts.
|
| 220 |
|
| 221 |
-
Unless otherwise indicated [[dcl.type.
|
| 222 |
have complete type or the `void` type; if it has a class type or
|
| 223 |
-
(possibly
|
| 224 |
-
|
| 225 |
cv `void`.
|
| 226 |
|
| 227 |
-
[*Note 8*: A glvalue
|
| 228 |
Class and array prvalues can have cv-qualified types; other prvalues
|
| 229 |
always have cv-unqualified types. See [[expr.type]]. — *end note*]
|
| 230 |
|
| 231 |
An lvalue is *modifiable* unless its type is const-qualified or is a
|
| 232 |
function type.
|
| 233 |
|
| 234 |
[*Note 9*: A program that attempts to modify an object through a
|
| 235 |
-
nonmodifiable lvalue or through an rvalue is ill-formed
|
| 236 |
-
[[expr.post.incr]], [[expr.pre.incr]]
|
| 237 |
|
| 238 |
If a program attempts to access [[defns.access]] the stored value of an
|
| 239 |
object through a glvalue whose type is not similar [[conv.qual]] to one
|
| 240 |
of the following types the behavior is undefined:[^4]
|
| 241 |
|
|
@@ -247,17 +253,18 @@ of the following types the behavior is undefined:[^4]
|
|
| 247 |
If a program invokes a defaulted copy/move constructor or copy/move
|
| 248 |
assignment operator for a union of type `U` with a glvalue argument that
|
| 249 |
does not denote an object of type cv `U` within its lifetime, the
|
| 250 |
behavior is undefined.
|
| 251 |
|
| 252 |
-
[*Note 10*:
|
| 253 |
-
|
|
|
|
| 254 |
|
| 255 |
### Type <a id="expr.type">[[expr.type]]</a>
|
| 256 |
|
| 257 |
-
If an expression initially has the type “reference to `T`”
|
| 258 |
-
[[dcl.ref]], [[dcl.init.ref]]
|
| 259 |
further analysis. The expression designates the object or function
|
| 260 |
denoted by the reference, and the expression is an lvalue or an xvalue,
|
| 261 |
depending on the expression.
|
| 262 |
|
| 263 |
[*Note 1*: Before the lifetime of the reference has started or after it
|
|
@@ -282,26 +289,27 @@ pointer-to-member type or `std::nullptr_t`, is:
|
|
| 282 |
- if `T1` or `T2` is “pointer to `noexcept` function” and the other type
|
| 283 |
is “pointer to function”, where the function types are otherwise the
|
| 284 |
same, “pointer to function”;
|
| 285 |
- if `T1` is “pointer to *cv1* `C1`” and `T2` is “pointer to *cv2*
|
| 286 |
`C2`”, where `C1` is reference-related to `C2` or `C2` is
|
| 287 |
-
reference-related to `C1` [[dcl.init.ref]], the
|
| 288 |
-
[[conv.qual]] of `T1` and `T2` or the
|
| 289 |
-
`T1`, respectively;
|
| 290 |
- if `T1` or `T2` is “pointer to member of `C1` of type function”, the
|
| 291 |
other type is “pointer to member of `C2` of type `noexcept` function”,
|
| 292 |
and `C1` is reference-related to `C2` or `C2` is reference-related to
|
| 293 |
`C1` [[dcl.init.ref]], where the function types are otherwise the
|
| 294 |
same, “pointer to member of `C2` of type function” or “pointer to
|
| 295 |
member of `C1` of type function”, respectively;
|
| 296 |
- if `T1` is “pointer to member of `C1` of type *cv1* `U`” and `T2` is
|
| 297 |
“pointer to member of `C2` of type *cv2* `U`”, for some non-function
|
| 298 |
type `U`, where `C1` is reference-related to `C2` or `C2` is
|
| 299 |
-
reference-related to `C1` [[dcl.init.ref]], the
|
| 300 |
-
`T2` and `T1` or the
|
| 301 |
-
|
| 302 |
-
|
|
|
|
| 303 |
- otherwise, a program that necessitates the determination of a
|
| 304 |
composite pointer type is ill-formed.
|
| 305 |
|
| 306 |
[*Example 1*:
|
| 307 |
|
|
@@ -318,16 +326,15 @@ pointer to `const int`”.
|
|
| 318 |
|
| 319 |
— *end example*]
|
| 320 |
|
| 321 |
### Context dependence <a id="expr.context">[[expr.context]]</a>
|
| 322 |
|
| 323 |
-
In some contexts, *unevaluated operands* appear
|
| 324 |
-
[[expr.typeid]], [[expr.sizeof]], [[expr.unary.noexcept]],
|
| 325 |
-
|
| 326 |
-
operand is not evaluated.
|
| 327 |
|
| 328 |
-
[*Note 1*: In an unevaluated operand, a non-static class member
|
| 329 |
named [[expr.prim.id]] and naming of objects or functions does not, by
|
| 330 |
itself, require that a definition be provided [[basic.def.odr]]. An
|
| 331 |
unevaluated operand is considered a full-expression
|
| 332 |
[[intro.execution]]. — *end note*]
|
| 333 |
|
|
@@ -350,21 +357,24 @@ volatile-qualified type and it is one of the following:
|
|
| 350 |
these expressions.
|
| 351 |
|
| 352 |
[*Note 2*: Using an overloaded operator causes a function call; the
|
| 353 |
above covers only operators with built-in meaning. — *end note*]
|
| 354 |
|
| 355 |
-
|
| 356 |
-
|
| 357 |
|
| 358 |
-
[*Note 3*: If the expression is an lvalue of class type, it
|
| 359 |
-
volatile copy constructor to initialize the temporary object
|
| 360 |
-
result object of the
|
|
|
|
| 361 |
|
| 362 |
-
The
|
| 363 |
|
| 364 |
## Standard conversions <a id="conv">[[conv]]</a>
|
| 365 |
|
|
|
|
|
|
|
| 366 |
Standard conversions are implicit conversions with built-in meaning.
|
| 367 |
[[conv]] enumerates the full set of such conversions. A *standard
|
| 368 |
conversion sequence* is a sequence of standard conversions in the
|
| 369 |
following order:
|
| 370 |
|
|
@@ -443,14 +453,16 @@ the operand of the unary `&` operator. Specific exceptions are given in
|
|
| 443 |
the descriptions of those operators and contexts. — *end note*]
|
| 444 |
|
| 445 |
### Lvalue-to-rvalue conversion <a id="conv.lval">[[conv.lval]]</a>
|
| 446 |
|
| 447 |
A glvalue [[basic.lval]] of a non-function, non-array type `T` can be
|
| 448 |
-
converted to a prvalue.[^5]
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
|
|
|
|
|
|
| 452 |
|
| 453 |
When an lvalue-to-rvalue conversion is applied to an expression E, and
|
| 454 |
either
|
| 455 |
|
| 456 |
- E is not potentially evaluated, or
|
|
@@ -485,13 +497,12 @@ rules:
|
|
| 485 |
`T` is volatile-qualified [[intro.execution]], and the glvalue can
|
| 486 |
refer to an inactive member of a union [[class.union]]. — *end note*]
|
| 487 |
- Otherwise, if `T` has a class type, the conversion copy-initializes
|
| 488 |
the result object from the glvalue.
|
| 489 |
- Otherwise, if the object to which the glvalue refers contains an
|
| 490 |
-
invalid pointer value
|
| 491 |
-
|
| 492 |
-
*implementation-defined*.
|
| 493 |
- Otherwise, the object indicated by the glvalue is read
|
| 494 |
[[defns.access]], and the value contained in the object is the prvalue
|
| 495 |
result.
|
| 496 |
|
| 497 |
[*Note 2*: See also [[basic.lval]]. — *end note*]
|
|
@@ -529,46 +540,46 @@ int k = X().n; // OK, X() prvalue is converted to xvalue
|
|
| 529 |
|
| 530 |
— *end example*]
|
| 531 |
|
| 532 |
### Qualification conversions <a id="conv.qual">[[conv.qual]]</a>
|
| 533 |
|
| 534 |
-
A *
|
| 535 |
-
`T` is
|
| 536 |
|
| 537 |
where each cvᵢ is a set of cv-qualifiers [[basic.type.qualifier]], and
|
| 538 |
each Pᵢ is “pointer to” [[dcl.ptr]], “pointer to member of class Cᵢ of
|
| 539 |
type” [[dcl.mptr]], “array of Nᵢ”, or “array of unknown bound of”
|
| 540 |
[[dcl.array]]. If Pᵢ designates an array, the cv-qualifiers cvᵢ₊₁ on the
|
| 541 |
element type are also taken as the cv-qualifiers cvᵢ of the array.
|
| 542 |
|
| 543 |
[*Example 1*: The type denoted by the *type-id* `const int **` has
|
| 544 |
-
three
|
| 545 |
-
`const int`”, and as “pointer to pointer to
|
| 546 |
`const int`”. — *end example*]
|
| 547 |
|
| 548 |
The n-tuple of cv-qualifiers after the first one in the longest
|
| 549 |
-
|
| 550 |
-
*cv-qualification signature* of `T`.
|
| 551 |
|
| 552 |
-
Two types `T1` and `T2` are *similar* if they have
|
| 553 |
-
with the same n such that corresponding Pᵢ
|
| 554 |
-
same or one is “array of Nᵢ” and the other is
|
| 555 |
-
of”, and the types denoted by `U` are the same.
|
| 556 |
|
| 557 |
-
The *
|
| 558 |
-
similar to `T1` whose
|
| 559 |
|
| 560 |
-
- for every i > 0, cv³ᵢ is the union of cv¹ᵢ and cv²ᵢ
|
| 561 |
- if either P¹ᵢ or P²ᵢ is “array of unknown bound of”, P³ᵢ is “array of
|
| 562 |
-
unknown bound of”, otherwise it is P¹ᵢ
|
| 563 |
- if the resulting cv³ᵢ is different from cv¹ᵢ or cv²ᵢ, or the resulting
|
| 564 |
P³ᵢ is different from P¹ᵢ or P²ᵢ, then `const` is added to every cv³ₖ
|
| 565 |
-
for 0 < k < i
|
| 566 |
|
| 567 |
-
where cvʲᵢ and Pʲᵢ are the components of the
|
| 568 |
-
prvalue of type `T1` can be converted to type `T2` if the
|
| 569 |
-
type of `T1` and `T2` is `T2`.
|
| 570 |
|
| 571 |
[*Note 1*:
|
| 572 |
|
| 573 |
If a program could assign a pointer of type `T**` to a pointer of type
|
| 574 |
`const` `T**` (that is, if line \#1 below were allowed), a program could
|
|
@@ -586,12 +597,12 @@ int main() {
|
|
| 586 |
```
|
| 587 |
|
| 588 |
— *end note*]
|
| 589 |
|
| 590 |
[*Note 2*: Given similar types `T1` and `T2`, this construction ensures
|
| 591 |
-
that both can be converted to the
|
| 592 |
-
`T2`. — *end note*]
|
| 593 |
|
| 594 |
[*Note 3*: A prvalue of type “pointer to *cv1* `T`” can be converted to
|
| 595 |
a prvalue of type “pointer to *cv2* `T`” if “*cv2* `T`” is more
|
| 596 |
cv-qualified than “*cv1* `T`”. A prvalue of type “pointer to member of
|
| 597 |
`X` of type *cv1* `T`” can be converted to a prvalue of type “pointer to
|
|
@@ -602,24 +613,24 @@ than “*cv1* `T`”. — *end note*]
|
|
| 602 |
pointer-to-member-function types) are never cv-qualified
|
| 603 |
[[dcl.fct]]. — *end note*]
|
| 604 |
|
| 605 |
### Integral promotions <a id="conv.prom">[[conv.prom]]</a>
|
| 606 |
|
| 607 |
-
A prvalue of an integer type other than `bool`, `
|
| 608 |
-
or `wchar_t` whose integer conversion rank [[conv.rank]] is
|
| 609 |
-
the rank of `int` can be converted to a prvalue of type `int`
|
| 610 |
-
can represent all the values of the source type; otherwise, the
|
| 611 |
-
prvalue can be converted to a prvalue of type `unsigned int`.
|
| 612 |
|
| 613 |
-
A prvalue of type `char16_t`, `char32_t`, or `wchar_t`
|
| 614 |
[[basic.fundamental]] can be converted to a prvalue of the first of the
|
| 615 |
following types that can represent all the values of its underlying
|
| 616 |
type: `int`, `unsigned int`, `long int`, `unsigned long int`,
|
| 617 |
`long long int`, or `unsigned long long int`. If none of the types in
|
| 618 |
that list can represent all the values of its underlying type, a prvalue
|
| 619 |
-
of type `char16_t`, `char32_t`, or `wchar_t` can be converted
|
| 620 |
-
prvalue of its underlying type.
|
| 621 |
|
| 622 |
A prvalue of an unscoped enumeration type whose underlying type is not
|
| 623 |
fixed can be converted to a prvalue of the first of the following types
|
| 624 |
that can represent all the values of the enumeration [[dcl.enum]]:
|
| 625 |
`int`, `unsigned int`, `long int`, `unsigned long int`, `long long int`,
|
|
@@ -640,12 +651,12 @@ can also be converted to a prvalue of the promoted underlying type.
|
|
| 640 |
A prvalue for an integral bit-field [[class.bit]] can be converted to a
|
| 641 |
prvalue of type `int` if `int` can represent all the values of the
|
| 642 |
bit-field; otherwise, it can be converted to `unsigned int` if
|
| 643 |
`unsigned int` can represent all the values of the bit-field. If the
|
| 644 |
bit-field is larger yet, no integral promotion applies to it. If the
|
| 645 |
-
bit-field has
|
| 646 |
-
|
| 647 |
|
| 648 |
A prvalue of type `bool` can be converted to a prvalue of type `int`,
|
| 649 |
with `false` becoming zero and `true` becoming one.
|
| 650 |
|
| 651 |
These conversions are called *integral promotions*.
|
|
@@ -675,16 +686,19 @@ The conversions allowed as integral promotions are excluded from the set
|
|
| 675 |
of integral conversions.
|
| 676 |
|
| 677 |
### Floating-point conversions <a id="conv.double">[[conv.double]]</a>
|
| 678 |
|
| 679 |
A prvalue of floating-point type can be converted to a prvalue of
|
| 680 |
-
another floating-point type
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
the
|
|
|
|
|
|
|
|
|
|
| 686 |
|
| 687 |
The conversions allowed as floating-point promotions are excluded from
|
| 688 |
the set of floating-point conversions.
|
| 689 |
|
| 690 |
### Floating-integral conversions <a id="conv.fpint">[[conv.fpint]]</a>
|
|
@@ -807,36 +821,40 @@ This pattern is called the *usual arithmetic conversions*, which are
|
|
| 807 |
defined as follows:
|
| 808 |
|
| 809 |
- If either operand is of scoped enumeration type [[dcl.enum]], no
|
| 810 |
conversions are performed; if the other operand does not have the same
|
| 811 |
type, the expression is ill-formed.
|
| 812 |
-
-
|
| 813 |
-
|
| 814 |
-
- Otherwise, if either operand is `double`, the other shall be converted
|
| 815 |
-
to `double`.
|
| 816 |
-
- Otherwise, if either operand is `float`, the other shall be converted
|
| 817 |
-
to `float`.
|
| 818 |
-
- Otherwise, the integral promotions [[conv.prom]] shall be performed on
|
| 819 |
-
both operands.[^9] Then the following rules shall be applied to the
|
| 820 |
-
promoted operands:
|
| 821 |
- If both operands have the same type, no further conversion is
|
| 822 |
needed.
|
| 823 |
-
- Otherwise, if
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 838 |
|
| 839 |
If one operand is of enumeration type and the other operand is of a
|
| 840 |
different enumeration type or a floating-point type, this behavior is
|
| 841 |
deprecated [[depr.arith.conv.enum]].
|
| 842 |
|
|
@@ -853,35 +871,43 @@ primary-expression:
|
|
| 853 |
requires-expression
|
| 854 |
```
|
| 855 |
|
| 856 |
### Literals <a id="expr.prim.literal">[[expr.prim.literal]]</a>
|
| 857 |
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
value category as the corresponding
|
| 862 |
-
in [[lex.ext]], and any other
|
|
|
|
| 863 |
|
| 864 |
### This <a id="expr.prim.this">[[expr.prim.this]]</a>
|
| 865 |
|
| 866 |
-
The keyword `this` names a pointer to the object for which
|
| 867 |
-
member function [[class.
|
| 868 |
-
initializer [[class.mem]] is evaluated.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
|
| 870 |
If a declaration declares a member function or member function template
|
| 871 |
of a class `X`, the expression `this` is a prvalue of type “pointer to
|
| 872 |
-
*cv-qualifier-seq* `X`”
|
| 873 |
-
end of the *function-definition*,
|
| 874 |
-
It shall not appear
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
|
|
|
| 878 |
|
| 879 |
-
[*Note
|
| 880 |
the complete declarator is known. — *end note*]
|
| 881 |
|
| 882 |
-
[*Note
|
| 883 |
|
| 884 |
In a *trailing-return-type*, the class being defined is not required to
|
| 885 |
be complete for purposes of class member access [[expr.ref]]. Class
|
| 886 |
members declared later are not visible.
|
| 887 |
|
|
@@ -900,21 +926,21 @@ template auto A::f(int t) -> decltype(t + g());
|
|
| 900 |
|
| 901 |
— *end note*]
|
| 902 |
|
| 903 |
Otherwise, if a *member-declarator* declares a non-static data member
|
| 904 |
[[class.mem]] of a class `X`, the expression `this` is a prvalue of type
|
| 905 |
-
“pointer to `X`”
|
| 906 |
-
[[class.mem]].
|
| 907 |
|
| 908 |
The expression `this` shall not appear in any other context.
|
| 909 |
|
| 910 |
[*Example 2*:
|
| 911 |
|
| 912 |
``` cpp
|
| 913 |
class Outer {
|
| 914 |
int a[sizeof(*this)]; // error: not inside a member function
|
| 915 |
-
unsigned int sz = sizeof(*this); // OK
|
| 916 |
|
| 917 |
void f() {
|
| 918 |
int b[sizeof(*this)]; // OK
|
| 919 |
|
| 920 |
struct Inner {
|
|
@@ -927,16 +953,19 @@ class Outer {
|
|
| 927 |
— *end example*]
|
| 928 |
|
| 929 |
### Parentheses <a id="expr.prim.paren">[[expr.prim.paren]]</a>
|
| 930 |
|
| 931 |
A parenthesized expression `(E)` is a primary expression whose type,
|
| 932 |
-
|
| 933 |
-
expression can be used in exactly the same contexts as
|
| 934 |
-
be used, and with the same meaning, except as
|
|
|
|
| 935 |
|
| 936 |
### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
|
| 937 |
|
|
|
|
|
|
|
| 938 |
``` bnf
|
| 939 |
id-expression:
|
| 940 |
unqualified-id
|
| 941 |
qualified-id
|
| 942 |
```
|
|
@@ -944,63 +973,71 @@ id-expression:
|
|
| 944 |
An *id-expression* is a restricted form of a *primary-expression*.
|
| 945 |
|
| 946 |
[*Note 1*: An *id-expression* can appear after `.` and `->` operators
|
| 947 |
[[expr.ref]]. — *end note*]
|
| 948 |
|
| 949 |
-
|
| 950 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 951 |
|
| 952 |
- as part of a class member access [[expr.ref]] in which the object
|
| 953 |
expression refers to the member’s class[^10] or a class derived from
|
| 954 |
that class, or
|
| 955 |
- to form a pointer to member [[expr.unary.op]], or
|
| 956 |
- if that *id-expression* denotes a non-static data member and it
|
| 957 |
appears in an unevaluated operand.
|
| 958 |
-
\[*Example
|
| 959 |
``` cpp
|
| 960 |
struct S {
|
| 961 |
int m;
|
| 962 |
};
|
| 963 |
int i = sizeof(S::m); // OK
|
| 964 |
int j = sizeof(S::m + 42); // OK
|
| 965 |
```
|
| 966 |
|
| 967 |
— *end example*]
|
| 968 |
|
| 969 |
-
A potentially-evaluated *id-expression* that denotes an immediate
|
| 970 |
-
function [[dcl.constexpr]] shall appear only
|
| 971 |
-
|
| 972 |
-
- as a subexpression of an immediate invocation, or
|
| 973 |
-
- in an immediate function context [[expr.const]].
|
| 974 |
-
|
| 975 |
For an *id-expression* that denotes an overload set, overload resolution
|
| 976 |
-
is performed to select a unique function
|
| 977 |
-
[[over.over]]).
|
| 978 |
|
| 979 |
-
[*Note
|
| 980 |
|
| 981 |
A program cannot refer to a function with a trailing *requires-clause*
|
| 982 |
whose *constraint-expression* is not satisfied, because such functions
|
| 983 |
are never selected by overload resolution.
|
| 984 |
|
| 985 |
-
[*Example
|
| 986 |
|
| 987 |
``` cpp
|
| 988 |
template<typename T> struct A {
|
| 989 |
static void f(int) requires false;
|
| 990 |
-
}
|
| 991 |
|
| 992 |
void g() {
|
| 993 |
A<int>::f(0); // error: cannot call f
|
| 994 |
void (*p1)(int) = A<int>::f; // error: cannot take the address of f
|
| 995 |
decltype(A<int>::f)* p2 = nullptr; // error: the type decltype(A<int>::f) is invalid
|
| 996 |
}
|
| 997 |
```
|
| 998 |
|
| 999 |
In each case, the constraints of `f` are not satisfied. In the
|
| 1000 |
declaration of `p2`, those constraints are required to be satisfied even
|
| 1001 |
-
though `f` is an unevaluated operand [[
|
| 1002 |
|
| 1003 |
— *end example*]
|
| 1004 |
|
| 1005 |
— *end note*]
|
| 1006 |
|
|
@@ -1027,56 +1064,109 @@ the copy of the parameter [[dcl.fct.def.coroutine]].
|
|
| 1027 |
*literal-operator-id*s, see [[over.literal]]; for *template-id*s, see
|
| 1028 |
[[temp.names]]. A *type-name* or *decltype-specifier* prefixed by `~`
|
| 1029 |
denotes the destructor of the type so named; see [[expr.prim.id.dtor]].
|
| 1030 |
Within the definition of a non-static member function, an *identifier*
|
| 1031 |
that names a non-static member is transformed to a class member access
|
| 1032 |
-
expression
|
| 1033 |
-
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1044 |
type of such an identifier will typically be `const`
|
| 1045 |
qualified. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1046 |
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
[*Note 3*: If the entity is a template parameter object for a template
|
| 1050 |
parameter of type `T` [[temp.param]], the type of the expression is
|
| 1051 |
`const T`. — *end note*]
|
| 1052 |
|
| 1053 |
-
[*Note
|
| 1054 |
it is cv-qualified or is a reference type. — *end note*]
|
| 1055 |
|
| 1056 |
-
The expression is an
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
-
|
|
|
|
| 1060 |
|
| 1061 |
[*Example 1*:
|
| 1062 |
|
| 1063 |
``` cpp
|
| 1064 |
void f() {
|
| 1065 |
float x, &r = x;
|
| 1066 |
-
|
|
|
|
|
|
|
| 1067 |
decltype(x) y1; // y1 has type float
|
| 1068 |
-
decltype((x)) y2 = y1; // y2 has type float const&
|
| 1069 |
-
// is not mutable and x is an lvalue
|
| 1070 |
decltype(r) r1 = y1; // r1 has type float&
|
| 1071 |
decltype((r)) r2 = y2; // r2 has type float const&
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1072 |
};
|
| 1073 |
}
|
| 1074 |
```
|
| 1075 |
|
| 1076 |
— *end example*]
|
| 1077 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1078 |
#### Qualified names <a id="expr.prim.id.qual">[[expr.prim.id.qual]]</a>
|
| 1079 |
|
| 1080 |
``` bnf
|
| 1081 |
qualified-id:
|
| 1082 |
nested-name-specifier templateₒₚₜ unqualified-id
|
|
@@ -1090,56 +1180,66 @@ nested-name-specifier:
|
|
| 1090 |
decltype-specifier '::'
|
| 1091 |
nested-name-specifier identifier '::'
|
| 1092 |
nested-name-specifier templateₒₚₜ simple-template-id '::'
|
| 1093 |
```
|
| 1094 |
|
| 1095 |
-
The
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
-
|
| 1103 |
-
|
| 1104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1105 |
and a prvalue otherwise.
|
| 1106 |
|
| 1107 |
-
[*Note 1*: A class member can be referred to using a *qualified-id* at
|
| 1108 |
-
any point in its potential scope [[basic.scope.class]]. — *end note*]
|
| 1109 |
-
|
| 1110 |
-
Where *type-name* `::~` *type-name* is used, the two *type-name*s shall
|
| 1111 |
-
refer to the same type (ignoring cv-qualifications); this notation
|
| 1112 |
-
denotes the destructor of the type so named [[expr.prim.id.dtor]]. The
|
| 1113 |
-
*unqualified-id* in a *qualified-id* shall not be of the form
|
| 1114 |
-
`~`*decltype-specifier*.
|
| 1115 |
-
|
| 1116 |
-
The *nested-name-specifier* `::` names the global namespace. A
|
| 1117 |
-
*nested-name-specifier* that names a namespace [[basic.namespace]],
|
| 1118 |
-
optionally followed by the keyword `template` [[temp.names]], and then
|
| 1119 |
-
followed by the name of a member of that namespace (or the name of a
|
| 1120 |
-
member of a namespace made visible by a *using-directive*), is a
|
| 1121 |
-
*qualified-id*; [[namespace.qual]] describes name lookup for namespace
|
| 1122 |
-
members that appear in *qualified-id*s. The result is the member. The
|
| 1123 |
-
type of the result is the type of the member. The result is an lvalue if
|
| 1124 |
-
the member is a function, a variable, or a structured binding
|
| 1125 |
-
[[dcl.struct.bind]] and a prvalue otherwise.
|
| 1126 |
-
|
| 1127 |
-
A *nested-name-specifier* that denotes an enumeration [[dcl.enum]],
|
| 1128 |
-
followed by the name of an enumerator of that enumeration, is a
|
| 1129 |
-
*qualified-id* that refers to the enumerator. The result is the
|
| 1130 |
-
enumerator. The type of the result is the type of the enumeration. The
|
| 1131 |
-
result is a prvalue.
|
| 1132 |
-
|
| 1133 |
-
In a *qualified-id*, if the *unqualified-id* is a
|
| 1134 |
-
*conversion-function-id*, its *conversion-type-id* is first looked up in
|
| 1135 |
-
the class denoted by the *nested-name-specifier* of the *qualified-id*
|
| 1136 |
-
and the name, if found, is used. Otherwise, it is looked up in the
|
| 1137 |
-
context in which the entire *qualified-id* occurs. In each of these
|
| 1138 |
-
lookups, only names that denote types or templates whose specializations
|
| 1139 |
-
are types are considered.
|
| 1140 |
-
|
| 1141 |
#### Destruction <a id="expr.prim.id.dtor">[[expr.prim.id.dtor]]</a>
|
| 1142 |
|
| 1143 |
An *id-expression* that denotes the destructor of a type `T` names the
|
| 1144 |
destructor of `T` if `T` is a class type [[class.dtor]], otherwise the
|
| 1145 |
*id-expression* is said to name a *pseudo-destructor*.
|
|
@@ -1147,12 +1247,12 @@ destructor of `T` if `T` is a class type [[class.dtor]], otherwise the
|
|
| 1147 |
If the *id-expression* names a pseudo-destructor, `T` shall be a scalar
|
| 1148 |
type and the *id-expression* shall appear as the right operand of a
|
| 1149 |
class member access [[expr.ref]] that forms the *postfix-expression* of
|
| 1150 |
a function call [[expr.call]].
|
| 1151 |
|
| 1152 |
-
[*Note 1*: Such a call ends the lifetime of the object
|
| 1153 |
-
[[basic.life]]
|
| 1154 |
|
| 1155 |
[*Example 1*:
|
| 1156 |
|
| 1157 |
``` cpp
|
| 1158 |
struct C { };
|
|
@@ -1169,25 +1269,45 @@ void f() {
|
|
| 1169 |
|
| 1170 |
— *end example*]
|
| 1171 |
|
| 1172 |
### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
|
| 1173 |
|
|
|
|
|
|
|
| 1174 |
``` bnf
|
| 1175 |
lambda-expression:
|
| 1176 |
-
lambda-introducer
|
| 1177 |
-
lambda-introducer '<' template-parameter-list '>' requires-clauseₒₚₜ
|
|
|
|
| 1178 |
```
|
| 1179 |
|
| 1180 |
``` bnf
|
| 1181 |
lambda-introducer:
|
| 1182 |
'[' lambda-captureₒₚₜ ']'
|
| 1183 |
```
|
| 1184 |
|
| 1185 |
``` bnf
|
| 1186 |
lambda-declarator:
|
| 1187 |
-
|
| 1188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1189 |
```
|
| 1190 |
|
| 1191 |
A *lambda-expression* provides a concise way to create a simple function
|
| 1192 |
object.
|
| 1193 |
|
|
@@ -1207,29 +1327,46 @@ A *lambda-expression* is a prvalue whose result object is called the
|
|
| 1207 |
*closure object*.
|
| 1208 |
|
| 1209 |
[*Note 1*: A closure object behaves like a function object
|
| 1210 |
[[function.objects]]. — *end note*]
|
| 1211 |
|
| 1212 |
-
|
| 1213 |
-
*
|
|
|
|
|
|
|
|
|
|
| 1214 |
|
| 1215 |
-
[*Note 2*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1216 |
[[dcl.decl]]. — *end note*]
|
| 1217 |
|
| 1218 |
-
If a *lambda-
|
| 1219 |
-
|
| 1220 |
-
|
| 1221 |
-
*trailing-return-type*
|
| 1222 |
-
|
|
|
|
|
|
|
| 1223 |
|
| 1224 |
[*Example 2*:
|
| 1225 |
|
| 1226 |
``` cpp
|
| 1227 |
-
auto x1 = [](int i){ return i; };
|
| 1228 |
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 1229 |
int j;
|
| 1230 |
-
auto x3 = []()->auto&& { return j; };
|
| 1231 |
```
|
| 1232 |
|
| 1233 |
— *end example*]
|
| 1234 |
|
| 1235 |
A lambda is a *generic lambda* if the *lambda-expression* has any
|
|
@@ -1237,12 +1374,12 @@ generic parameter type placeholders [[dcl.spec.auto]], or if the lambda
|
|
| 1237 |
has a *template-parameter-list*.
|
| 1238 |
|
| 1239 |
[*Example 3*:
|
| 1240 |
|
| 1241 |
``` cpp
|
| 1242 |
-
int i = [](int i, auto a) { return i; }(3, 4); // OK
|
| 1243 |
-
int j = []<class T>(T t, int i) { return i; }(3, 4); // OK
|
| 1244 |
```
|
| 1245 |
|
| 1246 |
— *end example*]
|
| 1247 |
|
| 1248 |
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
|
@@ -1281,56 +1418,90 @@ and whose *template-parameter-list* consists of the specified
|
|
| 1281 |
call operator template is the *requires-clause* immediately following
|
| 1282 |
`<` *template-parameter-list* `>`, if any. The trailing
|
| 1283 |
*requires-clause* of the function call operator or operator template is
|
| 1284 |
the *requires-clause* of the *lambda-declarator*, if any.
|
| 1285 |
|
| 1286 |
-
[*Note 2*: The function call operator template for a generic lambda
|
| 1287 |
-
|
| 1288 |
|
| 1289 |
[*Example 1*:
|
| 1290 |
|
| 1291 |
``` cpp
|
| 1292 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
| 1293 |
bool b = glambda(3, 3.14); // OK
|
| 1294 |
|
| 1295 |
auto vglambda = [](auto printer) {
|
| 1296 |
-
return [=](auto&& ... ts) { // OK
|
| 1297 |
printer(std::forward<decltype(ts)>(ts)...);
|
| 1298 |
|
| 1299 |
return [=]() {
|
| 1300 |
printer(ts ...);
|
| 1301 |
};
|
| 1302 |
};
|
| 1303 |
};
|
| 1304 |
auto p = vglambda( [](auto v1, auto v2, auto v3)
|
| 1305 |
{ std::cout << v1 << v2 << v3; } );
|
| 1306 |
-
auto q = p(1, 'a', 3.14); // OK
|
| 1307 |
-
q(); // OK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1308 |
```
|
| 1309 |
|
| 1310 |
— *end example*]
|
| 1311 |
|
| 1312 |
-
The function call operator or operator template is
|
| 1313 |
-
[[class.
|
| 1314 |
-
*parameter-declaration-clause* is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1315 |
neither virtual nor declared `volatile`. Any *noexcept-specifier*
|
| 1316 |
specified on a *lambda-expression* applies to the corresponding function
|
| 1317 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 1318 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 1319 |
-
call operator or operator template.
|
| 1320 |
-
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
|
|
|
|
|
|
| 1324 |
immediate function [[dcl.constexpr]] if the corresponding
|
| 1325 |
*lambda-expression*'s *parameter-declaration-clause* is followed by
|
| 1326 |
`consteval`.
|
| 1327 |
|
| 1328 |
-
[*
|
| 1329 |
-
the context in which the *lambda-expression* appears. — *end note*]
|
| 1330 |
-
|
| 1331 |
-
[*Example 2*:
|
| 1332 |
|
| 1333 |
``` cpp
|
| 1334 |
auto ID = [](auto a) { return a; };
|
| 1335 |
static_assert(ID(3) == 3); // OK
|
| 1336 |
|
|
@@ -1341,11 +1512,11 @@ struct NonLiteral {
|
|
| 1341 |
static_assert(ID(NonLiteral{3}).n == 3); // error
|
| 1342 |
```
|
| 1343 |
|
| 1344 |
— *end example*]
|
| 1345 |
|
| 1346 |
-
[*Example
|
| 1347 |
|
| 1348 |
``` cpp
|
| 1349 |
auto monoid = [](auto v) { return [=] { return v; }; };
|
| 1350 |
auto add = [](auto m1) constexpr {
|
| 1351 |
auto ret = m1();
|
|
@@ -1370,18 +1541,18 @@ static_assert(add(one)(one)() == two()); // error: two() is not a constan
|
|
| 1370 |
static_assert(add(one)(one)() == monoid(2)()); // OK
|
| 1371 |
```
|
| 1372 |
|
| 1373 |
— *end example*]
|
| 1374 |
|
| 1375 |
-
[*Note
|
| 1376 |
|
| 1377 |
-
The function call operator or operator template
|
| 1378 |
[[temp.constr.decl]] by a *type-constraint* [[temp.param]], a
|
| 1379 |
*requires-clause* [[temp.pre]], or a trailing *requires-clause*
|
| 1380 |
[[dcl.decl]].
|
| 1381 |
|
| 1382 |
-
[*Example
|
| 1383 |
|
| 1384 |
``` cpp
|
| 1385 |
template <typename T> concept C1 = ...;
|
| 1386 |
template <std::size_t N> concept C2 = ...;
|
| 1387 |
template <typename A, typename B> concept C3 = ...;
|
|
@@ -1402,27 +1573,29 @@ The closure type for a non-generic *lambda-expression* with no
|
|
| 1402 |
*lambda-capture* whose constraints (if any) are satisfied has a
|
| 1403 |
conversion function to pointer to function with C++ language linkage
|
| 1404 |
[[dcl.link]] having the same parameter and return types as the closure
|
| 1405 |
type’s function call operator. The conversion is to “pointer to
|
| 1406 |
`noexcept` function” if the function call operator has a non-throwing
|
| 1407 |
-
exception specification.
|
| 1408 |
-
|
| 1409 |
-
|
| 1410 |
-
|
| 1411 |
-
|
| 1412 |
-
|
| 1413 |
-
function
|
|
|
|
|
|
|
| 1414 |
|
| 1415 |
For a generic lambda with no *lambda-capture*, the closure type has a
|
| 1416 |
conversion function template to pointer to function. The conversion
|
| 1417 |
function template has the same invented template parameter list, and the
|
| 1418 |
pointer to function has the same parameter types, as the function call
|
| 1419 |
operator template. The return type of the pointer to function shall
|
| 1420 |
behave as if it were a *decltype-specifier* denoting the return type of
|
| 1421 |
the corresponding function call operator template specialization.
|
| 1422 |
|
| 1423 |
-
[*Note
|
| 1424 |
|
| 1425 |
If the generic lambda has no *trailing-return-type* or the
|
| 1426 |
*trailing-return-type* contains a placeholder type, return type
|
| 1427 |
deduction of the corresponding function call operator template
|
| 1428 |
specialization has to be done. The corresponding specialization is that
|
|
@@ -1454,11 +1627,11 @@ struct Closure {
|
|
| 1454 |
};
|
| 1455 |
```
|
| 1456 |
|
| 1457 |
— *end note*]
|
| 1458 |
|
| 1459 |
-
[*Example
|
| 1460 |
|
| 1461 |
``` cpp
|
| 1462 |
void f1(int (*)(int)) { }
|
| 1463 |
void f2(char (*)(int)) { }
|
| 1464 |
|
|
@@ -1470,45 +1643,49 @@ void h(char (*)(int)) { } // #4
|
|
| 1470 |
|
| 1471 |
auto glambda = [](auto a) { return a; };
|
| 1472 |
f1(glambda); // OK
|
| 1473 |
f2(glambda); // error: ID is not convertible
|
| 1474 |
g(glambda); // error: ambiguous
|
| 1475 |
-
h(glambda); // OK
|
| 1476 |
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 1477 |
```
|
| 1478 |
|
| 1479 |
— *end example*]
|
| 1480 |
|
| 1481 |
-
|
| 1482 |
-
|
| 1483 |
-
|
| 1484 |
-
function call operator template specialization
|
| 1485 |
-
|
| 1486 |
-
|
| 1487 |
-
|
| 1488 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1489 |
|
| 1490 |
-
[*Note
|
| 1491 |
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 1492 |
parameter types are required to match the return type and parameter
|
| 1493 |
types of the pointer to function. — *end note*]
|
| 1494 |
|
| 1495 |
-
[*Example
|
| 1496 |
|
| 1497 |
``` cpp
|
| 1498 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 1499 |
-
int (*GL_int)(int) = GL; // OK
|
| 1500 |
-
GL_int(3); // OK
|
| 1501 |
```
|
| 1502 |
|
| 1503 |
— *end example*]
|
| 1504 |
|
| 1505 |
The conversion function or conversion function template is public,
|
| 1506 |
constexpr, non-virtual, non-explicit, const, and has a non-throwing
|
| 1507 |
exception specification [[except.spec]].
|
| 1508 |
|
| 1509 |
-
[*Example
|
| 1510 |
|
| 1511 |
``` cpp
|
| 1512 |
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 1513 |
auto C = [](auto a) { return a; };
|
| 1514 |
|
|
@@ -1520,18 +1697,14 @@ static_assert(Fwd(NC,3) == 3); // error
|
|
| 1520 |
```
|
| 1521 |
|
| 1522 |
— *end example*]
|
| 1523 |
|
| 1524 |
The *lambda-expression*’s *compound-statement* yields the
|
| 1525 |
-
*function-body* [[dcl.fct.def]] of the function call operator, but
|
| 1526 |
-
|
| 1527 |
-
of `this` [[class.this]] and transforming *id-expression*s referring to
|
| 1528 |
-
non-static class members into class member access expressions using
|
| 1529 |
-
`(*this)` ([[class.mfct.non-static]]), the *compound-statement* is
|
| 1530 |
-
considered in the context of the *lambda-expression*.
|
| 1531 |
|
| 1532 |
-
[*Example
|
| 1533 |
|
| 1534 |
``` cpp
|
| 1535 |
struct S1 {
|
| 1536 |
int x, y;
|
| 1537 |
int operator()(int);
|
|
@@ -1556,12 +1729,12 @@ defaulted default constructor otherwise. It has a defaulted copy
|
|
| 1556 |
constructor and a defaulted move constructor [[class.copy.ctor]]. It has
|
| 1557 |
a deleted copy assignment operator if the *lambda-expression* has a
|
| 1558 |
*lambda-capture* and defaulted copy and move assignment operators
|
| 1559 |
otherwise [[class.copy.assign]].
|
| 1560 |
|
| 1561 |
-
[*Note
|
| 1562 |
-
usual,
|
| 1563 |
|
| 1564 |
The closure type associated with a *lambda-expression* has an
|
| 1565 |
implicitly-declared destructor [[class.dtor]].
|
| 1566 |
|
| 1567 |
A member of a closure type shall not be explicitly instantiated
|
|
@@ -1607,13 +1780,12 @@ simple-capture:
|
|
| 1607 |
init-capture:
|
| 1608 |
'...'ₒₚₜ identifier initializer
|
| 1609 |
'&' '...'ₒₚₜ identifier initializer
|
| 1610 |
```
|
| 1611 |
|
| 1612 |
-
The body of a *lambda-expression* may refer to
|
| 1613 |
-
|
| 1614 |
-
scopes by capturing those entities, as described below.
|
| 1615 |
|
| 1616 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 1617 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 1618 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 1619 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
|
@@ -1646,35 +1818,37 @@ A *lambda-expression* shall not have a *capture-default* or
|
|
| 1646 |
*simple-capture* in its *lambda-introducer* unless its innermost
|
| 1647 |
enclosing scope is a block scope [[basic.scope.block]] or it appears
|
| 1648 |
within a default member initializer and its innermost enclosing scope is
|
| 1649 |
the corresponding class scope [[basic.scope.class]].
|
| 1650 |
|
| 1651 |
-
The *identifier* in a *simple-capture*
|
| 1652 |
-
|
| 1653 |
-
lookup shall find a local entity. The *simple-capture*s `this` and
|
| 1654 |
`* this` denote the local entity `*this`. An entity that is designated
|
| 1655 |
by a *simple-capture* is said to be *explicitly captured*.
|
| 1656 |
|
| 1657 |
-
If an *identifier* in a *
|
| 1658 |
-
|
| 1659 |
-
|
|
|
|
| 1660 |
|
| 1661 |
[*Example 2*:
|
| 1662 |
|
| 1663 |
``` cpp
|
| 1664 |
void f() {
|
| 1665 |
int x = 0;
|
| 1666 |
-
auto g = [x](int x) { return 0; }; // error: parameter and
|
|
|
|
|
|
|
| 1667 |
}
|
| 1668 |
```
|
| 1669 |
|
| 1670 |
— *end example*]
|
| 1671 |
|
| 1672 |
-
An *init-capture*
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
*
|
| 1676 |
|
| 1677 |
- if the capture is by copy (see below), the non-static data member
|
| 1678 |
declared for the capture and the variable are treated as two different
|
| 1679 |
ways of referring to the same object, which has the lifetime of the
|
| 1680 |
non-static data member, and no additional copy and destruction is
|
|
@@ -1693,11 +1867,14 @@ int x = 4;
|
|
| 1693 |
auto y = [&r = x, x = x+1]()->int {
|
| 1694 |
r += 2;
|
| 1695 |
return x+2;
|
| 1696 |
}(); // Updates ::x to 6, and initializes y to 7.
|
| 1697 |
|
| 1698 |
-
auto z = [a = 42](int a) { return 1; }; // error: parameter and local variable have the same name
|
|
|
|
|
|
|
|
|
|
| 1699 |
```
|
| 1700 |
|
| 1701 |
— *end example*]
|
| 1702 |
|
| 1703 |
For the purposes of lambda capture, an expression potentially references
|
|
@@ -1711,13 +1888,13 @@ local entities as follows:
|
|
| 1711 |
*id-expression*. — *end note*]
|
| 1712 |
- A `this` expression potentially references `*this`.
|
| 1713 |
- A *lambda-expression* potentially references the local entities named
|
| 1714 |
by its *simple-capture*s.
|
| 1715 |
|
| 1716 |
-
If an expression potentially references a local entity within a
|
| 1717 |
-
|
| 1718 |
-
|
| 1719 |
expressions [[expr.typeid]] were ignored, the entity is said to be
|
| 1720 |
*implicitly captured* by each intervening *lambda-expression* with an
|
| 1721 |
associated *capture-default* that does not explicitly capture it. The
|
| 1722 |
implicit capture of `*this` is deprecated when the *capture-default* is
|
| 1723 |
`=`; see [[depr.capture.this]].
|
|
@@ -1728,38 +1905,38 @@ implicit capture of `*this` is deprecated when the *capture-default* is
|
|
| 1728 |
void f(int, const int (&)[2] = {}); // #1
|
| 1729 |
void f(const int&, const int (&)[1]); // #2
|
| 1730 |
void test() {
|
| 1731 |
const int x = 17;
|
| 1732 |
auto g = [](auto a) {
|
| 1733 |
-
f(x); // OK
|
| 1734 |
};
|
| 1735 |
|
| 1736 |
auto g1 = [=](auto a) {
|
| 1737 |
-
f(x); // OK
|
| 1738 |
};
|
| 1739 |
|
| 1740 |
auto g2 = [=](auto a) {
|
| 1741 |
int selector[sizeof(a) == 1 ? 1 : 2]{};
|
| 1742 |
-
f(x, selector); // OK
|
| 1743 |
};
|
| 1744 |
|
| 1745 |
auto g3 = [=](auto a) {
|
| 1746 |
typeid(a + x); // captures x regardless of whether a + x is an unevaluated operand
|
| 1747 |
};
|
| 1748 |
}
|
| 1749 |
```
|
| 1750 |
|
| 1751 |
-
Within `g1`, an implementation
|
| 1752 |
it is not odr-used.
|
| 1753 |
|
| 1754 |
— *end example*]
|
| 1755 |
|
| 1756 |
[*Note 4*:
|
| 1757 |
|
| 1758 |
The set of captured entities is determined syntactically, and entities
|
| 1759 |
-
|
| 1760 |
-
|
| 1761 |
|
| 1762 |
[*Example 5*:
|
| 1763 |
|
| 1764 |
``` cpp
|
| 1765 |
template<bool B>
|
|
@@ -1775,12 +1952,12 @@ void f(int n) {
|
|
| 1775 |
— *end example*]
|
| 1776 |
|
| 1777 |
— *end note*]
|
| 1778 |
|
| 1779 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 1780 |
-
entity captured by a *lambda-expression* is odr-used [[
|
| 1781 |
-
|
| 1782 |
|
| 1783 |
[*Note 5*: As a consequence, if a *lambda-expression* explicitly
|
| 1784 |
captures an entity that is not odr-usable, the program is ill-formed
|
| 1785 |
[[basic.def.odr]]. — *end note*]
|
| 1786 |
|
|
@@ -1790,26 +1967,26 @@ captures an entity that is not odr-usable, the program is ill-formed
|
|
| 1790 |
void f1(int i) {
|
| 1791 |
int const N = 20;
|
| 1792 |
auto m1 = [=]{
|
| 1793 |
int const M = 30;
|
| 1794 |
auto m2 = [i]{
|
| 1795 |
-
int x[N][M]; // OK
|
| 1796 |
-
x[0][0] = i; // OK
|
| 1797 |
};
|
| 1798 |
};
|
| 1799 |
struct s1 {
|
| 1800 |
int f;
|
| 1801 |
void work(int n) {
|
| 1802 |
int m = n*n;
|
| 1803 |
int j = 40;
|
| 1804 |
auto m3 = [this,m] {
|
| 1805 |
auto m4 = [&,j] { // error: j not odr-usable due to intervening lambda m3
|
| 1806 |
int x = n; // error: n is odr-used but not odr-usable due to intervening lambda m3
|
| 1807 |
-
x += m; // OK
|
| 1808 |
x += i; // error: i is odr-used but not odr-usable
|
| 1809 |
// due to intervening function and class scopes
|
| 1810 |
-
x += f; // OK
|
| 1811 |
};
|
| 1812 |
};
|
| 1813 |
}
|
| 1814 |
};
|
| 1815 |
}
|
|
@@ -1872,11 +2049,11 @@ the entity is a reference to an object, an lvalue reference to the
|
|
| 1872 |
referenced function type if the entity is a reference to a function, or
|
| 1873 |
the type of the corresponding captured entity otherwise. A member of an
|
| 1874 |
anonymous union shall not be captured by copy.
|
| 1875 |
|
| 1876 |
Every *id-expression* within the *compound-statement* of a
|
| 1877 |
-
*lambda-expression* that is an odr-use [[
|
| 1878 |
captured by copy is transformed into an access to the corresponding
|
| 1879 |
unnamed data member of the closure type.
|
| 1880 |
|
| 1881 |
[*Note 7*: An *id-expression* that is not an odr-use refers to the
|
| 1882 |
original entity, never to a member of the closure type. However, such an
|
|
@@ -1892,12 +2069,12 @@ the closure type.
|
|
| 1892 |
``` cpp
|
| 1893 |
void f(const int*);
|
| 1894 |
void g() {
|
| 1895 |
const int N = 10;
|
| 1896 |
[=] {
|
| 1897 |
-
int arr[N]; // OK
|
| 1898 |
-
f(&N); // OK
|
| 1899 |
// the corresponding member of the closure type
|
| 1900 |
};
|
| 1901 |
}
|
| 1902 |
```
|
| 1903 |
|
|
@@ -1945,13 +2122,15 @@ auto h(int &r) {
|
|
| 1945 |
|
| 1946 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 1947 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 1948 |
`m2`’s capture is transformed as follows:
|
| 1949 |
|
| 1950 |
-
-
|
| 1951 |
-
non-static data member of `m1`’s closure type;
|
| 1952 |
-
|
|
|
|
|
|
|
| 1953 |
entity captured by `m1`.
|
| 1954 |
|
| 1955 |
[*Example 11*:
|
| 1956 |
|
| 1957 |
The nested *lambda-expression*s and invocations below will output
|
|
@@ -1992,12 +2171,11 @@ captured by reference, invoking the function call operator of the
|
|
| 1992 |
corresponding *lambda-expression* after the lifetime of the entity has
|
| 1993 |
ended is likely to result in undefined behavior. — *end note*]
|
| 1994 |
|
| 1995 |
A *simple-capture* containing an ellipsis is a pack expansion
|
| 1996 |
[[temp.variadic]]. An *init-capture* containing an ellipsis is a pack
|
| 1997 |
-
expansion that
|
| 1998 |
-
declarative region is the *lambda-expression*’s *compound-statement*.
|
| 1999 |
|
| 2000 |
[*Example 12*:
|
| 2001 |
|
| 2002 |
``` cpp
|
| 2003 |
template<class... Args>
|
|
@@ -2064,10 +2242,12 @@ bool f(Args ...args) {
|
|
| 2064 |
|
| 2065 |
— *end example*]
|
| 2066 |
|
| 2067 |
### Requires expressions <a id="expr.prim.req">[[expr.prim.req]]</a>
|
| 2068 |
|
|
|
|
|
|
|
| 2069 |
A *requires-expression* provides a concise way to express requirements
|
| 2070 |
on template arguments that can be checked by name lookup
|
| 2071 |
[[basic.lookup]] or by checking properties of types and expressions.
|
| 2072 |
|
| 2073 |
``` bnf
|
|
@@ -2075,22 +2255,22 @@ requires-expression:
|
|
| 2075 |
requires requirement-parameter-listₒₚₜ requirement-body
|
| 2076 |
```
|
| 2077 |
|
| 2078 |
``` bnf
|
| 2079 |
requirement-parameter-list:
|
| 2080 |
-
'(' parameter-declaration-clause
|
| 2081 |
```
|
| 2082 |
|
| 2083 |
``` bnf
|
| 2084 |
requirement-body:
|
| 2085 |
'{' requirement-seq '}'
|
| 2086 |
```
|
| 2087 |
|
| 2088 |
``` bnf
|
| 2089 |
requirement-seq:
|
| 2090 |
requirement
|
| 2091 |
-
requirement
|
| 2092 |
```
|
| 2093 |
|
| 2094 |
``` bnf
|
| 2095 |
requirement:
|
| 2096 |
simple-requirement
|
|
@@ -2099,11 +2279,11 @@ requirement:
|
|
| 2099 |
nested-requirement
|
| 2100 |
```
|
| 2101 |
|
| 2102 |
A *requires-expression* is a prvalue of type `bool` whose value is
|
| 2103 |
described below. Expressions appearing within a *requirement-body* are
|
| 2104 |
-
unevaluated operands [[
|
| 2105 |
|
| 2106 |
[*Example 1*:
|
| 2107 |
|
| 2108 |
A common use of *requires-expression*s is to define requirements in
|
| 2109 |
concepts such as the one below:
|
|
@@ -2131,13 +2311,11 @@ introduces the *requires-expression*.
|
|
| 2131 |
|
| 2132 |
— *end example*]
|
| 2133 |
|
| 2134 |
A *requires-expression* may introduce local parameters using a
|
| 2135 |
*parameter-declaration-clause* [[dcl.fct]]. A local parameter of a
|
| 2136 |
-
*requires-expression* shall not have a default argument.
|
| 2137 |
-
introduced by a local parameter is in scope from the point of its
|
| 2138 |
-
declaration until the closing brace of the *requirement-body*. These
|
| 2139 |
parameters have no linkage, storage, or lifetime; they are only used as
|
| 2140 |
notation for the purpose of defining *requirement*s. The
|
| 2141 |
*parameter-declaration-clause* of a *requirement-parameter-list* shall
|
| 2142 |
not terminate with an ellipsis.
|
| 2143 |
|
|
@@ -2150,14 +2328,10 @@ concept C = requires(T t, ...) { // error: terminates with an ellipsis
|
|
| 2150 |
};
|
| 2151 |
```
|
| 2152 |
|
| 2153 |
— *end example*]
|
| 2154 |
|
| 2155 |
-
The *requirement-body* contains a sequence of *requirement*s. These
|
| 2156 |
-
*requirement*s may refer to local parameters, template parameters, and
|
| 2157 |
-
any other declarations visible from the enclosing context.
|
| 2158 |
-
|
| 2159 |
The substitution of template arguments into a *requires-expression* may
|
| 2160 |
result in the formation of invalid types or expressions in its
|
| 2161 |
*requirement*s or the violation of the semantic constraints of those
|
| 2162 |
*requirement*s. In such cases, the *requires-expression* evaluates to
|
| 2163 |
`false`; it does not cause the program to be ill-formed. The
|
|
@@ -2196,11 +2370,12 @@ simple-requirement:
|
|
| 2196 |
|
| 2197 |
A *simple-requirement* asserts the validity of an *expression*.
|
| 2198 |
|
| 2199 |
[*Note 1*: The enclosing *requires-expression* will evaluate to `false`
|
| 2200 |
if substitution of template arguments into the *expression* fails. The
|
| 2201 |
-
*expression* is an unevaluated operand
|
|
|
|
| 2202 |
|
| 2203 |
[*Example 1*:
|
| 2204 |
|
| 2205 |
``` cpp
|
| 2206 |
template<typename T> concept C =
|
|
@@ -2235,19 +2410,20 @@ if substitution of template arguments fails. — *end note*]
|
|
| 2235 |
template<typename T, typename T::type = 0> struct S;
|
| 2236 |
template<typename T> using Ref = T&;
|
| 2237 |
|
| 2238 |
template<typename T> concept C = requires {
|
| 2239 |
typename T::inner; // required nested member name
|
| 2240 |
-
typename S<T>; // required
|
|
|
|
| 2241 |
typename Ref<T>; // required alias template substitution, fails if T is void
|
| 2242 |
};
|
| 2243 |
```
|
| 2244 |
|
| 2245 |
— *end example*]
|
| 2246 |
|
| 2247 |
A *type-requirement* that names a class template specialization does not
|
| 2248 |
-
require that type to be complete [[
|
| 2249 |
|
| 2250 |
#### Compound requirements <a id="expr.prim.req.compound">[[expr.prim.req.compound]]</a>
|
| 2251 |
|
| 2252 |
``` bnf
|
| 2253 |
compound-requirement:
|
|
@@ -2270,10 +2446,11 @@ properties proceed in the following order:
|
|
| 2270 |
- If the *return-type-requirement* is present, then:
|
| 2271 |
- Substitution of template arguments (if any) into the
|
| 2272 |
*return-type-requirement* is performed.
|
| 2273 |
- The immediately-declared constraint [[temp.param]] of the
|
| 2274 |
*type-constraint* for `decltype((E))` shall be satisfied.
|
|
|
|
| 2275 |
\[*Example 1*:
|
| 2276 |
Given concepts `C` and `D`,
|
| 2277 |
``` cpp
|
| 2278 |
requires {
|
| 2279 |
{ E1 } -> C;
|
|
@@ -2352,43 +2529,31 @@ template<typename T> concept D = requires (T t) {
|
|
| 2352 |
`D<T>` is satisfied if `sizeof(decltype (+t)) == 1`
|
| 2353 |
[[temp.constr.atomic]].
|
| 2354 |
|
| 2355 |
— *end example*]
|
| 2356 |
|
| 2357 |
-
A local parameter shall only appear as an unevaluated operand
|
| 2358 |
-
[[expr.prop]] within the *constraint-expression*.
|
| 2359 |
-
|
| 2360 |
-
[*Example 2*:
|
| 2361 |
-
|
| 2362 |
-
``` cpp
|
| 2363 |
-
template<typename T> concept C = requires (T a) {
|
| 2364 |
-
requires sizeof(a) == 4; // OK
|
| 2365 |
-
requires a == 0; // error: evaluation of a constraint variable
|
| 2366 |
-
};
|
| 2367 |
-
```
|
| 2368 |
-
|
| 2369 |
-
— *end example*]
|
| 2370 |
-
|
| 2371 |
## Compound expressions <a id="expr.compound">[[expr.compound]]</a>
|
| 2372 |
|
| 2373 |
### Postfix expressions <a id="expr.post">[[expr.post]]</a>
|
| 2374 |
|
|
|
|
|
|
|
| 2375 |
Postfix expressions group left-to-right.
|
| 2376 |
|
| 2377 |
``` bnf
|
| 2378 |
postfix-expression:
|
| 2379 |
primary-expression
|
| 2380 |
-
postfix-expression '['
|
| 2381 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 2382 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 2383 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 2384 |
simple-type-specifier braced-init-list
|
| 2385 |
typename-specifier braced-init-list
|
| 2386 |
postfix-expression '.' 'template'ₒₚₜ id-expression
|
| 2387 |
postfix-expression '->' 'template'ₒₚₜ id-expression
|
| 2388 |
postfix-expression '++'
|
| 2389 |
-
postfix-expression '-
|
| 2390 |
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 2391 |
static_cast '<' type-id '>' '(' expression ')'
|
| 2392 |
reinterpret_cast '<' type-id '>' '(' expression ')'
|
| 2393 |
const_cast '<' type-id '>' '(' expression ')'
|
| 2394 |
typeid '(' expression ')'
|
|
@@ -2399,38 +2564,43 @@ postfix-expression:
|
|
| 2399 |
expression-list:
|
| 2400 |
initializer-list
|
| 2401 |
```
|
| 2402 |
|
| 2403 |
[*Note 1*: The `>` token following the *type-id* in a `dynamic_cast`,
|
| 2404 |
-
`static_cast`, `reinterpret_cast`, or `const_cast`
|
| 2405 |
-
replacing a `>
|
| 2406 |
[[temp.names]]. — *end note*]
|
| 2407 |
|
| 2408 |
#### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 2409 |
|
| 2410 |
-
A
|
| 2411 |
-
|
| 2412 |
-
|
| 2413 |
-
|
| 2414 |
-
|
| 2415 |
-
|
| 2416 |
-
|
| 2417 |
-
|
| 2418 |
-
|
|
|
|
| 2419 |
|
| 2420 |
-
|
| 2421 |
-
|
| 2422 |
-
|
|
|
|
|
|
|
|
|
|
| 2423 |
|
| 2424 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2425 |
commutative operation except for sequencing. See [[expr.unary]] and
|
| 2426 |
[[expr.add]] for details of `*` and `+` and [[dcl.array]] for details
|
| 2427 |
of array types. — *end note*]
|
| 2428 |
|
| 2429 |
-
A *braced-init-list* shall not be used with the built-in subscript
|
| 2430 |
-
operator.
|
| 2431 |
-
|
| 2432 |
#### Function call <a id="expr.call">[[expr.call]]</a>
|
| 2433 |
|
| 2434 |
A function call is a postfix expression followed by parentheses
|
| 2435 |
containing a possibly empty, comma-separated list of
|
| 2436 |
*initializer-clause*s which constitute the arguments to the function.
|
|
@@ -2444,72 +2614,69 @@ type. For a call to a non-member function or to a static member
|
|
| 2444 |
function, the postfix expression shall either be an lvalue that refers
|
| 2445 |
to a function (in which case the function-to-pointer standard conversion
|
| 2446 |
[[conv.func]] is suppressed on the postfix expression), or have function
|
| 2447 |
pointer type.
|
| 2448 |
|
| 2449 |
-
For a call to a non-static member function, the postfix expression shall
|
| 2450 |
-
be an implicit ([[class.mfct.non-static]], [[class.static]]) or
|
| 2451 |
-
explicit class member access [[expr.ref]] whose *id-expression* is a
|
| 2452 |
-
function member name, or a pointer-to-member expression
|
| 2453 |
-
[[expr.mptr.oper]] selecting a function member; the call is as a member
|
| 2454 |
-
of the class object referred to by the object expression. In the case of
|
| 2455 |
-
an implicit class member access, the implied object is the one pointed
|
| 2456 |
-
to by `this`.
|
| 2457 |
-
|
| 2458 |
-
[*Note 2*: A member function call of the form `f()` is interpreted as
|
| 2459 |
-
`(*this).f()` (see [[class.mfct.non-static]]). — *end note*]
|
| 2460 |
-
|
| 2461 |
If the selected function is non-virtual, or if the *id-expression* in
|
| 2462 |
the class member access expression is a *qualified-id*, that function is
|
| 2463 |
called. Otherwise, its final overrider [[class.virtual]] in the dynamic
|
| 2464 |
type of the object expression is called; such a call is referred to as a
|
| 2465 |
*virtual function call*.
|
| 2466 |
|
| 2467 |
-
[*Note
|
| 2468 |
the current value of the object expression. [[class.cdtor]] describes
|
| 2469 |
the behavior of virtual function calls when the object expression refers
|
| 2470 |
to an object under construction or destruction. — *end note*]
|
| 2471 |
|
| 2472 |
-
[*Note
|
| 2473 |
lookup [[basic.lookup]] does not find a declaration of that name, the
|
| 2474 |
program is ill-formed. No function is implicitly declared by such a
|
| 2475 |
call. — *end note*]
|
| 2476 |
|
| 2477 |
If the *postfix-expression* names a destructor or pseudo-destructor
|
| 2478 |
[[expr.prim.id.dtor]], the type of the function call expression is
|
| 2479 |
`void`; otherwise, the type of the function call expression is the
|
| 2480 |
return type of the statically chosen function (i.e., ignoring the
|
| 2481 |
`virtual` keyword), even if the type of the function actually called is
|
| 2482 |
-
different.
|
| 2483 |
-
cv `void`. If the *postfix-expression* names a pseudo-destructor (in
|
| 2484 |
which case the *postfix-expression* is a possibly-parenthesized class
|
| 2485 |
member access), the function call destroys the object of scalar type
|
| 2486 |
-
denoted by the object expression of the class member access
|
| 2487 |
-
[[expr.ref]], [[basic.life]]
|
| 2488 |
|
| 2489 |
-
Calling a function through an expression whose function type is
|
| 2490 |
-
different from the function type of the called function’s definition
|
| 2491 |
-
results in undefined behavior
|
|
|
|
|
|
|
| 2492 |
|
| 2493 |
-
|
| 2494 |
-
|
| 2495 |
-
|
| 2496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2497 |
|
| 2498 |
[*Example 1*:
|
| 2499 |
|
| 2500 |
``` cpp
|
| 2501 |
template<typename ...T> int f(int n = 0, T ...t);
|
| 2502 |
int x = f<int>(); // error: no argument for second function parameter
|
| 2503 |
```
|
| 2504 |
|
| 2505 |
— *end example*]
|
| 2506 |
|
| 2507 |
-
If the function is
|
| 2508 |
-
the function [[
|
| 2509 |
-
of the call, converted as if by an explicit type
|
| 2510 |
-
[[expr.cast]].
|
| 2511 |
|
| 2512 |
[*Note 5*: There is no access or ambiguity checking on this conversion;
|
| 2513 |
the access checking and disambiguation are done as part of the (possibly
|
| 2514 |
implicit) class member access operator. See [[class.member.lookup]],
|
| 2515 |
[[class.access.base]], and [[expr.ref]]. — *end note*]
|
|
@@ -2527,14 +2694,14 @@ enclosing full-expression. The initialization and destruction of each
|
|
| 2527 |
parameter occurs within the context of the calling function.
|
| 2528 |
|
| 2529 |
[*Example 2*: The access of the constructor, conversion functions or
|
| 2530 |
destructor is checked at the point of call in the calling function. If a
|
| 2531 |
constructor or destructor for a function parameter throws an exception,
|
| 2532 |
-
the search for a handler starts in the
|
| 2533 |
-
|
| 2534 |
-
|
| 2535 |
-
|
| 2536 |
|
| 2537 |
The *postfix-expression* is sequenced before each *expression* in the
|
| 2538 |
*expression-list* and any default argument. The initialization of a
|
| 2539 |
parameter, including every associated value computation and side effect,
|
| 2540 |
is indeterminately sequenced with respect to that of any other
|
|
@@ -2590,14 +2757,14 @@ statically chosen function.
|
|
| 2590 |
parameters, but these changes cannot affect the values of the arguments
|
| 2591 |
except where a parameter is of a reference type [[dcl.ref]]; if the
|
| 2592 |
reference is to a const-qualified type, `const_cast` is required to be
|
| 2593 |
used to cast away the constness in order to modify the argument’s value.
|
| 2594 |
Where a parameter is of `const` reference type a temporary object is
|
| 2595 |
-
introduced if needed
|
| 2596 |
-
[[dcl.
|
| 2597 |
-
modify the values of non-constant objects
|
| 2598 |
-
parameters. — *end note*]
|
| 2599 |
|
| 2600 |
A function can be declared to accept fewer arguments (by declaring
|
| 2601 |
default arguments [[dcl.fct.default]]) or more arguments (by using the
|
| 2602 |
ellipsis, `...`, or a function parameter pack [[dcl.fct]]) than the
|
| 2603 |
number of parameters in the function definition [[dcl.fct.def]].
|
|
@@ -2647,73 +2814,93 @@ A *simple-type-specifier* [[dcl.type.simple]] or *typename-specifier*
|
|
| 2647 |
by a *braced-init-list* (the initializer) constructs a value of the
|
| 2648 |
specified type given the initializer. If the type is a placeholder for a
|
| 2649 |
deduced class type, it is replaced by the return type of the function
|
| 2650 |
selected by overload resolution for class template deduction
|
| 2651 |
[[over.match.class.deduct]] for the remainder of this subclause.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2652 |
|
| 2653 |
If the initializer is a parenthesized single expression, the type
|
| 2654 |
conversion expression is equivalent to the corresponding cast expression
|
| 2655 |
[[expr.cast]]. Otherwise, if the type is cv `void` and the initializer
|
| 2656 |
is `()` or `{}` (after pack expansion, if any), the expression is a
|
| 2657 |
-
prvalue of
|
| 2658 |
-
|
| 2659 |
-
|
| 2660 |
-
|
| 2661 |
-
|
| 2662 |
|
| 2663 |
#### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 2664 |
|
| 2665 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 2666 |
-
followed by the keyword `template`
|
| 2667 |
-
|
| 2668 |
-
|
| 2669 |
-
|
| 2670 |
-
the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2671 |
|
| 2672 |
For the first option (dot) the first expression shall be a glvalue. For
|
| 2673 |
the second option (arrow) the first expression shall be a prvalue having
|
| 2674 |
pointer type. The expression `E1->E2` is converted to the equivalent
|
| 2675 |
form `(*(E1)).E2`; the remainder of [[expr.ref]] will address only the
|
| 2676 |
first option (dot).[^13]
|
| 2677 |
|
| 2678 |
Abbreviating *postfix-expression*`.`*id-expression* as `E1.E2`, `E1` is
|
| 2679 |
called the *object expression*. If the object expression is of scalar
|
| 2680 |
type, `E2` shall name the pseudo-destructor of that same type (ignoring
|
| 2681 |
-
cv-qualifications) and `E1.E2` is
|
| 2682 |
returning `void`”.
|
| 2683 |
|
| 2684 |
-
[*Note
|
| 2685 |
[[expr.prim.id.dtor]]. — *end note*]
|
| 2686 |
|
| 2687 |
Otherwise, the object expression shall be of class type. The class type
|
| 2688 |
shall be complete unless the class member access appears in the
|
| 2689 |
definition of that class.
|
| 2690 |
|
| 2691 |
-
[*Note
|
| 2692 |
-
|
| 2693 |
-
[[basic.scope.class]]. — *end note*]
|
| 2694 |
|
| 2695 |
-
|
| 2696 |
-
base classes.
|
| 2697 |
-
|
| 2698 |
-
[*Note 3*: Because the name of a class is inserted in its class scope
|
| 2699 |
-
[[class]], the name of a class is also considered a nested member of
|
| 2700 |
-
that class. — *end note*]
|
| 2701 |
-
|
| 2702 |
-
[*Note 4*: [[basic.lookup.classref]] describes how names are looked up
|
| 2703 |
after the `.` and `->` operators. — *end note*]
|
| 2704 |
|
| 2705 |
If `E2` is a bit-field, `E1.E2` is a bit-field. The type and value
|
| 2706 |
category of `E1.E2` are determined as follows. In the remainder of
|
| 2707 |
[[expr.ref]], *cq* represents either `const` or the absence of `const`
|
| 2708 |
and *vq* represents either `volatile` or the absence of `volatile`. *cv*
|
| 2709 |
represents an arbitrary set of cv-qualifiers, as defined in
|
| 2710 |
[[basic.type.qualifier]].
|
| 2711 |
|
| 2712 |
If `E2` is declared to have type “reference to `T`”, then `E1.E2` is an
|
| 2713 |
-
lvalue
|
| 2714 |
-
|
|
|
|
|
|
|
|
|
|
| 2715 |
|
| 2716 |
- If `E2` is a static data member and the type of `E2` is `T`, then
|
| 2717 |
`E1.E2` is an lvalue; the expression designates the named member of
|
| 2718 |
the class. The type of `E1.E2` is `T`.
|
| 2719 |
- If `E2` is a non-static data member and the type of `E1` is “*cq1 vq1*
|
|
@@ -2726,33 +2913,53 @@ rules applies.
|
|
| 2726 |
*cq12* stand for the “union” of *cq1* and *cq2*; that is, if *cq1* or
|
| 2727 |
*cq2* is `const`, then *cq12* is `const`. If `E2` is declared to be a
|
| 2728 |
`mutable` member, then the type of `E1.E2` is “*vq12* `T`”. If `E2` is
|
| 2729 |
not declared to be a `mutable` member, then the type of `E1.E2` is
|
| 2730 |
“*cq12* *vq12* `T`”.
|
| 2731 |
-
- If `E2` is
|
| 2732 |
-
|
| 2733 |
-
|
| 2734 |
-
|
| 2735 |
- If `E2` refers to a static member function, `E1.E2` is an lvalue.
|
| 2736 |
- Otherwise (when `E2` refers to a non-static member function),
|
| 2737 |
`E1.E2` is a prvalue. The expression can be used only as the
|
| 2738 |
left-hand operand of a member function call [[class.mfct]].
|
| 2739 |
\[*Note 5*: Any redundant set of parentheses surrounding the
|
| 2740 |
expression is ignored [[expr.prim.paren]]. — *end note*]
|
| 2741 |
- If `E2` is a nested type, the expression `E1.E2` is ill-formed.
|
| 2742 |
- If `E2` is a member enumerator and the type of `E2` is `T`, the
|
| 2743 |
-
expression `E1.E2` is a prvalue
|
|
|
|
| 2744 |
|
| 2745 |
-
If `E2` is a non-static
|
| 2746 |
-
|
| 2747 |
-
|
| 2748 |
-
|
| 2749 |
|
| 2750 |
[*Note 6*: The program is also ill-formed if the naming class is an
|
| 2751 |
ambiguous base of the class type of the object expression; see
|
| 2752 |
[[class.access.base]]. — *end note*]
|
| 2753 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2754 |
#### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 2755 |
|
| 2756 |
The value of a postfix `++` expression is the value of its operand.
|
| 2757 |
|
| 2758 |
[*Note 1*: The value obtained is a copy of the original
|
|
@@ -2775,11 +2982,11 @@ The result is a prvalue. The type of the result is the cv-unqualified
|
|
| 2775 |
version of the type of the operand. If the operand is a bit-field that
|
| 2776 |
cannot represent the incremented value, the resulting value of the
|
| 2777 |
bit-field is *implementation-defined*. See also [[expr.add]] and
|
| 2778 |
[[expr.ass]].
|
| 2779 |
|
| 2780 |
-
The operand of postfix `
|
| 2781 |
`++` operator.
|
| 2782 |
|
| 2783 |
[*Note 3*: For prefix increment and decrement, see
|
| 2784 |
[[expr.pre.incr]]. — *end note*]
|
| 2785 |
|
|
@@ -2805,13 +3012,14 @@ If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
|
| 2805 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 2806 |
unique `B` subobject of the `D` object pointed to by `v`, or a null
|
| 2807 |
pointer value if `v` is a null pointer value. Similarly, if `T` is
|
| 2808 |
“reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B` is a
|
| 2809 |
base class of `D`, the result is the unique `B` subobject of the `D`
|
| 2810 |
-
object referred to by `v`.[^14]
|
| 2811 |
-
|
| 2812 |
-
|
|
|
|
| 2813 |
|
| 2814 |
[*Example 1*:
|
| 2815 |
|
| 2816 |
``` cpp
|
| 2817 |
struct B { };
|
|
@@ -2831,11 +3039,11 @@ If `v` is a null pointer value, the result is a null pointer value.
|
|
| 2831 |
If `T` is “pointer to cv `void`”, then the result is a pointer to the
|
| 2832 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 2833 |
applied to see if the object pointed or referred to by `v` can be
|
| 2834 |
converted to the type pointed or referred to by `T`.
|
| 2835 |
|
| 2836 |
-
|
| 2837 |
check logically executes as follows:
|
| 2838 |
|
| 2839 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 2840 |
points (refers) to a public base class subobject of a `C` object, and
|
| 2841 |
if only one object of type `C` is derived from the subobject pointed
|
|
@@ -2889,40 +3097,44 @@ destruction. — *end note*]
|
|
| 2889 |
|
| 2890 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 2891 |
`std::type_info` [[type.info]] and dynamic type `const` `std::type_info`
|
| 2892 |
or `const` *name* where *name* is an *implementation-defined* class
|
| 2893 |
publicly derived from `std::type_info` which preserves the behavior
|
| 2894 |
-
described in [[type.info]].[^15]
|
| 2895 |
-
|
| 2896 |
-
|
| 2897 |
-
program is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2898 |
|
| 2899 |
When `typeid` is applied to a glvalue whose type is a polymorphic class
|
| 2900 |
type [[class.virtual]], the result refers to a `std::type_info` object
|
| 2901 |
representing the type of the most derived object [[intro.object]] (that
|
| 2902 |
is, the dynamic type) to which the glvalue refers. If the glvalue is
|
| 2903 |
-
obtained by applying the unary `*` operator to a pointer[^16]
|
| 2904 |
-
|
|
|
|
| 2905 |
expression throws an exception [[except.throw]] of a type that would
|
| 2906 |
match a handler of type `std::bad_typeid` exception [[bad.typeid]].
|
| 2907 |
|
| 2908 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 2909 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 2910 |
representing the static type of the expression. Lvalue-to-rvalue
|
| 2911 |
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
| 2912 |
[[conv.func]] conversions are not applied to the expression. If the
|
| 2913 |
expression is a prvalue, the temporary materialization conversion
|
| 2914 |
[[conv.rval]] is applied. The expression is an unevaluated operand
|
| 2915 |
-
[[
|
| 2916 |
|
| 2917 |
When `typeid` is applied to a *type-id*, the result refers to a
|
| 2918 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 2919 |
type of the *type-id* is a reference to a possibly cv-qualified type,
|
| 2920 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 2921 |
-
object representing the cv-unqualified referenced type.
|
| 2922 |
-
the *type-id* is a class type or a reference to a class type, the class
|
| 2923 |
-
shall be completely-defined.
|
| 2924 |
|
| 2925 |
[*Note 1*: The *type-id* cannot denote a function type with a
|
| 2926 |
*cv-qualifier-seq* or a *ref-qualifier* [[dcl.fct]]. — *end note*]
|
| 2927 |
|
| 2928 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
|
@@ -2942,12 +3154,14 @@ typeid(D) == typeid(d2); // yields true
|
|
| 2942 |
typeid(D) == typeid(const D&); // yields true
|
| 2943 |
```
|
| 2944 |
|
| 2945 |
— *end example*]
|
| 2946 |
|
| 2947 |
-
|
| 2948 |
-
|
|
|
|
|
|
|
| 2949 |
|
| 2950 |
[*Note 2*: Subclause [[class.cdtor]] describes the behavior of `typeid`
|
| 2951 |
applied to an object under construction or destruction. — *end note*]
|
| 2952 |
|
| 2953 |
#### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
|
@@ -2983,19 +3197,19 @@ B &br = d;
|
|
| 2983 |
static_cast<D&>(br); // produces lvalue denoting the original d object
|
| 2984 |
```
|
| 2985 |
|
| 2986 |
— *end example*]
|
| 2987 |
|
| 2988 |
-
An lvalue of type
|
| 2989 |
-
|
| 2990 |
-
|
| 2991 |
-
|
| 2992 |
-
|
| 2993 |
-
|
| 2994 |
-
|
| 2995 |
-
|
| 2996 |
-
|
| 2997 |
|
| 2998 |
An expression E can be explicitly converted to a type `T` if there is an
|
| 2999 |
implicit conversion sequence [[over.best.ics]] from E to `T`, if
|
| 3000 |
overload resolution for a direct-initialization [[dcl.init]] of an
|
| 3001 |
object or reference of type `T` from E would find at least one viable
|
|
@@ -3024,13 +3238,16 @@ direct-initialization defines the type of the expression as
|
|
| 3024 |
Otherwise, the `static_cast` shall perform one of the conversions listed
|
| 3025 |
below. No other conversion shall be performed explicitly using a
|
| 3026 |
`static_cast`.
|
| 3027 |
|
| 3028 |
Any expression can be explicitly converted to type cv `void`, in which
|
| 3029 |
-
case
|
| 3030 |
|
| 3031 |
-
[*Note 3*:
|
|
|
|
|
|
|
|
|
|
| 3032 |
[[class.temporary]], the destructor for that object is not executed
|
| 3033 |
until the usual time, and the value of the object is preserved for the
|
| 3034 |
purpose of executing the destructor. — *end note*]
|
| 3035 |
|
| 3036 |
The inverse of any standard conversion sequence [[conv]] not containing
|
|
@@ -3068,39 +3285,48 @@ explicitly converted to a floating-point type; the result is the same as
|
|
| 3068 |
that of converting from the original value to the floating-point type.
|
| 3069 |
|
| 3070 |
A value of integral or enumeration type can be explicitly converted to a
|
| 3071 |
complete enumeration type. If the enumeration type has a fixed
|
| 3072 |
underlying type, the value is first converted to that type by integral
|
| 3073 |
-
|
| 3074 |
-
|
| 3075 |
-
|
| 3076 |
-
|
| 3077 |
-
|
| 3078 |
-
|
| 3079 |
-
|
| 3080 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3081 |
|
| 3082 |
A prvalue of type “pointer to *cv1* `B`”, where `B` is a class type, can
|
| 3083 |
be converted to a prvalue of type “pointer to *cv2* `D`”, where `D` is a
|
| 3084 |
complete class derived [[class.derived]] from `B`, if *cv2* is the same
|
| 3085 |
cv-qualification as, or greater cv-qualification than, *cv1*. If `B` is
|
| 3086 |
a virtual base class of `D` or a base class of a virtual base class of
|
| 3087 |
`D`, or if no valid standard conversion from “pointer to `D`” to
|
| 3088 |
“pointer to `B`” exists [[conv.ptr]], the program is ill-formed. The
|
| 3089 |
null pointer value [[basic.compound]] is converted to the null pointer
|
| 3090 |
value of the destination type. If the prvalue of type “pointer to *cv1*
|
| 3091 |
-
`B`” points to a `B` that is actually a
|
| 3092 |
-
`D`, the resulting pointer points to the enclosing object
|
| 3093 |
-
Otherwise, the behavior is undefined.
|
| 3094 |
|
| 3095 |
A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
|
| 3096 |
converted to a prvalue of type “pointer to member of `B` of type *cv2*
|
| 3097 |
`T`”, where `D` is a complete class type and `B` is a base class
|
| 3098 |
[[class.derived]] of `D`, if *cv2* is the same cv-qualification as, or
|
| 3099 |
greater cv-qualification than, *cv1*.
|
| 3100 |
|
| 3101 |
-
[*Note
|
| 3102 |
pointer-to-member-function types) are never cv-qualified
|
| 3103 |
[[dcl.fct]]. — *end note*]
|
| 3104 |
|
| 3105 |
If no valid standard conversion from “pointer to member of `B` of type
|
| 3106 |
`T`” to “pointer to member of `D` of type `T`” exists [[conv.mem]], the
|
|
@@ -3109,11 +3335,11 @@ converted to the null member pointer value of the destination type. If
|
|
| 3109 |
class `B` contains the original member, or is a base or derived class of
|
| 3110 |
the class containing the original member, the resulting pointer to
|
| 3111 |
member points to the original member. Otherwise, the behavior is
|
| 3112 |
undefined.
|
| 3113 |
|
| 3114 |
-
[*Note
|
| 3115 |
dynamic type of the object with which indirection through the pointer to
|
| 3116 |
member is performed must contain the original member; see
|
| 3117 |
[[expr.mptr.oper]]. — *end note*]
|
| 3118 |
|
| 3119 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
|
@@ -3121,14 +3347,13 @@ prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
|
|
| 3121 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 3122 |
*cv1*. If the original pointer value represents the address `A` of a
|
| 3123 |
byte in memory and `A` does not satisfy the alignment requirement of
|
| 3124 |
`T`, then the resulting pointer value is unspecified. Otherwise, if the
|
| 3125 |
original pointer value points to an object *a*, and there is an object
|
| 3126 |
-
*b* of type `T`
|
| 3127 |
-
|
| 3128 |
-
|
| 3129 |
-
conversion.
|
| 3130 |
|
| 3131 |
[*Example 3*:
|
| 3132 |
|
| 3133 |
``` cpp
|
| 3134 |
T* p1 = new T;
|
|
@@ -3178,39 +3403,37 @@ A value of integral type or enumeration type can be explicitly converted
|
|
| 3178 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 3179 |
any such exists on the implementation) and back to the same pointer type
|
| 3180 |
will have its original value; mappings between pointers and integers are
|
| 3181 |
otherwise *implementation-defined*.
|
| 3182 |
|
| 3183 |
-
[*Note 4*: Except as described in [[basic.stc.dynamic.safety]], the
|
| 3184 |
-
result of such a conversion will not be a safely-derived pointer
|
| 3185 |
-
value. — *end note*]
|
| 3186 |
-
|
| 3187 |
A function pointer can be explicitly converted to a function pointer of
|
| 3188 |
a different type.
|
| 3189 |
|
| 3190 |
-
[*Note
|
| 3191 |
function type [[dcl.fct]] that is not the same as the type used in the
|
| 3192 |
definition of the function is undefined [[expr.call]]. — *end note*]
|
| 3193 |
|
| 3194 |
Except that converting a prvalue of type “pointer to `T1`” to the type
|
| 3195 |
“pointer to `T2`” (where `T1` and `T2` are function types) and back to
|
| 3196 |
its original type yields the original pointer value, the result of such
|
| 3197 |
a pointer conversion is unspecified.
|
| 3198 |
|
| 3199 |
-
[*Note
|
| 3200 |
conversions. — *end note*]
|
| 3201 |
|
| 3202 |
An object pointer can be explicitly converted to an object pointer of a
|
| 3203 |
-
different type.[^17]
|
| 3204 |
-
|
|
|
|
|
|
|
| 3205 |
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 3206 |
|
| 3207 |
-
[*Note
|
| 3208 |
-
“pointer to `T2`” (where `
|
| 3209 |
-
alignment requirements of `T2` are no stricter
|
| 3210 |
-
back to its original type yields the original
|
| 3211 |
-
value. — *end note*]
|
| 3212 |
|
| 3213 |
Converting a function pointer to an object pointer type or vice versa is
|
| 3214 |
conditionally-supported. The meaning of such a conversion is
|
| 3215 |
*implementation-defined*, except that if an implementation supports
|
| 3216 |
conversions in both directions, converting a prvalue of one type to the
|
|
@@ -3218,21 +3441,23 @@ other type and back, possibly with different cv-qualification, shall
|
|
| 3218 |
yield the original pointer value.
|
| 3219 |
|
| 3220 |
The null pointer value [[basic.compound]] is converted to the null
|
| 3221 |
pointer value of the destination type.
|
| 3222 |
|
| 3223 |
-
[*Note
|
| 3224 |
converted to a pointer type, and a null pointer constant of integral
|
| 3225 |
type is not necessarily converted to a null pointer
|
| 3226 |
value. — *end note*]
|
| 3227 |
|
| 3228 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 3229 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 3230 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 3231 |
-
object types.[^18]
|
| 3232 |
-
|
| 3233 |
-
|
|
|
|
|
|
|
| 3234 |
|
| 3235 |
- Converting a prvalue of type “pointer to member function” to a
|
| 3236 |
different pointer-to-member-function type and back to its original
|
| 3237 |
type yields the original pointer-to-member value.
|
| 3238 |
- Converting a prvalue of type “pointer to data member of `X` of type
|
|
@@ -3260,17 +3485,17 @@ otherwise, the result is a prvalue and the lvalue-to-rvalue
|
|
| 3260 |
Conversions that can be performed explicitly using `const_cast` are
|
| 3261 |
listed below. No other conversion shall be performed explicitly using
|
| 3262 |
`const_cast`.
|
| 3263 |
|
| 3264 |
[*Note 1*: Subject to the restrictions in this subclause, an expression
|
| 3265 |
-
|
| 3266 |
operator. — *end note*]
|
| 3267 |
|
| 3268 |
For two similar types `T1` and `T2` [[conv.qual]], a prvalue of type
|
| 3269 |
`T1` may be explicitly converted to the type `T2` using a `const_cast`
|
| 3270 |
-
if, considering the
|
| 3271 |
-
same as P²ᵢ for all i. The result of a `const_cast` refers to the
|
| 3272 |
original entity.
|
| 3273 |
|
| 3274 |
[*Example 1*:
|
| 3275 |
|
| 3276 |
``` cpp
|
|
@@ -3303,46 +3528,56 @@ materialization conversion [[conv.rval]] otherwise.
|
|
| 3303 |
A null pointer value [[basic.compound]] is converted to the null pointer
|
| 3304 |
value of the destination type. The null member pointer value
|
| 3305 |
[[conv.mem]] is converted to the null member pointer value of the
|
| 3306 |
destination type.
|
| 3307 |
|
| 3308 |
-
[*Note 2*:
|
| 3309 |
-
|
| 3310 |
-
|
| 3311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3312 |
|
| 3313 |
A conversion from a type `T1` to a type `T2` *casts away constness* if
|
| 3314 |
-
`T1` and `T2` are different, there is a
|
| 3315 |
-
of `T1` yielding *n* such that `T2` has a
|
|
|
|
| 3316 |
|
| 3317 |
and there is no qualification conversion that converts `T1` to
|
| 3318 |
|
| 3319 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 3320 |
lvalue reference cast or casting from an expression of type `T1` to an
|
| 3321 |
xvalue of type `T2` using an rvalue reference cast casts away constness
|
| 3322 |
if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
|
| 3323 |
to `T2`” casts away constness.
|
| 3324 |
|
| 3325 |
[*Note 3*: Some conversions which involve only changes in
|
| 3326 |
-
cv-qualification cannot be done using `const_cast
|
| 3327 |
conversions between pointers to functions are not covered because such
|
| 3328 |
conversions lead to values whose use causes undefined behavior. For the
|
| 3329 |
same reasons, conversions between pointers to member functions, and in
|
| 3330 |
particular, the conversion from a pointer to a const member function to
|
| 3331 |
a pointer to a non-const member function, are not
|
| 3332 |
covered. — *end note*]
|
| 3333 |
|
| 3334 |
### Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 3335 |
|
|
|
|
|
|
|
| 3336 |
Expressions with unary operators group right-to-left.
|
| 3337 |
|
| 3338 |
``` bnf
|
|
|
|
|
|
|
| 3339 |
unary-expression:
|
| 3340 |
postfix-expression
|
| 3341 |
unary-operator cast-expression
|
| 3342 |
'++' cast-expression
|
| 3343 |
-
'-
|
| 3344 |
await-expression
|
| 3345 |
sizeof unary-expression
|
| 3346 |
sizeof '(' type-id ')'
|
| 3347 |
sizeof '...' '(' identifier ')'
|
| 3348 |
alignof '(' type-id ')'
|
|
@@ -3350,41 +3585,43 @@ unary-expression:
|
|
| 3350 |
new-expression
|
| 3351 |
delete-expression
|
| 3352 |
```
|
| 3353 |
|
| 3354 |
``` bnf
|
|
|
|
|
|
|
| 3355 |
unary-operator: one of
|
| 3356 |
'* & + - ! ~'
|
| 3357 |
```
|
| 3358 |
|
| 3359 |
#### Unary operators <a id="expr.unary.op">[[expr.unary.op]]</a>
|
| 3360 |
|
| 3361 |
-
The unary `*` operator performs *indirection*
|
| 3362 |
-
|
| 3363 |
-
|
| 3364 |
-
function to which the
|
| 3365 |
-
is “pointer to `T`”, the type of the result is “`T`”.
|
| 3366 |
|
| 3367 |
[*Note 1*: Indirection through a pointer to an incomplete type (other
|
| 3368 |
than cv `void`) is valid. The lvalue thus obtained can be used in
|
| 3369 |
limited ways (to initialize a reference, for example); this lvalue must
|
| 3370 |
not be converted to a prvalue, see [[conv.lval]]. — *end note*]
|
| 3371 |
|
| 3372 |
-
|
| 3373 |
|
| 3374 |
-
The
|
|
|
|
| 3375 |
|
| 3376 |
- If the operand is a *qualified-id* naming a non-static or variant
|
| 3377 |
-
member `m` of some class `C`
|
| 3378 |
-
“pointer to member of class `C` of type
|
| 3379 |
-
|
| 3380 |
-
- Otherwise,
|
| 3381 |
-
|
| 3382 |
-
|
| 3383 |
-
\[*Note 2*: In particular, taking
|
| 3384 |
-
|
| 3385 |
-
|
| 3386 |
|
| 3387 |
[*Example 1*:
|
| 3388 |
|
| 3389 |
``` cpp
|
| 3390 |
struct A { int i; };
|
|
@@ -3417,44 +3654,53 @@ the *unqualified-id*’s class. — *end note*]
|
|
| 3417 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 3418 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 3419 |
the built-in meaning or the operator function is called. The operand of
|
| 3420 |
`&` shall not be a bit-field.
|
| 3421 |
|
| 3422 |
-
[*Note 5*: The address of an
|
| 3423 |
-
|
| 3424 |
-
|
| 3425 |
-
|
| 3426 |
-
|
| 3427 |
-
|
| 3428 |
-
function”. — *end note*]
|
| 3429 |
|
| 3430 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 3431 |
enumeration, or pointer type and the result is the value of the
|
| 3432 |
argument. Integral promotion is performed on integral or enumeration
|
| 3433 |
operands. The type of the result is the type of the promoted operand.
|
| 3434 |
|
| 3435 |
The operand of the unary `-` operator shall have arithmetic or unscoped
|
| 3436 |
-
enumeration type and the result is the
|
| 3437 |
promotion is performed on integral or enumeration operands. The negative
|
| 3438 |
of an unsigned quantity is computed by subtracting its value from 2ⁿ,
|
| 3439 |
where n is the number of bits in the promoted operand. The type of the
|
| 3440 |
result is the type of the promoted operand.
|
| 3441 |
|
|
|
|
|
|
|
|
|
|
| 3442 |
The operand of the logical negation operator `!` is contextually
|
| 3443 |
converted to `bool` [[conv]]; its value is `true` if the converted
|
| 3444 |
operand is `false` and `false` otherwise. The type of the result is
|
| 3445 |
`bool`.
|
| 3446 |
|
| 3447 |
-
The operand of `~` shall have integral or unscoped
|
| 3448 |
-
|
| 3449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3450 |
There is an ambiguity in the grammar when `~` is followed by a
|
| 3451 |
*type-name* or *decltype-specifier*. The ambiguity is resolved by
|
| 3452 |
-
treating `~` as the
|
| 3453 |
-
|
| 3454 |
|
| 3455 |
-
[*Note
|
| 3456 |
the `.`, `->`, or `::` tokens, a `~` followed by a *type-name* or
|
| 3457 |
*decltype-specifier* in a member access expression or *qualified-id* is
|
| 3458 |
unambiguously parsed as a destructor name. — *end note*]
|
| 3459 |
|
| 3460 |
#### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
|
@@ -3468,12 +3714,12 @@ operand; it is an lvalue, and it is a bit-field if the operand is a
|
|
| 3468 |
bit-field. The expression `++x` is equivalent to `x+=1`.
|
| 3469 |
|
| 3470 |
[*Note 1*: See the discussions of addition [[expr.add]] and assignment
|
| 3471 |
operators [[expr.ass]] for information on conversions. — *end note*]
|
| 3472 |
|
| 3473 |
-
The operand of prefix `
|
| 3474 |
-
`1`. The requirements on the operand of prefix `
|
| 3475 |
of its result are otherwise the same as those of prefix `++`.
|
| 3476 |
|
| 3477 |
[*Note 2*: For postfix increment and decrement, see
|
| 3478 |
[[expr.post.incr]]. — *end note*]
|
| 3479 |
|
|
@@ -3489,30 +3735,30 @@ await-expression:
|
|
| 3489 |
```
|
| 3490 |
|
| 3491 |
An *await-expression* shall appear only in a potentially-evaluated
|
| 3492 |
expression within the *compound-statement* of a *function-body* outside
|
| 3493 |
of a *handler* [[except.pre]]. In a *declaration-statement* or in the
|
| 3494 |
-
*simple-declaration* (if any) of
|
| 3495 |
*await-expression* shall appear only in an *initializer* of that
|
| 3496 |
*declaration-statement* or *simple-declaration*. An *await-expression*
|
| 3497 |
shall not appear in a default argument [[dcl.fct.default]]. An
|
| 3498 |
-
*await-expression* shall not appear in the initializer of a block
|
| 3499 |
variable with static or thread storage duration. A context within a
|
| 3500 |
function where an *await-expression* can appear is called a *suspension
|
| 3501 |
context* of the function.
|
| 3502 |
|
| 3503 |
Evaluation of an *await-expression* involves the following auxiliary
|
| 3504 |
types, expressions, and objects:
|
| 3505 |
|
| 3506 |
- *p* is an lvalue naming the promise object [[dcl.fct.def.coroutine]]
|
| 3507 |
of the enclosing coroutine and `P` is the type of that object.
|
| 3508 |
-
-
|
| 3509 |
-
|
| 3510 |
-
|
| 3511 |
-
the
|
| 3512 |
-
|
| 3513 |
-
|
| 3514 |
*p*`.await_transform(`*cast-expression*`)`; otherwise, *a* is the
|
| 3515 |
*cast-expression*.
|
| 3516 |
- *o* is determined by enumerating the applicable `operator co_await`
|
| 3517 |
functions for an argument *a* [[over.match.oper]], and choosing the
|
| 3518 |
best one through overload resolution [[over.match]]. If overload
|
|
@@ -3540,25 +3786,31 @@ and the *await-ready* expression, then:
|
|
| 3540 |
- If the result of *await-ready* is `false`, the coroutine is considered
|
| 3541 |
suspended. Then:
|
| 3542 |
- If the type of *await-suspend* is `std::coroutine_handle<Z>`,
|
| 3543 |
*await-suspend*`.resume()` is evaluated. \[*Note 1*: This resumes
|
| 3544 |
the coroutine referred to by the result of *await-suspend*. Any
|
| 3545 |
-
number of coroutines
|
| 3546 |
eventually returning control flow to the current coroutine caller or
|
| 3547 |
resumer [[dcl.fct.def.coroutine]]. — *end note*]
|
| 3548 |
- Otherwise, if the type of *await-suspend* is `bool`, *await-suspend*
|
| 3549 |
is evaluated, and the coroutine is resumed if the result is `false`.
|
| 3550 |
- Otherwise, *await-suspend* is evaluated.
|
| 3551 |
|
| 3552 |
If the evaluation of *await-suspend* exits via an exception, the
|
| 3553 |
exception is caught, the coroutine is resumed, and the exception is
|
| 3554 |
-
immediately
|
| 3555 |
-
|
| 3556 |
-
|
|
|
|
|
|
|
| 3557 |
- If the result of *await-ready* is `true`, or when the coroutine is
|
| 3558 |
-
resumed
|
| 3559 |
-
the
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3560 |
|
| 3561 |
[*Example 1*:
|
| 3562 |
|
| 3563 |
``` cpp
|
| 3564 |
template <typename T>
|
|
@@ -3585,11 +3837,11 @@ auto operator co_await(std::chrono::duration<Rep, Period> d) {
|
|
| 3585 |
using namespace std::chrono;
|
| 3586 |
|
| 3587 |
my_future<int> h();
|
| 3588 |
|
| 3589 |
my_future<void> g() {
|
| 3590 |
-
std::cout << "just about go to sleep...\n";
|
| 3591 |
co_await 10ms;
|
| 3592 |
std::cout << "resumed\n";
|
| 3593 |
co_await h();
|
| 3594 |
}
|
| 3595 |
|
|
@@ -3602,33 +3854,40 @@ int a[] = { co_await h() }; // error: await-expression outside of function s
|
|
| 3602 |
#### Sizeof <a id="expr.sizeof">[[expr.sizeof]]</a>
|
| 3603 |
|
| 3604 |
The `sizeof` operator yields the number of bytes occupied by a
|
| 3605 |
non-potentially-overlapping object of the type of its operand. The
|
| 3606 |
operand is either an expression, which is an unevaluated operand
|
| 3607 |
-
[[
|
| 3608 |
-
not be applied to an expression that has function or
|
| 3609 |
-
the parenthesized name of such types, or to a
|
| 3610 |
-
bit-field. The result of `sizeof` applied to
|
| 3611 |
-
types is `1`. The result of `sizeof` applied
|
| 3612 |
-
type [[basic.fundamental]] is
|
|
|
|
| 3613 |
|
| 3614 |
-
[*Note 1*:
|
|
|
|
|
|
|
| 3615 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 3616 |
-
implementation-defined.[^21]
|
|
|
|
|
|
|
| 3617 |
|
| 3618 |
[*Note 2*: See [[intro.memory]] for the definition of byte and
|
| 3619 |
-
[[
|
| 3620 |
representation. — *end note*]
|
| 3621 |
|
| 3622 |
When applied to a reference type, the result is the size of the
|
| 3623 |
referenced type. When applied to a class, the result is the number of
|
| 3624 |
bytes in an object of that class including any padding required for
|
| 3625 |
placing objects of that type in an array. The result of applying
|
| 3626 |
`sizeof` to a potentially-overlapping subobject is the size of the type,
|
| 3627 |
-
not the size of the subobject.
|
| 3628 |
-
|
| 3629 |
-
|
|
|
|
|
|
|
| 3630 |
|
| 3631 |
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 3632 |
function-to-pointer [[conv.func]] standard conversions are not applied
|
| 3633 |
to the operand of `sizeof`. If the operand is a prvalue, the temporary
|
| 3634 |
materialization conversion [[conv.rval]] is applied.
|
|
@@ -3652,11 +3911,11 @@ struct count {
|
|
| 3652 |
The result of `sizeof` and `sizeof...` is a prvalue of type
|
| 3653 |
`std::size_t`.
|
| 3654 |
|
| 3655 |
[*Note 3*: A `sizeof` expression is an integral constant expression
|
| 3656 |
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 3657 |
-
`<cstddef>`
|
| 3658 |
|
| 3659 |
#### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
| 3660 |
|
| 3661 |
An `alignof` expression yields the alignment requirement of its operand
|
| 3662 |
type. The operand shall be a *type-id* representing a complete object
|
|
@@ -3664,21 +3923,21 @@ type, or an array thereof, or a reference to one of those types.
|
|
| 3664 |
|
| 3665 |
The result is a prvalue of type `std::size_t`.
|
| 3666 |
|
| 3667 |
[*Note 1*: An `alignof` expression is an integral constant expression
|
| 3668 |
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 3669 |
-
`<cstddef>`
|
| 3670 |
|
| 3671 |
When `alignof` is applied to a reference type, the result is the
|
| 3672 |
alignment of the referenced type. When `alignof` is applied to an array
|
| 3673 |
type, the result is the alignment of the element type.
|
| 3674 |
|
| 3675 |
#### `noexcept` operator <a id="expr.unary.noexcept">[[expr.unary.noexcept]]</a>
|
| 3676 |
|
| 3677 |
The `noexcept` operator determines whether the evaluation of its
|
| 3678 |
-
operand, which is an unevaluated operand [[
|
| 3679 |
-
exception [[except.throw]].
|
| 3680 |
|
| 3681 |
``` bnf
|
| 3682 |
noexcept-expression:
|
| 3683 |
noexcept '(' expression ')'
|
| 3684 |
```
|
|
@@ -3694,17 +3953,17 @@ is potentially-throwing [[except.spec]].
|
|
| 3694 |
#### New <a id="expr.new">[[expr.new]]</a>
|
| 3695 |
|
| 3696 |
The *new-expression* attempts to create an object of the *type-id*
|
| 3697 |
[[dcl.name]] or *new-type-id* to which it is applied. The type of that
|
| 3698 |
object is the *allocated type*. This type shall be a complete object
|
| 3699 |
-
type, but not an abstract class type
|
| 3700 |
-
[[
|
| 3701 |
|
| 3702 |
[*Note 1*: Because references are not objects, references cannot be
|
| 3703 |
created by *new-expression*s. — *end note*]
|
| 3704 |
|
| 3705 |
-
[*Note 2*: The *type-id*
|
| 3706 |
object created by the *new-expression* has a cv-qualified
|
| 3707 |
type. — *end note*]
|
| 3708 |
|
| 3709 |
``` bnf
|
| 3710 |
new-expression:
|
|
@@ -3810,27 +4069,10 @@ returning `int`).
|
|
| 3810 |
|
| 3811 |
— *end example*]
|
| 3812 |
|
| 3813 |
— *end note*]
|
| 3814 |
|
| 3815 |
-
Objects created by a *new-expression* have dynamic storage duration
|
| 3816 |
-
[[basic.stc.dynamic]].
|
| 3817 |
-
|
| 3818 |
-
[*Note 5*: The lifetime of such an object is not necessarily
|
| 3819 |
-
restricted to the scope in which it is created. — *end note*]
|
| 3820 |
-
|
| 3821 |
-
When the allocated object is not an array, the result of the
|
| 3822 |
-
*new-expression* is a pointer to the object created.
|
| 3823 |
-
|
| 3824 |
-
When the allocated object is an array (that is, the
|
| 3825 |
-
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 3826 |
-
denotes an array type), the *new-expression* yields a pointer to the
|
| 3827 |
-
initial element (if any) of the array.
|
| 3828 |
-
|
| 3829 |
-
[*Note 6*: Both `new int` and `new int[10]` have type `int*` and the
|
| 3830 |
-
type of `new int[i][10]` is `int (*)[10]` — *end note*]
|
| 3831 |
-
|
| 3832 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 3833 |
the associated array type.
|
| 3834 |
|
| 3835 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 3836 |
converted constant expression [[expr.const]] of type `std::size_t` and
|
|
@@ -3842,12 +4084,12 @@ well-formed (because `n` is the *expression* of a
|
|
| 3842 |
`n` is not a constant expression). — *end example*]
|
| 3843 |
|
| 3844 |
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 3845 |
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 3846 |
object is an array with `n` elements, where `n` is determined from the
|
| 3847 |
-
number of initial elements supplied in the *new-initializer*
|
| 3848 |
-
[[dcl.init.aggr]], [[dcl.init.string]]
|
| 3849 |
|
| 3850 |
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 3851 |
implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
| 3852 |
|
| 3853 |
- the expression is of non-class type and its value before converting to
|
|
@@ -3862,12 +4104,12 @@ implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
|
| 3862 |
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 3863 |
number of elements to initialize.
|
| 3864 |
|
| 3865 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 3866 |
|
| 3867 |
-
- if the *expression* is a core constant
|
| 3868 |
-
ill-formed;
|
| 3869 |
- otherwise, an allocation function is not called; instead
|
| 3870 |
- if the allocation function that would have been called has a
|
| 3871 |
non-throwing exception specification [[except.spec]], the value of
|
| 3872 |
the *new-expression* is the null pointer value of the required
|
| 3873 |
result type;
|
|
@@ -3876,10 +4118,26 @@ If the *expression* is erroneous after converting to `std::size_t`:
|
|
| 3876 |
`std::bad_array_new_length` [[new.badlength]].
|
| 3877 |
|
| 3878 |
When the value of the *expression* is zero, the allocation function is
|
| 3879 |
called to allocate an array with no elements.
|
| 3880 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3881 |
A *new-expression* may obtain storage for the object by calling an
|
| 3882 |
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 3883 |
*new-expression* terminates by throwing an exception, it may release
|
| 3884 |
storage by calling a deallocation function
|
| 3885 |
[[basic.stc.dynamic.deallocation]]. If the allocated type is a non-array
|
|
@@ -3887,28 +4145,28 @@ type, the allocation function’s name is `operator new` and the
|
|
| 3887 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 3888 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 3889 |
the deallocation function’s name is `operator delete[]`.
|
| 3890 |
|
| 3891 |
[*Note 7*: An implementation is required to provide default definitions
|
| 3892 |
-
for the global allocation functions
|
| 3893 |
-
[[new.delete.single]], [[new.delete.array]]
|
| 3894 |
-
alternative definitions of these functions
|
| 3895 |
-
and/or class-specific versions [[class.free]].
|
| 3896 |
-
deallocation functions that
|
| 3897 |
-
include functions that do not perform allocation or
|
| 3898 |
-
example, see [[new.delete.placement]]. — *end note*]
|
| 3899 |
|
| 3900 |
-
If the *new-expression*
|
| 3901 |
-
|
| 3902 |
-
|
| 3903 |
-
|
| 3904 |
-
|
| 3905 |
-
|
| 3906 |
|
| 3907 |
An implementation is allowed to omit a call to a replaceable global
|
| 3908 |
-
allocation function
|
| 3909 |
-
|
| 3910 |
provided by extending the allocation of another *new-expression*.
|
| 3911 |
|
| 3912 |
During an evaluation of a constant expression, a call to an allocation
|
| 3913 |
function is always omitted.
|
| 3914 |
|
|
@@ -4039,12 +4297,12 @@ from one invocation of `new` to another.
|
|
| 4039 |
|
| 4040 |
— *end example*]
|
| 4041 |
|
| 4042 |
[*Note 10*: Unless an allocation function has a non-throwing exception
|
| 4043 |
specification [[except.spec]], it indicates failure to allocate storage
|
| 4044 |
-
by throwing a `std::bad_alloc` exception
|
| 4045 |
-
[[basic.stc.dynamic.allocation]], [[except]], [[bad.alloc]]
|
| 4046 |
a non-null pointer otherwise. If the allocation function has a
|
| 4047 |
non-throwing exception specification, it returns null to indicate
|
| 4048 |
failure to allocate storage and a non-null pointer
|
| 4049 |
otherwise. — *end note*]
|
| 4050 |
|
|
@@ -4075,34 +4333,33 @@ evaluations of expressions in the *new-initializer*. Initialization of
|
|
| 4075 |
the allocated object is sequenced before the value computation of the
|
| 4076 |
*new-expression*.
|
| 4077 |
|
| 4078 |
If the *new-expression* creates an object or an array of objects of
|
| 4079 |
class type, access and ambiguity control are done for the allocation
|
| 4080 |
-
function, the deallocation function [[
|
| 4081 |
-
[[class.ctor]] selected for the initialization (if
|
| 4082 |
-
*new-expression* creates an array of objects of class type,
|
| 4083 |
-
destructor is potentially invoked [[class.dtor]].
|
| 4084 |
|
| 4085 |
-
If any part of the object initialization described above[^24]
|
| 4086 |
-
|
| 4087 |
-
|
| 4088 |
-
the
|
| 4089 |
-
|
| 4090 |
-
|
| 4091 |
-
|
|
|
|
| 4092 |
|
| 4093 |
[*Note 13*: This is appropriate when the called allocation function
|
| 4094 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 4095 |
leak. — *end note*]
|
| 4096 |
|
| 4097 |
-
If the *new-expression*
|
| 4098 |
-
|
| 4099 |
-
|
| 4100 |
-
|
| 4101 |
-
|
| 4102 |
-
not a class type or array thereof, the deallocation function’s name is
|
| 4103 |
-
looked up in the global scope.
|
| 4104 |
|
| 4105 |
A declaration of a placement deallocation function matches the
|
| 4106 |
declaration of a placement allocation function if it has the same number
|
| 4107 |
of parameters and, after parameter transformations [[dcl.fct]], all
|
| 4108 |
parameter types except the first are identical. If the lookup finds a
|
|
@@ -4153,26 +4410,32 @@ delete-expression:
|
|
| 4153 |
```
|
| 4154 |
|
| 4155 |
The first alternative is a *single-object delete expression*, and the
|
| 4156 |
second is an *array delete expression*. Whenever the `delete` keyword is
|
| 4157 |
immediately followed by empty square brackets, it shall be interpreted
|
| 4158 |
-
as the second alternative.[^25]
|
| 4159 |
-
|
| 4160 |
-
|
| 4161 |
-
type
|
|
|
|
|
|
|
|
|
|
| 4162 |
|
| 4163 |
If the operand has a class type, the operand is converted to a pointer
|
| 4164 |
type by calling the above-mentioned conversion function, and the
|
| 4165 |
converted operand is used in place of the original operand for the
|
| 4166 |
remainder of this subclause. In a single-object delete expression, the
|
| 4167 |
value of the operand of `delete` may be a null pointer value, a pointer
|
| 4168 |
-
|
| 4169 |
-
pointer to a subobject
|
| 4170 |
-
|
| 4171 |
-
|
| 4172 |
-
|
| 4173 |
-
|
|
|
|
|
|
|
|
|
|
| 4174 |
|
| 4175 |
[*Note 1*: This means that the syntax of the *delete-expression* must
|
| 4176 |
match the type of the object allocated by `new`, not the syntax of the
|
| 4177 |
*new-expression*. — *end note*]
|
| 4178 |
|
|
@@ -4180,17 +4443,17 @@ match the type of the object allocated by `new`, not the syntax of the
|
|
| 4180 |
*delete-expression*; it is not necessary to cast away the constness
|
| 4181 |
[[expr.const.cast]] of the pointer expression before it is used as the
|
| 4182 |
operand of the *delete-expression*. — *end note*]
|
| 4183 |
|
| 4184 |
In a single-object delete expression, if the static type of the object
|
| 4185 |
-
to be deleted is
|
| 4186 |
-
deallocation function (see below) is not a destroying operator
|
| 4187 |
-
the static type shall be a base class of the dynamic type of the
|
| 4188 |
-
to be deleted and the static type shall have a virtual destructor
|
| 4189 |
-
behavior is undefined. In an array delete expression, if the
|
| 4190 |
-
type of the object to be deleted
|
| 4191 |
-
behavior is undefined.
|
| 4192 |
|
| 4193 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 4194 |
exactly once.
|
| 4195 |
|
| 4196 |
If the object being deleted has incomplete class type at the point of
|
|
@@ -4231,61 +4494,78 @@ exception. — *end note*]
|
|
| 4231 |
|
| 4232 |
If the value of the operand of the *delete-expression* is a null pointer
|
| 4233 |
value, it is unspecified whether a deallocation function will be called
|
| 4234 |
as described above.
|
| 4235 |
|
| 4236 |
-
|
| 4237 |
-
|
| 4238 |
-
|
| 4239 |
-
[[new.delete.array]]. A C++ program can provide alternative definitions
|
| 4240 |
-
of these functions [[replacement.functions]], and/or class-specific
|
| 4241 |
-
versions [[class.free]]. — *end note*]
|
| 4242 |
|
| 4243 |
-
|
| 4244 |
-
|
| 4245 |
-
|
| 4246 |
-
|
| 4247 |
-
|
| 4248 |
-
global scope.
|
| 4249 |
|
| 4250 |
-
If
|
| 4251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4252 |
|
| 4253 |
- If any of the deallocation functions is a destroying operator delete,
|
| 4254 |
all deallocation functions that are not destroying operator deletes
|
| 4255 |
are eliminated from further consideration.
|
| 4256 |
- If the type has new-extended alignment, a function with a parameter of
|
| 4257 |
type `std::align_val_t` is preferred; otherwise a function without
|
| 4258 |
such a parameter is preferred. If any preferred functions are found,
|
| 4259 |
all non-preferred functions are eliminated from further consideration.
|
| 4260 |
- If exactly one function remains, that function is selected and the
|
| 4261 |
selection process terminates.
|
| 4262 |
-
- If the deallocation functions
|
| 4263 |
-
parameter of type `std::size_t` is selected.
|
| 4264 |
- If the type is complete and if, for an array delete expression only,
|
| 4265 |
the operand is a pointer to a class type with a non-trivial destructor
|
| 4266 |
-
or a (possibly
|
| 4267 |
parameter of type `std::size_t` is selected.
|
| 4268 |
- Otherwise, it is unspecified whether a deallocation function with a
|
| 4269 |
parameter of type `std::size_t` is selected.
|
| 4270 |
|
| 4271 |
For a single-object delete expression, the deleted object is the object
|
| 4272 |
-
|
| 4273 |
-
destructor, and
|
| 4274 |
|
| 4275 |
-
[*Note
|
| 4276 |
delete and the deleted object is not the most derived object in the
|
| 4277 |
former case, the behavior is undefined, as stated above. — *end note*]
|
| 4278 |
|
| 4279 |
For an array delete expression, the deleted object is the array object.
|
| 4280 |
When a *delete-expression* is executed, the selected deallocation
|
| 4281 |
function shall be called with the address of the deleted object in a
|
| 4282 |
single-object delete expression, or the address of the deleted object
|
| 4283 |
suitably adjusted for the array allocation overhead [[expr.new]] in an
|
| 4284 |
array delete expression, as its first argument.
|
| 4285 |
|
| 4286 |
-
[*Note
|
| 4287 |
ignored when forming this argument. — *end note*]
|
| 4288 |
|
| 4289 |
If a destroying operator delete is used, an unspecified value is passed
|
| 4290 |
as the argument corresponding to the parameter of type
|
| 4291 |
`std::destroying_delete_t`. If a deallocation function with a parameter
|
|
@@ -4294,19 +4574,18 @@ deleted object is passed as the corresponding argument. If a
|
|
| 4294 |
deallocation function with a parameter of type `std::size_t` is used,
|
| 4295 |
the size of the deleted object in a single-object delete expression, or
|
| 4296 |
of the array plus allocation overhead in an array delete expression, is
|
| 4297 |
passed as the corresponding argument.
|
| 4298 |
|
| 4299 |
-
[*Note
|
| 4300 |
function, and either the first argument was not the result of a prior
|
| 4301 |
call to a replaceable allocation function or the second or third
|
| 4302 |
argument was not the corresponding argument in said call, the behavior
|
| 4303 |
-
is undefined
|
| 4304 |
-
[[new.delete.array]]). — *end note*]
|
| 4305 |
|
| 4306 |
Access and ambiguity control are done for both the deallocation function
|
| 4307 |
-
and the destructor
|
| 4308 |
|
| 4309 |
### Explicit type conversion (cast notation) <a id="expr.cast">[[expr.cast]]</a>
|
| 4310 |
|
| 4311 |
The result of the expression `(T)` *cast-expression* is of type `T`. The
|
| 4312 |
result is an lvalue if `T` is an lvalue reference type or an rvalue
|
|
@@ -4410,13 +4689,14 @@ type “pointer to member of `T`” to its first operand, which shall be of
|
|
| 4410 |
type “pointer to `U`” where `U` is either `T` or a class of which `T` is
|
| 4411 |
an unambiguous and accessible base class. The expression `E1->*E2` is
|
| 4412 |
converted into the equivalent form `(*(E1)).*E2`.
|
| 4413 |
|
| 4414 |
Abbreviating *pm-expression*`.*`*cast-expression* as `E1.*E2`, `E1` is
|
| 4415 |
-
called the *object expression*. If the
|
| 4416 |
-
|
| 4417 |
-
|
|
|
|
| 4418 |
|
| 4419 |
The restrictions on cv-qualification, and the manner in which the
|
| 4420 |
cv-qualifiers of the operands are combined to produce the cv-qualifiers
|
| 4421 |
of the result, are the same as the rules for `E1.E2` given in
|
| 4422 |
[[expr.ref]].
|
|
@@ -4488,13 +4768,15 @@ The binary `*` operator indicates multiplication.
|
|
| 4488 |
|
| 4489 |
The binary `/` operator yields the quotient, and the binary `%` operator
|
| 4490 |
yields the remainder from the division of the first expression by the
|
| 4491 |
second. If the second operand of `/` or `%` is zero the behavior is
|
| 4492 |
undefined. For integral operands the `/` operator yields the algebraic
|
| 4493 |
-
quotient with any fractional part discarded;[^28]
|
| 4494 |
-
|
| 4495 |
-
|
|
|
|
|
|
|
| 4496 |
|
| 4497 |
### Additive operators <a id="expr.add">[[expr.add]]</a>
|
| 4498 |
|
| 4499 |
The additive operators `+` and `-` group left-to-right. The usual
|
| 4500 |
arithmetic conversions [[expr.arith.conv]] are performed for operands of
|
|
@@ -4535,30 +4817,41 @@ from an expression `P` of pointer type, the result has the type of `P`.
|
|
| 4535 |
(possibly-hypothetical) array element i + j of `x` if 0 ≤ i + j ≤ n
|
| 4536 |
and the expression `P - J` points to the (possibly-hypothetical) array
|
| 4537 |
element i - j of `x` if 0 ≤ i - j ≤ n.
|
| 4538 |
- Otherwise, the behavior is undefined.
|
| 4539 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4540 |
When two pointer expressions `P` and `Q` are subtracted, the type of the
|
| 4541 |
result is an *implementation-defined* signed integral type; this type
|
| 4542 |
shall be the same type that is defined as `std::ptrdiff_t` in the
|
| 4543 |
`<cstddef>` header [[support.types.layout]].
|
| 4544 |
|
| 4545 |
- If `P` and `Q` both evaluate to null pointer values, the result is 0.
|
| 4546 |
- Otherwise, if `P` and `Q` point to, respectively, array elements i and
|
| 4547 |
j of the same array object `x`, the expression `P - Q` has the value
|
| 4548 |
i - j.
|
| 4549 |
-
- Otherwise, the behavior is undefined. \[*Note
|
| 4550 |
is not in the range of representable values of type `std::ptrdiff_t`,
|
| 4551 |
the behavior is undefined. — *end note*]
|
| 4552 |
|
| 4553 |
For addition or subtraction, if the expressions `P` or `Q` have type
|
| 4554 |
“pointer to cv `T`”, where `T` and the array element type are not
|
| 4555 |
similar [[conv.qual]], the behavior is undefined.
|
| 4556 |
|
| 4557 |
-
[*
|
| 4558 |
-
|
| 4559 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4560 |
|
| 4561 |
### Shift operators <a id="expr.shift">[[expr.shift]]</a>
|
| 4562 |
|
| 4563 |
The shift operators `<<` and `>>` group left-to-right.
|
| 4564 |
|
|
@@ -4627,37 +4920,41 @@ Then:
|
|
| 4627 |
|
| 4628 |
If both operands have the same enumeration type `E`, the operator yields
|
| 4629 |
the result of converting the operands to the underlying type of `E` and
|
| 4630 |
applying `<=>` to the converted operands.
|
| 4631 |
|
| 4632 |
-
If at least one of the operands is of pointer type and the other
|
| 4633 |
-
is of pointer or array type, array-to-pointer conversions
|
| 4634 |
[[conv.array]], pointer conversions [[conv.ptr]], and qualification
|
| 4635 |
conversions [[conv.qual]] are performed on both operands to bring them
|
| 4636 |
to their composite pointer type [[expr.type]]. After the conversions,
|
| 4637 |
the operands shall have the same type.
|
| 4638 |
|
| 4639 |
[*Note 1*: If both of the operands are arrays, array-to-pointer
|
| 4640 |
conversions [[conv.array]] are not applied. — *end note*]
|
| 4641 |
|
| 4642 |
-
|
| 4643 |
-
|
| 4644 |
-
|
| 4645 |
-
`p` and `q` compare
|
|
|
|
|
|
|
| 4646 |
`std::strong_ordering::less` if `q` compares greater than `p` and
|
| 4647 |
`std::strong_ordering::greater` if `p` compares greater than `q`
|
| 4648 |
-
[[expr.rel]]
|
|
|
|
| 4649 |
|
| 4650 |
Otherwise, the program is ill-formed.
|
| 4651 |
|
| 4652 |
The three comparison category types [[cmp.categories]] (the types
|
| 4653 |
`std::strong_ordering`, `std::weak_ordering`, and
|
| 4654 |
-
`std::partial_ordering`) are not predefined; if
|
| 4655 |
-
|
| 4656 |
-
|
|
|
|
| 4657 |
specifier [[dcl.spec.auto]] in a defaulted three-way comparison
|
| 4658 |
-
[[class.spaceship]] or use of the built-in operator)
|
| 4659 |
ill-formed.
|
| 4660 |
|
| 4661 |
### Relational operators <a id="expr.rel">[[expr.rel]]</a>
|
| 4662 |
|
| 4663 |
The relational operators group left-to-right.
|
|
@@ -4689,30 +4986,36 @@ operands of arithmetic or enumeration type. If both operands are
|
|
| 4689 |
pointers, pointer conversions [[conv.ptr]] and qualification conversions
|
| 4690 |
[[conv.qual]] are performed to bring them to their composite pointer
|
| 4691 |
type [[expr.type]]. After conversions, the operands shall have the same
|
| 4692 |
type.
|
| 4693 |
|
| 4694 |
-
The result of comparing unequal pointers to objects
|
| 4695 |
-
|
|
|
|
|
|
|
| 4696 |
|
| 4697 |
- If two pointers point to different elements of the same array, or to
|
| 4698 |
subobjects thereof, the pointer to the element with the higher
|
| 4699 |
subscript is required to compare greater.
|
| 4700 |
- If two pointers point to different non-static data members of the same
|
| 4701 |
object, or to subobjects of such members, recursively, the pointer to
|
| 4702 |
-
the later declared member is required to compare greater provided
|
| 4703 |
-
|
| 4704 |
-
|
| 4705 |
- Otherwise, neither pointer is required to compare greater than the
|
| 4706 |
other.
|
| 4707 |
|
| 4708 |
If two operands `p` and `q` compare equal [[expr.eq]], `p<=q` and `p>=q`
|
| 4709 |
both yield `true` and `p<q` and `p>q` both yield `false`. Otherwise, if
|
| 4710 |
-
a pointer `p` compares greater than a pointer `q`, `p>=q`,
|
| 4711 |
-
`q<=p`, and `q<p` all yield `true` and `p<=q`, `p<q`, `q>=p`, and
|
| 4712 |
-
all yield `false`. Otherwise, the result of each of the operators
|
| 4713 |
-
unspecified.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4714 |
|
| 4715 |
If both operands (after conversions) are of arithmetic or enumeration
|
| 4716 |
type, each of the operators shall yield `true` if the specified
|
| 4717 |
relationship is true and `false` if it is false.
|
| 4718 |
|
|
@@ -4850,11 +5153,11 @@ exclusive-or-expression:
|
|
| 4850 |
The `^` operator groups left-to-right. The operands shall be of integral
|
| 4851 |
or unscoped enumeration type. The usual arithmetic conversions
|
| 4852 |
[[expr.arith.conv]] are performed. Given the coefficients `xᵢ` and `yᵢ`
|
| 4853 |
of the base-2 representation [[basic.fundamental]] of the converted
|
| 4854 |
operands `x` and `y`, the coefficient `rᵢ` of the base-2 representation
|
| 4855 |
-
of the result `r` is 1 if either (but not both) of `xᵢ` and `yᵢ`
|
| 4856 |
and 0 otherwise.
|
| 4857 |
|
| 4858 |
[*Note 1*: The result is the bitwise exclusive function of the
|
| 4859 |
operands. — *end note*]
|
| 4860 |
|
|
@@ -4869,11 +5172,11 @@ inclusive-or-expression:
|
|
| 4869 |
The `|` operator groups left-to-right. The operands shall be of integral
|
| 4870 |
or unscoped enumeration type. The usual arithmetic conversions
|
| 4871 |
[[expr.arith.conv]] are performed. Given the coefficients `xᵢ` and `yᵢ`
|
| 4872 |
of the base-2 representation [[basic.fundamental]] of the converted
|
| 4873 |
operands `x` and `y`, the coefficient `rᵢ` of the base-2 representation
|
| 4874 |
-
of the result `r` is 1 if at least one of `xᵢ` and `yᵢ`
|
| 4875 |
otherwise.
|
| 4876 |
|
| 4877 |
[*Note 1*: The result is the bitwise inclusive function of the
|
| 4878 |
operands. — *end note*]
|
| 4879 |
|
|
@@ -4958,14 +5261,15 @@ that determination. — *end note*]
|
|
| 4958 |
Attempts are made to form an implicit conversion sequence from an
|
| 4959 |
operand expression `E1` of type `T1` to a target type related to the
|
| 4960 |
type `T2` of the operand expression `E2` as follows:
|
| 4961 |
|
| 4962 |
- If `E2` is an lvalue, the target type is “lvalue reference to `T2`”,
|
| 4963 |
-
|
| 4964 |
-
bind directly [[dcl.init.ref]] to a glvalue.
|
| 4965 |
- If `E2` is an xvalue, the target type is “rvalue reference to `T2`”,
|
| 4966 |
-
|
|
|
|
| 4967 |
- If `E2` is a prvalue or if neither of the conversion sequences above
|
| 4968 |
can be formed and at least one of the operands has (possibly
|
| 4969 |
cv-qualified) class type:
|
| 4970 |
- if `T1` and `T2` are the same class type (ignoring cv-qualification)
|
| 4971 |
and `T2` is at least as cv-qualified as `T1`, the target type is
|
|
@@ -4997,11 +5301,11 @@ and it is a bit-field if the second or the third operand is a bit-field,
|
|
| 4997 |
or if both are bit-fields.
|
| 4998 |
|
| 4999 |
Otherwise, the result is a prvalue. If the second and third operands do
|
| 5000 |
not have the same type, and either has (possibly cv-qualified) class
|
| 5001 |
type, overload resolution is used to determine the conversions (if any)
|
| 5002 |
-
to be applied to the operands
|
| 5003 |
the overload resolution fails, the program is ill-formed. Otherwise, the
|
| 5004 |
conversions thus determined are applied, and the converted operands are
|
| 5005 |
used in place of the original operands for the remainder of this
|
| 5006 |
subclause.
|
| 5007 |
|
|
@@ -5041,11 +5345,11 @@ yield-expression:
|
|
| 5041 |
A *yield-expression* shall appear only within a suspension context of a
|
| 5042 |
function [[expr.await]]. Let *e* be the operand of the
|
| 5043 |
*yield-expression* and *p* be an lvalue naming the promise object of the
|
| 5044 |
enclosing coroutine [[dcl.fct.def.coroutine]], then the
|
| 5045 |
*yield-expression* is equivalent to the expression
|
| 5046 |
-
`co_await
|
| 5047 |
|
| 5048 |
[*Example 1*:
|
| 5049 |
|
| 5050 |
``` cpp
|
| 5051 |
template <typename T>
|
|
@@ -5062,14 +5366,14 @@ struct my_generator {
|
|
| 5062 |
iterator begin();
|
| 5063 |
iterator end();
|
| 5064 |
};
|
| 5065 |
|
| 5066 |
my_generator<pair<int,int>> g1() {
|
| 5067 |
-
for (int i =
|
| 5068 |
}
|
| 5069 |
my_generator<pair<int,int>> g2() {
|
| 5070 |
-
for (int i =
|
| 5071 |
}
|
| 5072 |
|
| 5073 |
auto f(int x = co_yield 5); // error: yield-expression outside of function suspension context
|
| 5074 |
int a[] = { co_yield 1 }; // error: yield-expression outside of function suspension context
|
| 5075 |
|
|
@@ -5102,12 +5406,12 @@ exception [[except.handle]]. The exception is reactivated with the
|
|
| 5102 |
existing exception object; no new exception object is created. The
|
| 5103 |
exception is no longer considered to be caught.
|
| 5104 |
|
| 5105 |
[*Example 1*:
|
| 5106 |
|
| 5107 |
-
|
| 5108 |
-
|
| 5109 |
|
| 5110 |
``` cpp
|
| 5111 |
try {
|
| 5112 |
// ...
|
| 5113 |
} catch (...) { // catch all exceptions
|
|
@@ -5124,17 +5428,18 @@ If no exception is presently being handled, evaluating a
|
|
| 5124 |
|
| 5125 |
### Assignment and compound assignment operators <a id="expr.ass">[[expr.ass]]</a>
|
| 5126 |
|
| 5127 |
The assignment operator (`=`) and the compound assignment operators all
|
| 5128 |
group right-to-left. All require a modifiable lvalue as their left
|
| 5129 |
-
operand; their result is an lvalue
|
| 5130 |
-
|
| 5131 |
-
In all cases, the assignment is
|
| 5132 |
-
|
| 5133 |
-
|
| 5134 |
-
operand. With respect to an
|
| 5135 |
-
|
|
|
|
| 5136 |
|
| 5137 |
[*Note 1*: Therefore, a function call cannot intervene between the
|
| 5138 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 5139 |
single compound assignment operator. — *end note*]
|
| 5140 |
|
|
@@ -5160,42 +5465,44 @@ If the right operand is an expression, it is implicitly converted
|
|
| 5160 |
|
| 5161 |
When the left operand of an assignment operator is a bit-field that
|
| 5162 |
cannot represent the value of the expression, the resulting value of the
|
| 5163 |
bit-field is *implementation-defined*.
|
| 5164 |
|
| 5165 |
-
|
| 5166 |
-
|
| 5167 |
-
assignment is a discarded-value expression or an unevaluated operand
|
|
|
|
| 5168 |
|
| 5169 |
The behavior of an expression of the form `E1 op= E2` is equivalent to
|
| 5170 |
-
`E1 = E1 op E2` except that `E1` is evaluated only once.
|
| 5171 |
-
|
| 5172 |
-
[
|
| 5173 |
-
|
| 5174 |
-
|
| 5175 |
-
arithmetic type
|
|
|
|
|
|
|
| 5176 |
|
| 5177 |
If the value being stored in an object is read via another object that
|
| 5178 |
overlaps in any way the storage of the first object, then the overlap
|
| 5179 |
shall be exact and the two objects shall have the same type, otherwise
|
| 5180 |
the behavior is undefined.
|
| 5181 |
|
| 5182 |
-
[*Note
|
| 5183 |
left and right sides of the assignment operation; it is not a statement
|
| 5184 |
-
about how the target of the assignment
|
| 5185 |
[[basic.lval]]. — *end note*]
|
| 5186 |
|
| 5187 |
A *braced-init-list* may appear on the right-hand side of
|
| 5188 |
|
| 5189 |
- an assignment to a scalar, in which case the initializer list shall
|
| 5190 |
have at most a single element. The meaning of `x = {v}`, where `T` is
|
| 5191 |
the scalar type of the expression `x`, is that of `x = T{v}`. The
|
| 5192 |
meaning of `x = {}` is `x = T{}`.
|
| 5193 |
- an assignment to an object of class type, in which case the
|
| 5194 |
initializer list is passed as the argument to the assignment operator
|
| 5195 |
-
function selected by overload resolution
|
| 5196 |
-
[[over.match]]).
|
| 5197 |
|
| 5198 |
[*Example 1*:
|
| 5199 |
|
| 5200 |
``` cpp
|
| 5201 |
complex<double> z;
|
|
@@ -5224,31 +5531,29 @@ left expression is sequenced before the right expression
|
|
| 5224 |
[[intro.execution]]. The type and value of the result are the type and
|
| 5225 |
value of the right operand; the result is of the same value category as
|
| 5226 |
its right operand, and is a bit-field if its right operand is a
|
| 5227 |
bit-field.
|
| 5228 |
|
| 5229 |
-
|
| 5230 |
|
| 5231 |
-
|
| 5232 |
-
|
|
|
|
|
|
|
|
|
|
| 5233 |
|
| 5234 |
-
|
| 5235 |
-
parentheses.
|
| 5236 |
-
|
| 5237 |
-
[*Example 2*:
|
| 5238 |
|
| 5239 |
``` cpp
|
| 5240 |
f(a, (t=3, t+2), c);
|
| 5241 |
```
|
| 5242 |
|
| 5243 |
has three arguments, the second of which has the value `5`.
|
| 5244 |
|
| 5245 |
— *end example*]
|
| 5246 |
|
| 5247 |
-
|
| 5248 |
-
*expr-or-braced-init-list* of a subscripting expression [[expr.sub]] is
|
| 5249 |
-
deprecated; see [[depr.comma.subscript]]. — *end note*]
|
| 5250 |
|
| 5251 |
## Constant expressions <a id="expr.const">[[expr.const]]</a>
|
| 5252 |
|
| 5253 |
Certain contexts require expressions that satisfy additional
|
| 5254 |
requirements as detailed in this subclause; other contexts have
|
|
@@ -5271,23 +5576,23 @@ A variable or temporary object `o` is *constant-initialized* if
|
|
| 5271 |
some initialization being performed, and
|
| 5272 |
- the full-expression of its initialization is a constant expression
|
| 5273 |
when interpreted as a *constant-expression*, except that if `o` is an
|
| 5274 |
object, that full-expression may also invoke constexpr constructors
|
| 5275 |
for `o` and its subobjects even if those objects are of non-literal
|
| 5276 |
-
class types. \[*Note 2*: Such a class
|
| 5277 |
destructor. Within this evaluation, `std::is_constant_evaluated()`
|
| 5278 |
[[meta.const.eval]] returns `true`. — *end note*]
|
| 5279 |
|
| 5280 |
A variable is *potentially-constant* if it is constexpr or it has
|
| 5281 |
-
reference or const-qualified integral or enumeration type.
|
| 5282 |
|
| 5283 |
-
A constant-initialized potentially-constant variable is *usable in
|
| 5284 |
-
constant expressions* at a point P if
|
| 5285 |
reachable from P and
|
| 5286 |
|
| 5287 |
-
-
|
| 5288 |
-
-
|
| 5289 |
- P is in the same translation unit as D.
|
| 5290 |
|
| 5291 |
An object or reference is *usable in constant expressions* if it is
|
| 5292 |
|
| 5293 |
- a variable that is usable in constant expressions, or
|
|
@@ -5300,52 +5605,57 @@ An object or reference is *usable in constant expressions* if it is
|
|
| 5300 |
|
| 5301 |
An expression E is a *core constant expression* unless the evaluation of
|
| 5302 |
E, following the rules of the abstract machine [[intro.execution]],
|
| 5303 |
would evaluate one of the following:
|
| 5304 |
|
| 5305 |
-
- `this` [[expr.prim.this]], except
|
| 5306 |
-
[[dcl.constexpr]] that is being evaluated as
|
| 5307 |
-
|
| 5308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5309 |
- an invocation of an undefined constexpr function;
|
| 5310 |
-
- an invocation of an instantiated constexpr function that
|
| 5311 |
-
|
| 5312 |
- an invocation of a virtual function [[class.virtual]] for an object
|
| 5313 |
-
|
| 5314 |
-
- the object is usable in constant expressions or
|
| 5315 |
-
- its lifetime began within the evaluation of E;
|
| 5316 |
- an expression that would exceed the implementation-defined limits (see
|
| 5317 |
[[implimits]]);
|
| 5318 |
- an operation that would have undefined behavior as specified in
|
| 5319 |
-
[[intro]] through [[cpp]]
|
| 5320 |
-
example, signed integer overflow [[expr.prop]], certain pointer
|
| 5321 |
-
arithmetic [[expr.add]], division by zero [[expr.mul]], or certain
|
| 5322 |
-
shift operations [[expr.shift]] — *end note*] ;
|
| 5323 |
- an lvalue-to-rvalue conversion [[conv.lval]] unless it is applied to
|
| 5324 |
- a non-volatile glvalue that refers to an object that is usable in
|
| 5325 |
constant expressions, or
|
| 5326 |
- a non-volatile glvalue of literal type that refers to a non-volatile
|
| 5327 |
object whose lifetime began within the evaluation of E;
|
| 5328 |
-
- an lvalue-to-rvalue conversion
|
| 5329 |
-
|
| 5330 |
-
thereof;
|
| 5331 |
- an lvalue-to-rvalue conversion that is applied to an object with an
|
| 5332 |
indeterminate value [[basic.indet]];
|
| 5333 |
- an invocation of an implicitly-defined copy/move constructor or
|
| 5334 |
copy/move assignment operator for a union whose active member (if any)
|
| 5335 |
is mutable, unless the lifetime of the union object began within the
|
| 5336 |
evaluation of E;
|
| 5337 |
-
- an *id-expression* that refers to a variable or data member of
|
| 5338 |
-
reference type unless the reference has a preceding initialization and
|
| 5339 |
-
either
|
| 5340 |
-
- it is usable in constant expressions or
|
| 5341 |
-
- its lifetime began within the evaluation of E;
|
| 5342 |
- in a *lambda-expression*, a reference to `this` or to a variable with
|
| 5343 |
automatic storage duration defined outside that *lambda-expression*,
|
| 5344 |
-
where the reference would be an odr-use
|
| 5345 |
-
[[expr.prim.lambda]]
|
| 5346 |
-
\[*Example
|
| 5347 |
``` cpp
|
| 5348 |
void g() {
|
| 5349 |
const int n = 0;
|
| 5350 |
[=] {
|
| 5351 |
constexpr int i = n; // OK, n is not odr-used here
|
|
@@ -5353,17 +5663,17 @@ would evaluate one of the following:
|
|
| 5353 |
};
|
| 5354 |
}
|
| 5355 |
```
|
| 5356 |
|
| 5357 |
— *end example*]
|
| 5358 |
-
\[*Note
|
| 5359 |
If the odr-use occurs in an invocation of a function call operator of
|
| 5360 |
a closure type, it no longer refers to `this` or to an enclosing
|
| 5361 |
automatic variable due to the transformation
|
| 5362 |
[[expr.prim.lambda.capture]] of the *id-expression* into an access of
|
| 5363 |
the corresponding data member.
|
| 5364 |
-
\[*Example
|
| 5365 |
``` cpp
|
| 5366 |
auto monad = [](auto v) { return [=] { return v; }; };
|
| 5367 |
auto bind = [](auto m) {
|
| 5368 |
return [=](auto fvm) { return fvm(m()); };
|
| 5369 |
};
|
|
@@ -5374,17 +5684,21 @@ would evaluate one of the following:
|
|
| 5374 |
|
| 5375 |
— *end example*]
|
| 5376 |
— *end note*]
|
| 5377 |
- a conversion from type cv `void*` to a pointer-to-object type;
|
| 5378 |
- a `reinterpret_cast` [[expr.reinterpret.cast]];
|
| 5379 |
-
- a modification of an object
|
| 5380 |
-
[[expr.pre.incr]]
|
| 5381 |
-
|
| 5382 |
-
within the evaluation of E;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5383 |
- a *new-expression* [[expr.new]], unless the selected allocation
|
| 5384 |
-
function is a replaceable global allocation function
|
| 5385 |
-
[[new.delete.single]], [[new.delete.array]]
|
| 5386 |
is deallocated within the evaluation of E;
|
| 5387 |
- a *delete-expression* [[expr.delete]], unless it deallocates a region
|
| 5388 |
of storage allocated within the evaluation of E;
|
| 5389 |
- a call to an instance of `std::allocator<T>::allocate`
|
| 5390 |
[[allocator.members]], unless the allocated storage is deallocated
|
|
@@ -5394,41 +5708,53 @@ would evaluate one of the following:
|
|
| 5394 |
allocated within the evaluation of E;
|
| 5395 |
- an *await-expression* [[expr.await]];
|
| 5396 |
- a *yield-expression* [[expr.yield]];
|
| 5397 |
- a three-way comparison [[expr.spaceship]], relational [[expr.rel]], or
|
| 5398 |
equality [[expr.eq]] operator where the result is unspecified;
|
| 5399 |
-
- a *throw-expression* [[expr.throw]]
|
| 5400 |
-
|
| 5401 |
-
|
| 5402 |
-
-
|
| 5403 |
-
- an
|
|
|
|
|
|
|
|
|
|
| 5404 |
|
| 5405 |
-
|
| 5406 |
-
|
| 5407 |
-
|
| 5408 |
-
invocation of the `va_start` macro [[cstdarg.syn]], it is unspecified
|
| 5409 |
-
whether E is a core constant expression.
|
| 5410 |
|
| 5411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5412 |
|
| 5413 |
``` cpp
|
| 5414 |
int x; // not constant
|
| 5415 |
struct A {
|
| 5416 |
constexpr A(bool b) : m(b?42:x) { }
|
| 5417 |
int m;
|
| 5418 |
};
|
| 5419 |
-
constexpr int v = A(true).m; // OK
|
| 5420 |
|
| 5421 |
constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
|
| 5422 |
|
| 5423 |
constexpr int f1(int k) {
|
| 5424 |
constexpr int x = k; // error: x is not initialized by a constant expression
|
| 5425 |
// because lifetime of k began outside the initializer of x
|
| 5426 |
return x;
|
| 5427 |
}
|
| 5428 |
constexpr int f2(int k) {
|
| 5429 |
-
int x = k; // OK
|
| 5430 |
// because x is not constexpr
|
| 5431 |
return x;
|
| 5432 |
}
|
| 5433 |
|
| 5434 |
constexpr int incr(int &n) {
|
|
@@ -5438,67 +5764,132 @@ constexpr int g(int k) {
|
|
| 5438 |
constexpr int x = incr(k); // error: incr(k) is not a core constant expression
|
| 5439 |
// because lifetime of k began outside the expression incr(k)
|
| 5440 |
return x;
|
| 5441 |
}
|
| 5442 |
constexpr int h(int k) {
|
| 5443 |
-
int x = incr(k); // OK
|
| 5444 |
return x;
|
| 5445 |
}
|
| 5446 |
-
constexpr int y = h(1); // OK
|
| 5447 |
// h(1) is a core constant expression because
|
| 5448 |
// the lifetime of k begins inside h(1)
|
| 5449 |
```
|
| 5450 |
|
| 5451 |
— *end example*]
|
| 5452 |
|
| 5453 |
For the purposes of determining whether an expression E is a core
|
| 5454 |
-
constant expression, the evaluation of
|
| 5455 |
`std::allocator<T>` as defined in [[allocator.members]], where `T` is a
|
| 5456 |
-
literal type,
|
| 5457 |
-
|
| 5458 |
-
|
| 5459 |
-
|
| 5460 |
-
|
| 5461 |
-
|
| 5462 |
-
|
| 5463 |
-
|
| 5464 |
-
|
| 5465 |
-
|
| 5466 |
-
|
| 5467 |
-
|
| 5468 |
-
|
| 5469 |
-
|
| 5470 |
-
|
| 5471 |
-
|
| 5472 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5473 |
|
| 5474 |
An object `a` is said to have *constant destruction* if:
|
| 5475 |
|
| 5476 |
-
- it is not of class type nor (possibly
|
| 5477 |
-
|
| 5478 |
-
- it is of class type or (possibly
|
| 5479 |
-
|
| 5480 |
expression E whose only effect is to destroy `a`, E would be a core
|
| 5481 |
constant expression if the lifetime of `a` and its non-mutable
|
| 5482 |
subobjects (but not its mutable subobjects) were considered to start
|
| 5483 |
within E.
|
| 5484 |
|
| 5485 |
An *integral constant expression* is an expression of integral or
|
| 5486 |
unscoped enumeration type, implicitly converted to a prvalue, where the
|
| 5487 |
converted expression is a core constant expression.
|
| 5488 |
|
| 5489 |
-
[*Note 6*: Such expressions
|
| 5490 |
[[class.bit]], as enumerator initializers if the underlying type is not
|
| 5491 |
fixed [[dcl.enum]], and as alignments [[dcl.align]]. — *end note*]
|
| 5492 |
|
| 5493 |
If an expression of literal class type is used in a context where an
|
| 5494 |
integral constant expression is required, then that expression is
|
| 5495 |
contextually implicitly converted [[conv]] to an integral or unscoped
|
| 5496 |
enumeration type and the selected conversion function shall be
|
| 5497 |
`constexpr`.
|
| 5498 |
|
| 5499 |
-
[*Example
|
| 5500 |
|
| 5501 |
``` cpp
|
| 5502 |
struct A {
|
| 5503 |
constexpr A(int i) : val(i) { }
|
| 5504 |
constexpr operator int() const { return val; }
|
|
@@ -5530,11 +5921,11 @@ constant expression and the implicit conversion sequence contains only
|
|
| 5530 |
and
|
| 5531 |
- function pointer conversions [[conv.fctptr]],
|
| 5532 |
|
| 5533 |
and where the reference binding (if any) binds directly.
|
| 5534 |
|
| 5535 |
-
[*Note 7*: Such expressions
|
| 5536 |
[[expr.new]], as case expressions [[stmt.switch]], as enumerator
|
| 5537 |
initializers if the underlying type is fixed [[dcl.enum]], as array
|
| 5538 |
bounds [[dcl.array]], and as non-type template arguments
|
| 5539 |
[[temp.arg]]. — *end note*]
|
| 5540 |
|
|
@@ -5549,10 +5940,12 @@ expression (as defined below), or a prvalue core constant expression
|
|
| 5549 |
whose value satisfies the following constraints:
|
| 5550 |
|
| 5551 |
- if the value is an object of class type, each non-static data member
|
| 5552 |
of reference type refers to an entity that is a permitted result of a
|
| 5553 |
constant expression,
|
|
|
|
|
|
|
| 5554 |
- if the value is of pointer type, it contains the address of an object
|
| 5555 |
with static storage duration, the address past the end of such an
|
| 5556 |
object [[expr.add]], the address of a non-immediate function, or a
|
| 5557 |
null pointer value,
|
| 5558 |
- if the value is of pointer-to-member-function type, it does not
|
|
@@ -5563,11 +5956,15 @@ whose value satisfies the following constraints:
|
|
| 5563 |
An entity is a *permitted result of a constant expression* if it is an
|
| 5564 |
object with static storage duration that either is not a temporary
|
| 5565 |
object or is a temporary object whose value satisfies the above
|
| 5566 |
constraints, or if it is a non-immediate function.
|
| 5567 |
|
| 5568 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5569 |
|
| 5570 |
``` cpp
|
| 5571 |
consteval int f() { return 42; }
|
| 5572 |
consteval auto g() { return f; }
|
| 5573 |
consteval int h(int (*p)() = g()) { return p(); }
|
|
@@ -5576,19 +5973,23 @@ constexpr auto e = g(); // error: a pointer to an immedi
|
|
| 5576 |
// not a permitted result of a constant expression
|
| 5577 |
```
|
| 5578 |
|
| 5579 |
— *end example*]
|
| 5580 |
|
| 5581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5582 |
|
| 5583 |
Since this document imposes no restrictions on the accuracy of
|
| 5584 |
floating-point operations, it is unspecified whether the evaluation of a
|
| 5585 |
floating-point expression during translation yields the same result as
|
| 5586 |
the evaluation of the same expression (or the same operations on the
|
| 5587 |
-
same values) during program execution.
|
| 5588 |
|
| 5589 |
-
[*Example
|
| 5590 |
|
| 5591 |
``` cpp
|
| 5592 |
bool f() {
|
| 5593 |
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
|
| 5594 |
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
|
|
@@ -5601,27 +6002,121 @@ It is unspecified whether the value of `f()` will be `true` or `false`.
|
|
| 5601 |
— *end example*]
|
| 5602 |
|
| 5603 |
— *end note*]
|
| 5604 |
|
| 5605 |
An expression or conversion is in an *immediate function context* if it
|
| 5606 |
-
is potentially evaluated and
|
| 5607 |
-
|
| 5608 |
-
|
| 5609 |
-
|
| 5610 |
-
|
| 5611 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5612 |
|
| 5613 |
An expression or conversion is *manifestly constant-evaluated* if it is:
|
| 5614 |
|
| 5615 |
- a *constant-expression*, or
|
| 5616 |
- the condition of a constexpr if statement [[stmt.if]], or
|
| 5617 |
- an immediate invocation, or
|
| 5618 |
- the result of substitution into an atomic constraint expression to
|
| 5619 |
determine whether it is satisfied [[temp.constr.atomic]], or
|
| 5620 |
- the initializer of a variable that is usable in constant expressions
|
| 5621 |
-
or has constant initialization.[^
|
| 5622 |
-
\[*Example
|
| 5623 |
``` cpp
|
| 5624 |
template<bool> struct X {};
|
| 5625 |
X<std::is_constant_evaluated()> x; // type X<true>
|
| 5626 |
int y;
|
| 5627 |
const int a = std::is_constant_evaluated() ? y : 1; // dynamic initialization to 1
|
|
@@ -5630,44 +6125,44 @@ An expression or conversion is *manifestly constant-evaluated* if it is:
|
|
| 5630 |
const int b = std::is_constant_evaluated() ? 2 : y; // static initialization to 2
|
| 5631 |
int c = y + (std::is_constant_evaluated() ? 2 : y); // dynamic initialization to y+y
|
| 5632 |
|
| 5633 |
constexpr int f() {
|
| 5634 |
const int n = std::is_constant_evaluated() ? 13 : 17; // n is 13
|
| 5635 |
-
int m = std::is_constant_evaluated() ? 13 : 17; // m
|
| 5636 |
char arr[n] = {}; // char[13]
|
| 5637 |
return m + sizeof(arr);
|
| 5638 |
}
|
| 5639 |
int p = f(); // m is 13; initialized to 26
|
| 5640 |
int q = p + f(); // m is 17 for this call; initialized to 56
|
| 5641 |
```
|
| 5642 |
|
| 5643 |
— *end example*]
|
| 5644 |
|
| 5645 |
-
[*Note
|
| 5646 |
-
in an unevaluated operand
|
|
|
|
| 5647 |
|
| 5648 |
An expression or conversion is *potentially constant evaluated* if it
|
| 5649 |
is:
|
| 5650 |
|
| 5651 |
- a manifestly constant-evaluated expression,
|
| 5652 |
- a potentially-evaluated expression [[basic.def.odr]],
|
| 5653 |
-
- an immediate subexpression of a *braced-init-list*,
|
| 5654 |
- an expression of the form `&` *cast-expression* that occurs within a
|
| 5655 |
-
templated entity,
|
| 5656 |
-
- a subexpression of one of
|
| 5657 |
-
|
| 5658 |
|
| 5659 |
A function or variable is *needed for constant evaluation* if it is:
|
| 5660 |
|
| 5661 |
- a constexpr function that is named by an expression [[basic.def.odr]]
|
| 5662 |
that is potentially constant evaluated, or
|
| 5663 |
-
- a variable
|
| 5664 |
-
|
| 5665 |
-
const-qualified integral type or of reference type.
|
| 5666 |
|
| 5667 |
<!-- Link reference definitions -->
|
| 5668 |
-
[allocator.members]:
|
| 5669 |
[bad.alloc]: support.md#bad.alloc
|
| 5670 |
[bad.cast]: support.md#bad.cast
|
| 5671 |
[bad.typeid]: support.md#bad.typeid
|
| 5672 |
[basic.align]: basic.md#basic.align
|
| 5673 |
[basic.compound]: basic.md#basic.compound
|
|
@@ -5675,23 +6170,26 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5675 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 5676 |
[basic.indet]: basic.md#basic.indet
|
| 5677 |
[basic.life]: basic.md#basic.life
|
| 5678 |
[basic.lookup]: basic.md#basic.lookup
|
| 5679 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 5680 |
-
[basic.lookup.
|
|
|
|
| 5681 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 5682 |
[basic.lval]: #basic.lval
|
| 5683 |
-
[basic.
|
| 5684 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 5685 |
[basic.scope.class]: basic.md#basic.scope.class
|
|
|
|
| 5686 |
[basic.start.main]: basic.md#basic.start.main
|
|
|
|
| 5687 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 5688 |
[basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
|
| 5689 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 5690 |
-
[basic.stc.
|
|
|
|
| 5691 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 5692 |
-
[basic.types]: basic.md#basic.types
|
| 5693 |
[class]: class.md#class
|
| 5694 |
[class.abstract]: class.md#class.abstract
|
| 5695 |
[class.access]: class.md#class.access
|
| 5696 |
[class.access.base]: class.md#class.access.base
|
| 5697 |
[class.base.init]: class.md#class.base.init
|
|
@@ -5706,31 +6204,32 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5706 |
[class.derived]: class.md#class.derived
|
| 5707 |
[class.dtor]: class.md#class.dtor
|
| 5708 |
[class.free]: class.md#class.free
|
| 5709 |
[class.friend]: class.md#class.friend
|
| 5710 |
[class.mem]: class.md#class.mem
|
| 5711 |
-
[class.member.lookup]:
|
| 5712 |
[class.mfct]: class.md#class.mfct
|
| 5713 |
-
[class.mfct.non
|
| 5714 |
[class.mi]: class.md#class.mi
|
| 5715 |
[class.prop]: class.md#class.prop
|
| 5716 |
-
[class.qual]: basic.md#class.qual
|
| 5717 |
[class.spaceship]: class.md#class.spaceship
|
| 5718 |
-
[class.static]: class.md#class.static
|
| 5719 |
[class.temporary]: basic.md#class.temporary
|
| 5720 |
-
[class.this]: class.md#class.this
|
| 5721 |
[class.union]: class.md#class.union
|
|
|
|
| 5722 |
[class.virtual]: class.md#class.virtual
|
| 5723 |
[cmp.categories]: support.md#cmp.categories
|
|
|
|
| 5724 |
[conv]: #conv
|
| 5725 |
[conv.array]: #conv.array
|
| 5726 |
[conv.bool]: #conv.bool
|
| 5727 |
[conv.double]: #conv.double
|
| 5728 |
[conv.fctptr]: #conv.fctptr
|
| 5729 |
[conv.fpint]: #conv.fpint
|
| 5730 |
[conv.fpprom]: #conv.fpprom
|
| 5731 |
[conv.func]: #conv.func
|
|
|
|
| 5732 |
[conv.integral]: #conv.integral
|
| 5733 |
[conv.lval]: #conv.lval
|
| 5734 |
[conv.mem]: #conv.mem
|
| 5735 |
[conv.prom]: #conv.prom
|
| 5736 |
[conv.ptr]: #conv.ptr
|
|
@@ -5741,10 +6240,11 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5741 |
[cstdarg.syn]: support.md#cstdarg.syn
|
| 5742 |
[cstddef.syn]: support.md#cstddef.syn
|
| 5743 |
[dcl.align]: dcl.md#dcl.align
|
| 5744 |
[dcl.array]: dcl.md#dcl.array
|
| 5745 |
[dcl.asm]: dcl.md#dcl.asm
|
|
|
|
| 5746 |
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 5747 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 5748 |
[dcl.decl]: dcl.md#dcl.decl
|
| 5749 |
[dcl.enum]: dcl.md#dcl.enum
|
| 5750 |
[dcl.fct]: dcl.md#dcl.fct
|
|
@@ -5764,17 +6264,20 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5764 |
[dcl.ref]: dcl.md#dcl.ref
|
| 5765 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 5766 |
[dcl.stc]: dcl.md#dcl.stc
|
| 5767 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 5768 |
[dcl.type]: dcl.md#dcl.type
|
|
|
|
| 5769 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
|
|
|
|
|
|
| 5770 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 5771 |
[defns.access]: intro.md#defns.access
|
|
|
|
| 5772 |
[depr.arith.conv.enum]: future.md#depr.arith.conv.enum
|
| 5773 |
[depr.array.comp]: future.md#depr.array.comp
|
| 5774 |
[depr.capture.this]: future.md#depr.capture.this
|
| 5775 |
-
[depr.comma.subscript]: future.md#depr.comma.subscript
|
| 5776 |
[depr.volatile.type]: future.md#depr.volatile.type
|
| 5777 |
[except]: except.md#except
|
| 5778 |
[except.handle]: except.md#except.handle
|
| 5779 |
[except.pre]: except.md#except.pre
|
| 5780 |
[except.spec]: except.md#except.spec
|
|
@@ -5803,26 +6306,30 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5803 |
[expr.mptr.oper]: #expr.mptr.oper
|
| 5804 |
[expr.mul]: #expr.mul
|
| 5805 |
[expr.new]: #expr.new
|
| 5806 |
[expr.or]: #expr.or
|
| 5807 |
[expr.post]: #expr.post
|
|
|
|
| 5808 |
[expr.post.incr]: #expr.post.incr
|
| 5809 |
[expr.pre]: #expr.pre
|
| 5810 |
[expr.pre.incr]: #expr.pre.incr
|
| 5811 |
[expr.prim]: #expr.prim
|
| 5812 |
[expr.prim.fold]: #expr.prim.fold
|
| 5813 |
[expr.prim.id]: #expr.prim.id
|
| 5814 |
[expr.prim.id.dtor]: #expr.prim.id.dtor
|
|
|
|
| 5815 |
[expr.prim.id.qual]: #expr.prim.id.qual
|
| 5816 |
[expr.prim.id.unqual]: #expr.prim.id.unqual
|
| 5817 |
[expr.prim.lambda]: #expr.prim.lambda
|
| 5818 |
[expr.prim.lambda.capture]: #expr.prim.lambda.capture
|
| 5819 |
[expr.prim.lambda.closure]: #expr.prim.lambda.closure
|
|
|
|
| 5820 |
[expr.prim.literal]: #expr.prim.literal
|
| 5821 |
[expr.prim.paren]: #expr.prim.paren
|
| 5822 |
[expr.prim.req]: #expr.prim.req
|
| 5823 |
[expr.prim.req.compound]: #expr.prim.req.compound
|
|
|
|
| 5824 |
[expr.prim.req.nested]: #expr.prim.req.nested
|
| 5825 |
[expr.prim.req.simple]: #expr.prim.req.simple
|
| 5826 |
[expr.prim.req.type]: #expr.prim.req.type
|
| 5827 |
[expr.prim.this]: #expr.prim.this
|
| 5828 |
[expr.prop]: #expr.prop
|
|
@@ -5837,10 +6344,11 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5837 |
[expr.throw]: #expr.throw
|
| 5838 |
[expr.type]: #expr.type
|
| 5839 |
[expr.type.conv]: #expr.type.conv
|
| 5840 |
[expr.typeid]: #expr.typeid
|
| 5841 |
[expr.unary]: #expr.unary
|
|
|
|
| 5842 |
[expr.unary.noexcept]: #expr.unary.noexcept
|
| 5843 |
[expr.unary.op]: #expr.unary.op
|
| 5844 |
[expr.xor]: #expr.xor
|
| 5845 |
[expr.yield]: #expr.yield
|
| 5846 |
[function.objects]: utilities.md#function.objects
|
|
@@ -5852,35 +6360,41 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5852 |
[lex.ext]: lex.md#lex.ext
|
| 5853 |
[lex.icon]: lex.md#lex.icon
|
| 5854 |
[lex.literal]: lex.md#lex.literal
|
| 5855 |
[lex.string]: lex.md#lex.string
|
| 5856 |
[library]: library.md#library
|
| 5857 |
-
[meta.const.eval]:
|
| 5858 |
-
[namespace.
|
| 5859 |
[new.badlength]: support.md#new.badlength
|
| 5860 |
[new.delete.array]: support.md#new.delete.array
|
| 5861 |
[new.delete.placement]: support.md#new.delete.placement
|
| 5862 |
[new.delete.single]: support.md#new.delete.single
|
| 5863 |
[over]: over.md#over
|
| 5864 |
[over.ass]: over.md#over.ass
|
| 5865 |
[over.best.ics]: over.md#over.best.ics
|
| 5866 |
[over.built]: over.md#over.built
|
| 5867 |
[over.call]: over.md#over.call
|
|
|
|
| 5868 |
[over.ics.user]: over.md#over.ics.user
|
| 5869 |
[over.literal]: over.md#over.literal
|
| 5870 |
[over.match]: over.md#over.match
|
| 5871 |
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 5872 |
[over.match.oper]: over.md#over.match.oper
|
| 5873 |
[over.match.viable]: over.md#over.match.viable
|
| 5874 |
[over.oper]: over.md#over.oper
|
| 5875 |
[over.over]: over.md#over.over
|
|
|
|
| 5876 |
[replacement.functions]: library.md#replacement.functions
|
| 5877 |
[special]: class.md#special
|
|
|
|
|
|
|
| 5878 |
[stmt.if]: stmt.md#stmt.if
|
| 5879 |
[stmt.iter]: stmt.md#stmt.iter
|
| 5880 |
[stmt.jump]: stmt.md#stmt.jump
|
|
|
|
| 5881 |
[stmt.return]: stmt.md#stmt.return
|
|
|
|
| 5882 |
[stmt.switch]: stmt.md#stmt.switch
|
| 5883 |
[support.runtime]: support.md#support.runtime
|
| 5884 |
[support.types.layout]: support.md#support.types.layout
|
| 5885 |
[temp.arg]: temp.md#temp.arg
|
| 5886 |
[temp.concept]: temp.md#temp.concept
|
|
@@ -5888,17 +6402,25 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5888 |
[temp.constr.constr]: temp.md#temp.constr.constr
|
| 5889 |
[temp.constr.decl]: temp.md#temp.constr.decl
|
| 5890 |
[temp.dep.constexpr]: temp.md#temp.dep.constexpr
|
| 5891 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 5892 |
[temp.explicit]: temp.md#temp.explicit
|
|
|
|
| 5893 |
[temp.names]: temp.md#temp.names
|
|
|
|
| 5894 |
[temp.param]: temp.md#temp.param
|
| 5895 |
[temp.pre]: temp.md#temp.pre
|
| 5896 |
[temp.res]: temp.md#temp.res
|
|
|
|
| 5897 |
[temp.variadic]: temp.md#temp.variadic
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5898 |
[thread]: thread.md#thread
|
| 5899 |
[type.info]: support.md#type.info
|
|
|
|
| 5900 |
|
| 5901 |
[^1]: The precedence of operators is not directly specified, but it can
|
| 5902 |
be derived from the syntax.
|
| 5903 |
|
| 5904 |
[^2]: Overloaded operators are never assumed to be associative or
|
|
@@ -5907,11 +6429,11 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5907 |
[^3]: The cast and assignment operators must still perform their
|
| 5908 |
specific conversions as described in [[expr.type.conv]],
|
| 5909 |
[[expr.cast]], [[expr.static.cast]] and [[expr.ass]].
|
| 5910 |
|
| 5911 |
[^4]: The intent of this list is to specify those circumstances in which
|
| 5912 |
-
an object
|
| 5913 |
|
| 5914 |
[^5]: For historical reasons, this conversion is called the
|
| 5915 |
“lvalue-to-rvalue” conversion, even though that name does not
|
| 5916 |
accurately reflect the taxonomy of expressions described in
|
| 5917 |
[[basic.lval]].
|
|
@@ -5925,23 +6447,23 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5925 |
be obtained.
|
| 5926 |
|
| 5927 |
[^8]: The rule for conversion of pointers to members (from pointer to
|
| 5928 |
member of base to pointer to member of derived) appears inverted
|
| 5929 |
compared to the rule for pointers to objects (from pointer to
|
| 5930 |
-
derived to pointer to base)
|
| 5931 |
inversion is necessary to ensure type safety. Note that a pointer to
|
| 5932 |
member is not an object pointer or a function pointer and the rules
|
| 5933 |
for conversions of such pointers do not apply to pointers to
|
| 5934 |
members. In particular, a pointer to member cannot be converted to a
|
| 5935 |
`void*`.
|
| 5936 |
|
| 5937 |
[^9]: As a consequence, operands of type `bool`, `char8_t`, `char16_t`,
|
| 5938 |
-
`char32_t`, `wchar_t`, or
|
| 5939 |
integral type.
|
| 5940 |
|
| 5941 |
[^10]: This also applies when the object expression is an implicit
|
| 5942 |
-
`(*this)`
|
| 5943 |
|
| 5944 |
[^11]: This is true even if the subscript operator is used in the
|
| 5945 |
following common idiom: `&x[0]`.
|
| 5946 |
|
| 5947 |
[^12]: If the class member access expression is evaluated, the
|
|
@@ -5958,38 +6480,37 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 5958 |
[^15]: The recommended name for such a class is `extended_type_info`.
|
| 5959 |
|
| 5960 |
[^16]: If `p` is an expression of pointer type, then `*p`, `(*p)`,
|
| 5961 |
`*(p)`, `((*p))`, `*((p))`, and so on all meet this requirement.
|
| 5962 |
|
| 5963 |
-
[^17]: The types
|
| 5964 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 5965 |
constness.
|
| 5966 |
|
| 5967 |
-
[^18]: `T1` and `T2`
|
| 5968 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 5969 |
constness.
|
| 5970 |
|
| 5971 |
-
[^19]: This is sometimes referred to as a
|
| 5972 |
refers to the same object as the source glvalue.
|
| 5973 |
|
| 5974 |
-
[^20]: `const_cast`
|
|
|
|
| 5975 |
|
| 5976 |
-
|
| 5977 |
|
| 5978 |
-
[^
|
| 5979 |
-
|
| 5980 |
-
[^22]: The actual size of a potentially-overlapping subobject may be
|
| 5981 |
less than the result of applying `sizeof` to the subobject, due to
|
| 5982 |
virtual base classes and less strict padding requirements on
|
| 5983 |
potentially-overlapping subobjects.
|
| 5984 |
|
| 5985 |
[^23]: If the conversion function returns a signed integer type, the
|
| 5986 |
second standard conversion converts to the unsigned type
|
| 5987 |
`std::size_t` and thus thwarts any attempt to detect a negative
|
| 5988 |
value afterwards.
|
| 5989 |
|
| 5990 |
-
[^24]: This
|
| 5991 |
constructor.
|
| 5992 |
|
| 5993 |
[^25]: A *lambda-expression* with a *lambda-introducer* that consists of
|
| 5994 |
empty square brackets can follow the `delete` keyword if the
|
| 5995 |
*lambda-expression* is enclosed in parentheses.
|
|
@@ -6017,17 +6538,19 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 6017 |
|
| 6018 |
[^31]: As specified in [[basic.compound]], an object that is not an
|
| 6019 |
array element is considered to belong to a single-element array for
|
| 6020 |
this purpose.
|
| 6021 |
|
| 6022 |
-
[^32]:
|
| 6023 |
-
irrespective of whether the evaluation was performed during
|
| 6024 |
-
translation and/or during program execution.
|
| 6025 |
|
| 6026 |
-
[^33]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6027 |
initializer as described above.
|
| 6028 |
|
| 6029 |
-
[^
|
| 6030 |
-
narrowing conversion is performed [[dcl.init.list]].
|
| 6031 |
|
| 6032 |
-
[^
|
| 6033 |
-
expression is value-dependent [[temp.dep.constexpr]].
|
|
|
|
| 1 |
# Expressions <a id="expr">[[expr]]</a>
|
| 2 |
|
| 3 |
## Preamble <a id="expr.pre">[[expr.pre]]</a>
|
| 4 |
|
| 5 |
+
[*Note 1*:
|
| 6 |
+
|
| 7 |
+
[[expr]] defines the syntax, order of evaluation, and meaning of
|
| 8 |
+
expressions.[^1]
|
| 9 |
+
|
| 10 |
+
An expression is a sequence of operators and operands that specifies a
|
| 11 |
+
computation. An expression can result in a value and can cause side
|
| 12 |
+
effects.
|
| 13 |
+
|
| 14 |
+
— *end note*]
|
| 15 |
|
| 16 |
[*Note 2*: Operators can be overloaded, that is, given meaning when
|
| 17 |
applied to expressions of class type [[class]] or enumeration type
|
| 18 |
[[dcl.enum]]. Uses of overloaded operators are transformed into function
|
| 19 |
calls as described in [[over.oper]]. Overloaded operators obey the
|
|
|
|
| 43 |
zero divisor, and all floating-point exceptions varies among machines,
|
| 44 |
and is sometimes adjustable by a library function. — *end note*]
|
| 45 |
|
| 46 |
[*Note 4*:
|
| 47 |
|
| 48 |
+
The implementation can regroup operators according to the usual
|
| 49 |
mathematical rules only where the operators really are associative or
|
| 50 |
+
commutative.[^2]
|
| 51 |
+
|
| 52 |
+
For example, in the following fragment
|
| 53 |
|
| 54 |
``` cpp
|
| 55 |
int a, b;
|
| 56 |
...
|
| 57 |
a = a + 32760 + b + 5;
|
|
|
|
| 111 |
<a id="fig:basic.lval"></a>
|
| 112 |
|
| 113 |
![Expression category taxonomy \[fig:basic.lval\]](images/valuecategories.svg)
|
| 114 |
|
| 115 |
- A *glvalue* is an expression whose evaluation determines the identity
|
| 116 |
+
of an object or function.
|
| 117 |
- A *prvalue* is an expression whose evaluation initializes an object or
|
| 118 |
+
computes the value of an operand of an operator, as specified by the
|
| 119 |
+
context in which it appears, or an expression that has type cv `void`.
|
| 120 |
+
- An *xvalue* is a glvalue that denotes an object whose resources can be
|
| 121 |
+
reused (usually because it is near the end of its lifetime).
|
|
|
|
|
|
|
| 122 |
- An *lvalue* is a glvalue that is not an xvalue.
|
| 123 |
- An *rvalue* is a prvalue or an xvalue.
|
| 124 |
|
| 125 |
Every expression belongs to exactly one of the fundamental
|
| 126 |
classifications in this taxonomy: lvalue, xvalue, or prvalue. This
|
|
|
|
| 144 |
|
| 145 |
[*Note 3*:
|
| 146 |
|
| 147 |
An expression is an xvalue if it is:
|
| 148 |
|
| 149 |
+
- a move-eligible *id-expression* [[expr.prim.id.unqual]],
|
| 150 |
- the result of calling a function, whether implicitly or explicitly,
|
| 151 |
whose return type is an rvalue reference to object type [[expr.call]],
|
| 152 |
+
- a cast to an rvalue reference to object type
|
| 153 |
+
[[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.reinterpret.cast]], [[expr.const.cast]], [[expr.cast]],
|
|
|
|
| 154 |
- a subscripting operation with an xvalue array operand [[expr.sub]],
|
| 155 |
- a class member access expression designating a non-static data member
|
| 156 |
of non-reference type in which the object expression is an xvalue
|
| 157 |
[[expr.ref]], or
|
| 158 |
- a `.*` pointer-to-member expression in which the first operand is an
|
|
|
|
| 222 |
|
| 223 |
The discussion of reference initialization in [[dcl.init.ref]] and of
|
| 224 |
temporaries in [[class.temporary]] indicates the behavior of lvalues
|
| 225 |
and rvalues in other significant contexts.
|
| 226 |
|
| 227 |
+
Unless otherwise indicated [[dcl.type.decltype]], a prvalue shall always
|
| 228 |
have complete type or the `void` type; if it has a class type or
|
| 229 |
+
(possibly multidimensional) array of class type, that class shall not be
|
| 230 |
+
an abstract class [[class.abstract]]. A glvalue shall not have type
|
| 231 |
cv `void`.
|
| 232 |
|
| 233 |
+
[*Note 8*: A glvalue can have complete or incomplete non-`void` type.
|
| 234 |
Class and array prvalues can have cv-qualified types; other prvalues
|
| 235 |
always have cv-unqualified types. See [[expr.type]]. — *end note*]
|
| 236 |
|
| 237 |
An lvalue is *modifiable* unless its type is const-qualified or is a
|
| 238 |
function type.
|
| 239 |
|
| 240 |
[*Note 9*: A program that attempts to modify an object through a
|
| 241 |
+
nonmodifiable lvalue or through an rvalue is ill-formed
|
| 242 |
+
[[expr.ass]], [[expr.post.incr]], [[expr.pre.incr]]. — *end note*]
|
| 243 |
|
| 244 |
If a program attempts to access [[defns.access]] the stored value of an
|
| 245 |
object through a glvalue whose type is not similar [[conv.qual]] to one
|
| 246 |
of the following types the behavior is undefined:[^4]
|
| 247 |
|
|
|
|
| 253 |
If a program invokes a defaulted copy/move constructor or copy/move
|
| 254 |
assignment operator for a union of type `U` with a glvalue argument that
|
| 255 |
does not denote an object of type cv `U` within its lifetime, the
|
| 256 |
behavior is undefined.
|
| 257 |
|
| 258 |
+
[*Note 10*: In C, an entire object of structure type can be accessed,
|
| 259 |
+
e.g., using assignment. By contrast, C++ has no notion of accessing an
|
| 260 |
+
object of class type through an lvalue of class type. — *end note*]
|
| 261 |
|
| 262 |
### Type <a id="expr.type">[[expr.type]]</a>
|
| 263 |
|
| 264 |
+
If an expression initially has the type “reference to `T`”
|
| 265 |
+
[[dcl.ref]], [[dcl.init.ref]], the type is adjusted to `T` prior to any
|
| 266 |
further analysis. The expression designates the object or function
|
| 267 |
denoted by the reference, and the expression is an lvalue or an xvalue,
|
| 268 |
depending on the expression.
|
| 269 |
|
| 270 |
[*Note 1*: Before the lifetime of the reference has started or after it
|
|
|
|
| 289 |
- if `T1` or `T2` is “pointer to `noexcept` function” and the other type
|
| 290 |
is “pointer to function”, where the function types are otherwise the
|
| 291 |
same, “pointer to function”;
|
| 292 |
- if `T1` is “pointer to *cv1* `C1`” and `T2` is “pointer to *cv2*
|
| 293 |
`C2`”, where `C1` is reference-related to `C2` or `C2` is
|
| 294 |
+
reference-related to `C1` [[dcl.init.ref]], the qualification-combined
|
| 295 |
+
type [[conv.qual]] of `T1` and `T2` or the qualification-combined type
|
| 296 |
+
of `T2` and `T1`, respectively;
|
| 297 |
- if `T1` or `T2` is “pointer to member of `C1` of type function”, the
|
| 298 |
other type is “pointer to member of `C2` of type `noexcept` function”,
|
| 299 |
and `C1` is reference-related to `C2` or `C2` is reference-related to
|
| 300 |
`C1` [[dcl.init.ref]], where the function types are otherwise the
|
| 301 |
same, “pointer to member of `C2` of type function” or “pointer to
|
| 302 |
member of `C1` of type function”, respectively;
|
| 303 |
- if `T1` is “pointer to member of `C1` of type *cv1* `U`” and `T2` is
|
| 304 |
“pointer to member of `C2` of type *cv2* `U`”, for some non-function
|
| 305 |
type `U`, where `C1` is reference-related to `C2` or `C2` is
|
| 306 |
+
reference-related to `C1` [[dcl.init.ref]], the qualification-combined
|
| 307 |
+
type of `T2` and `T1` or the qualification-combined type of `T1` and
|
| 308 |
+
`T2`, respectively;
|
| 309 |
+
- if `T1` and `T2` are similar types [[conv.qual]], the
|
| 310 |
+
qualification-combined type of `T1` and `T2`;
|
| 311 |
- otherwise, a program that necessitates the determination of a
|
| 312 |
composite pointer type is ill-formed.
|
| 313 |
|
| 314 |
[*Example 1*:
|
| 315 |
|
|
|
|
| 326 |
|
| 327 |
— *end example*]
|
| 328 |
|
| 329 |
### Context dependence <a id="expr.context">[[expr.context]]</a>
|
| 330 |
|
| 331 |
+
In some contexts, *unevaluated operands* appear
|
| 332 |
+
[[expr.prim.req]], [[expr.typeid]], [[expr.sizeof]], [[expr.unary.noexcept]], [[dcl.type.decltype]], [[temp.pre]], [[temp.concept]].
|
| 333 |
+
An unevaluated operand is not evaluated.
|
|
|
|
| 334 |
|
| 335 |
+
[*Note 1*: In an unevaluated operand, a non-static class member can be
|
| 336 |
named [[expr.prim.id]] and naming of objects or functions does not, by
|
| 337 |
itself, require that a definition be provided [[basic.def.odr]]. An
|
| 338 |
unevaluated operand is considered a full-expression
|
| 339 |
[[intro.execution]]. — *end note*]
|
| 340 |
|
|
|
|
| 357 |
these expressions.
|
| 358 |
|
| 359 |
[*Note 2*: Using an overloaded operator causes a function call; the
|
| 360 |
above covers only operators with built-in meaning. — *end note*]
|
| 361 |
|
| 362 |
+
The temporary materialization conversion [[conv.rval]] is applied if the
|
| 363 |
+
(possibly converted) expression is a prvalue of object type.
|
| 364 |
|
| 365 |
+
[*Note 3*: If the original expression is an lvalue of class type, it
|
| 366 |
+
must have a volatile copy constructor to initialize the temporary object
|
| 367 |
+
that is the result object of the temporary materialization
|
| 368 |
+
conversion. — *end note*]
|
| 369 |
|
| 370 |
+
The expression is evaluated and its result (if any) is discarded.
|
| 371 |
|
| 372 |
## Standard conversions <a id="conv">[[conv]]</a>
|
| 373 |
|
| 374 |
+
### General <a id="conv.general">[[conv.general]]</a>
|
| 375 |
+
|
| 376 |
Standard conversions are implicit conversions with built-in meaning.
|
| 377 |
[[conv]] enumerates the full set of such conversions. A *standard
|
| 378 |
conversion sequence* is a sequence of standard conversions in the
|
| 379 |
following order:
|
| 380 |
|
|
|
|
| 453 |
the descriptions of those operators and contexts. — *end note*]
|
| 454 |
|
| 455 |
### Lvalue-to-rvalue conversion <a id="conv.lval">[[conv.lval]]</a>
|
| 456 |
|
| 457 |
A glvalue [[basic.lval]] of a non-function, non-array type `T` can be
|
| 458 |
+
converted to a prvalue.[^5]
|
| 459 |
+
|
| 460 |
+
If `T` is an incomplete type, a program that necessitates this
|
| 461 |
+
conversion is ill-formed. If `T` is a non-class type, the type of the
|
| 462 |
+
prvalue is the cv-unqualified version of `T`. Otherwise, the type of the
|
| 463 |
+
prvalue is `T`.[^6]
|
| 464 |
|
| 465 |
When an lvalue-to-rvalue conversion is applied to an expression E, and
|
| 466 |
either
|
| 467 |
|
| 468 |
- E is not potentially evaluated, or
|
|
|
|
| 497 |
`T` is volatile-qualified [[intro.execution]], and the glvalue can
|
| 498 |
refer to an inactive member of a union [[class.union]]. — *end note*]
|
| 499 |
- Otherwise, if `T` has a class type, the conversion copy-initializes
|
| 500 |
the result object from the glvalue.
|
| 501 |
- Otherwise, if the object to which the glvalue refers contains an
|
| 502 |
+
invalid pointer value [[basic.stc.dynamic.deallocation]], the behavior
|
| 503 |
+
is *implementation-defined*.
|
|
|
|
| 504 |
- Otherwise, the object indicated by the glvalue is read
|
| 505 |
[[defns.access]], and the value contained in the object is the prvalue
|
| 506 |
result.
|
| 507 |
|
| 508 |
[*Note 2*: See also [[basic.lval]]. — *end note*]
|
|
|
|
| 540 |
|
| 541 |
— *end example*]
|
| 542 |
|
| 543 |
### Qualification conversions <a id="conv.qual">[[conv.qual]]</a>
|
| 544 |
|
| 545 |
+
A *qualification-decomposition* of a type `T` is a sequence of cvᵢ and
|
| 546 |
+
Pᵢ such that `T` is
|
| 547 |
|
| 548 |
where each cvᵢ is a set of cv-qualifiers [[basic.type.qualifier]], and
|
| 549 |
each Pᵢ is “pointer to” [[dcl.ptr]], “pointer to member of class Cᵢ of
|
| 550 |
type” [[dcl.mptr]], “array of Nᵢ”, or “array of unknown bound of”
|
| 551 |
[[dcl.array]]. If Pᵢ designates an array, the cv-qualifiers cvᵢ₊₁ on the
|
| 552 |
element type are also taken as the cv-qualifiers cvᵢ of the array.
|
| 553 |
|
| 554 |
[*Example 1*: The type denoted by the *type-id* `const int **` has
|
| 555 |
+
three qualification-decompositions, taking `U` as “`int`”, as “pointer
|
| 556 |
+
to `const int`”, and as “pointer to pointer to
|
| 557 |
`const int`”. — *end example*]
|
| 558 |
|
| 559 |
The n-tuple of cv-qualifiers after the first one in the longest
|
| 560 |
+
qualification-decomposition of `T`, that is, cv₁, cv₂, …, cvₙ, is called
|
| 561 |
+
the *cv-qualification signature* of `T`.
|
| 562 |
|
| 563 |
+
Two types `T1` and `T2` are *similar* if they have
|
| 564 |
+
qualification-decompositions with the same n such that corresponding Pᵢ
|
| 565 |
+
components are either the same or one is “array of Nᵢ” and the other is
|
| 566 |
+
“array of unknown bound of”, and the types denoted by `U` are the same.
|
| 567 |
|
| 568 |
+
The *qualification-combined type* of two types `T1` and `T2` is the type
|
| 569 |
+
`T3` similar to `T1` whose qualification-decomposition is such that:
|
| 570 |
|
| 571 |
+
- for every i > 0, cv³ᵢ is the union of cv¹ᵢ and cv²ᵢ,
|
| 572 |
- if either P¹ᵢ or P²ᵢ is “array of unknown bound of”, P³ᵢ is “array of
|
| 573 |
+
unknown bound of”, otherwise it is P¹ᵢ, and
|
| 574 |
- if the resulting cv³ᵢ is different from cv¹ᵢ or cv²ᵢ, or the resulting
|
| 575 |
P³ᵢ is different from P¹ᵢ or P²ᵢ, then `const` is added to every cv³ₖ
|
| 576 |
+
for 0 < k < i,
|
| 577 |
|
| 578 |
+
where cvʲᵢ and Pʲᵢ are the components of the qualification-decomposition
|
| 579 |
+
of `T`j. A prvalue of type `T1` can be converted to type `T2` if the
|
| 580 |
+
qualification-combined type of `T1` and `T2` is `T2`.
|
| 581 |
|
| 582 |
[*Note 1*:
|
| 583 |
|
| 584 |
If a program could assign a pointer of type `T**` to a pointer of type
|
| 585 |
`const` `T**` (that is, if line \#1 below were allowed), a program could
|
|
|
|
| 597 |
```
|
| 598 |
|
| 599 |
— *end note*]
|
| 600 |
|
| 601 |
[*Note 2*: Given similar types `T1` and `T2`, this construction ensures
|
| 602 |
+
that both can be converted to the qualification-combined type of `T1`
|
| 603 |
+
and `T2`. — *end note*]
|
| 604 |
|
| 605 |
[*Note 3*: A prvalue of type “pointer to *cv1* `T`” can be converted to
|
| 606 |
a prvalue of type “pointer to *cv2* `T`” if “*cv2* `T`” is more
|
| 607 |
cv-qualified than “*cv1* `T`”. A prvalue of type “pointer to member of
|
| 608 |
`X` of type *cv1* `T`” can be converted to a prvalue of type “pointer to
|
|
|
|
| 613 |
pointer-to-member-function types) are never cv-qualified
|
| 614 |
[[dcl.fct]]. — *end note*]
|
| 615 |
|
| 616 |
### Integral promotions <a id="conv.prom">[[conv.prom]]</a>
|
| 617 |
|
| 618 |
+
A prvalue of an integer type other than `bool`, `char8_t`, `char16_t`,
|
| 619 |
+
`char32_t`, or `wchar_t` whose integer conversion rank [[conv.rank]] is
|
| 620 |
+
less than the rank of `int` can be converted to a prvalue of type `int`
|
| 621 |
+
if `int` can represent all the values of the source type; otherwise, the
|
| 622 |
+
source prvalue can be converted to a prvalue of type `unsigned int`.
|
| 623 |
|
| 624 |
+
A prvalue of type `char8_t`, `char16_t`, `char32_t`, or `wchar_t`
|
| 625 |
[[basic.fundamental]] can be converted to a prvalue of the first of the
|
| 626 |
following types that can represent all the values of its underlying
|
| 627 |
type: `int`, `unsigned int`, `long int`, `unsigned long int`,
|
| 628 |
`long long int`, or `unsigned long long int`. If none of the types in
|
| 629 |
that list can represent all the values of its underlying type, a prvalue
|
| 630 |
+
of type `char8_t`, `char16_t`, `char32_t`, or `wchar_t` can be converted
|
| 631 |
+
to a prvalue of its underlying type.
|
| 632 |
|
| 633 |
A prvalue of an unscoped enumeration type whose underlying type is not
|
| 634 |
fixed can be converted to a prvalue of the first of the following types
|
| 635 |
that can represent all the values of the enumeration [[dcl.enum]]:
|
| 636 |
`int`, `unsigned int`, `long int`, `unsigned long int`, `long long int`,
|
|
|
|
| 651 |
A prvalue for an integral bit-field [[class.bit]] can be converted to a
|
| 652 |
prvalue of type `int` if `int` can represent all the values of the
|
| 653 |
bit-field; otherwise, it can be converted to `unsigned int` if
|
| 654 |
`unsigned int` can represent all the values of the bit-field. If the
|
| 655 |
bit-field is larger yet, no integral promotion applies to it. If the
|
| 656 |
+
bit-field has enumeration type, it is treated as any other value of that
|
| 657 |
+
type for promotion purposes.
|
| 658 |
|
| 659 |
A prvalue of type `bool` can be converted to a prvalue of type `int`,
|
| 660 |
with `false` becoming zero and `true` becoming one.
|
| 661 |
|
| 662 |
These conversions are called *integral promotions*.
|
|
|
|
| 686 |
of integral conversions.
|
| 687 |
|
| 688 |
### Floating-point conversions <a id="conv.double">[[conv.double]]</a>
|
| 689 |
|
| 690 |
A prvalue of floating-point type can be converted to a prvalue of
|
| 691 |
+
another floating-point type with a greater or equal conversion rank
|
| 692 |
+
[[conv.rank]]. A prvalue of standard floating-point type can be
|
| 693 |
+
converted to a prvalue of another standard floating-point type.
|
| 694 |
+
|
| 695 |
+
If the source value can be exactly represented in the destination type,
|
| 696 |
+
the result of the conversion is that exact representation. If the source
|
| 697 |
+
value is between two adjacent destination values, the result of the
|
| 698 |
+
conversion is an *implementation-defined* choice of either of those
|
| 699 |
+
values. Otherwise, the behavior is undefined.
|
| 700 |
|
| 701 |
The conversions allowed as floating-point promotions are excluded from
|
| 702 |
the set of floating-point conversions.
|
| 703 |
|
| 704 |
### Floating-integral conversions <a id="conv.fpint">[[conv.fpint]]</a>
|
|
|
|
| 821 |
defined as follows:
|
| 822 |
|
| 823 |
- If either operand is of scoped enumeration type [[dcl.enum]], no
|
| 824 |
conversions are performed; if the other operand does not have the same
|
| 825 |
type, the expression is ill-formed.
|
| 826 |
+
- Otherwise, if either operand is of floating-point type, the following
|
| 827 |
+
rules are applied:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 828 |
- If both operands have the same type, no further conversion is
|
| 829 |
needed.
|
| 830 |
+
- Otherwise, if one of the operands is of a non-floating-point type,
|
| 831 |
+
that operand is converted to the type of the operand with the
|
| 832 |
+
floating-point type.
|
| 833 |
+
- Otherwise, if the floating-point conversion ranks [[conv.rank]] of
|
| 834 |
+
the types of the operands are ordered but not equal, then the
|
| 835 |
+
operand of the type with the lesser floating-point conversion rank
|
| 836 |
+
is converted to the type of the other operand.
|
| 837 |
+
- Otherwise, if the floating-point conversion ranks of the types of
|
| 838 |
+
the operands are equal, then the operand with the lesser
|
| 839 |
+
floating-point conversion subrank [[conv.rank]] is converted to the
|
| 840 |
+
type of the other operand.
|
| 841 |
+
- Otherwise, the expression is ill-formed.
|
| 842 |
+
- Otherwise, each operand is converted to a common type `C`. The
|
| 843 |
+
integral promotion rules [[conv.prom]] are used to determine a type
|
| 844 |
+
`T1` and type `T2` for each operand.[^9]
|
| 845 |
+
Then the following rules are applied to determine `C`:
|
| 846 |
+
- If `T1` and `T2` are the same type, `C` is that type.
|
| 847 |
+
- Otherwise, if `T1` and `T2` are both signed integer types or are
|
| 848 |
+
both unsigned integer types, `C` is the type with greater rank.
|
| 849 |
+
- Otherwise, let `U` be the unsigned integer type and `S` be the
|
| 850 |
+
signed integer type.
|
| 851 |
+
- If `U` has rank greater than or equal to the rank of `S`, `C` is
|
| 852 |
+
`U`.
|
| 853 |
+
- Otherwise, if `S` can represent all of the values of `U`, `C` is
|
| 854 |
+
`S`.
|
| 855 |
+
- Otherwise, `C` is the unsigned integer type corresponding to `S`.
|
| 856 |
|
| 857 |
If one operand is of enumeration type and the other operand is of a
|
| 858 |
different enumeration type or a floating-point type, this behavior is
|
| 859 |
deprecated [[depr.arith.conv.enum]].
|
| 860 |
|
|
|
|
| 871 |
requires-expression
|
| 872 |
```
|
| 873 |
|
| 874 |
### Literals <a id="expr.prim.literal">[[expr.prim.literal]]</a>
|
| 875 |
|
| 876 |
+
The type of a *literal* is determined based on its form as specified in
|
| 877 |
+
[[lex.literal]]. A *string-literal* is an lvalue designating a
|
| 878 |
+
corresponding string literal object [[lex.string]], a
|
| 879 |
+
*user-defined-literal* has the same value category as the corresponding
|
| 880 |
+
operator call expression described in [[lex.ext]], and any other
|
| 881 |
+
*literal* is a prvalue.
|
| 882 |
|
| 883 |
### This <a id="expr.prim.this">[[expr.prim.this]]</a>
|
| 884 |
|
| 885 |
+
The keyword `this` names a pointer to the object for which an implicit
|
| 886 |
+
object member function [[class.mfct.non.static]] is invoked or a
|
| 887 |
+
non-static data member’s initializer [[class.mem]] is evaluated.
|
| 888 |
+
|
| 889 |
+
The *current class* at a program point is the class associated with the
|
| 890 |
+
innermost class scope containing that point.
|
| 891 |
+
|
| 892 |
+
[*Note 1*: A *lambda-expression* does not introduce a class
|
| 893 |
+
scope. — *end note*]
|
| 894 |
|
| 895 |
If a declaration declares a member function or member function template
|
| 896 |
of a class `X`, the expression `this` is a prvalue of type “pointer to
|
| 897 |
+
*cv-qualifier-seq* `X`” wherever `X` is the current class between the
|
| 898 |
+
optional *cv-qualifier-seq* and the end of the *function-definition*,
|
| 899 |
+
*member-declarator*, or *declarator*. It shall not appear within the
|
| 900 |
+
declaration of either a static member function or an explicit object
|
| 901 |
+
member function of the current class (although its type and value
|
| 902 |
+
category are defined within such member functions as they are within an
|
| 903 |
+
implicit object member function).
|
| 904 |
|
| 905 |
+
[*Note 2*: This is because declaration matching does not occur until
|
| 906 |
the complete declarator is known. — *end note*]
|
| 907 |
|
| 908 |
+
[*Note 3*:
|
| 909 |
|
| 910 |
In a *trailing-return-type*, the class being defined is not required to
|
| 911 |
be complete for purposes of class member access [[expr.ref]]. Class
|
| 912 |
members declared later are not visible.
|
| 913 |
|
|
|
|
| 926 |
|
| 927 |
— *end note*]
|
| 928 |
|
| 929 |
Otherwise, if a *member-declarator* declares a non-static data member
|
| 930 |
[[class.mem]] of a class `X`, the expression `this` is a prvalue of type
|
| 931 |
+
“pointer to `X`” wherever `X` is the current class within the optional
|
| 932 |
+
default member initializer [[class.mem]].
|
| 933 |
|
| 934 |
The expression `this` shall not appear in any other context.
|
| 935 |
|
| 936 |
[*Example 2*:
|
| 937 |
|
| 938 |
``` cpp
|
| 939 |
class Outer {
|
| 940 |
int a[sizeof(*this)]; // error: not inside a member function
|
| 941 |
+
unsigned int sz = sizeof(*this); // OK, in default member initializer
|
| 942 |
|
| 943 |
void f() {
|
| 944 |
int b[sizeof(*this)]; // OK
|
| 945 |
|
| 946 |
struct Inner {
|
|
|
|
| 953 |
— *end example*]
|
| 954 |
|
| 955 |
### Parentheses <a id="expr.prim.paren">[[expr.prim.paren]]</a>
|
| 956 |
|
| 957 |
A parenthesized expression `(E)` is a primary expression whose type,
|
| 958 |
+
result, and value category are identical to those of E. The
|
| 959 |
+
parenthesized expression can be used in exactly the same contexts as
|
| 960 |
+
those where E can be used, and with the same meaning, except as
|
| 961 |
+
otherwise indicated.
|
| 962 |
|
| 963 |
### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
|
| 964 |
|
| 965 |
+
#### General <a id="expr.prim.id.general">[[expr.prim.id.general]]</a>
|
| 966 |
+
|
| 967 |
``` bnf
|
| 968 |
id-expression:
|
| 969 |
unqualified-id
|
| 970 |
qualified-id
|
| 971 |
```
|
|
|
|
| 973 |
An *id-expression* is a restricted form of a *primary-expression*.
|
| 974 |
|
| 975 |
[*Note 1*: An *id-expression* can appear after `.` and `->` operators
|
| 976 |
[[expr.ref]]. — *end note*]
|
| 977 |
|
| 978 |
+
If an *id-expression* E denotes a member M of an anonymous union
|
| 979 |
+
[[class.union.anon]] U:
|
| 980 |
+
|
| 981 |
+
- If U is a non-static data member, E refers to M as a member of the
|
| 982 |
+
lookup context of the terminal name of E (after any transformation to
|
| 983 |
+
a class member access expression [[class.mfct.non.static]]).
|
| 984 |
+
\[*Example 1*: `o.x` is interpreted as `o.u.x`, where u names the
|
| 985 |
+
anonymous union member. — *end example*]
|
| 986 |
+
- Otherwise, E is interpreted as a class member access [[expr.ref]] that
|
| 987 |
+
designates the member subobject M of the anonymous union variable for
|
| 988 |
+
U. \[*Note 2*: Under this interpretation, E no longer denotes a
|
| 989 |
+
non-static data member. — *end note*] \[*Example 2*: `N::x` is
|
| 990 |
+
interpreted as `N::u.x`, where u names the anonymous union
|
| 991 |
+
variable. — *end example*]
|
| 992 |
+
|
| 993 |
+
An *id-expression* that denotes a non-static data member or implicit
|
| 994 |
+
object member function of a class can only be used:
|
| 995 |
|
| 996 |
- as part of a class member access [[expr.ref]] in which the object
|
| 997 |
expression refers to the member’s class[^10] or a class derived from
|
| 998 |
that class, or
|
| 999 |
- to form a pointer to member [[expr.unary.op]], or
|
| 1000 |
- if that *id-expression* denotes a non-static data member and it
|
| 1001 |
appears in an unevaluated operand.
|
| 1002 |
+
\[*Example 3*:
|
| 1003 |
``` cpp
|
| 1004 |
struct S {
|
| 1005 |
int m;
|
| 1006 |
};
|
| 1007 |
int i = sizeof(S::m); // OK
|
| 1008 |
int j = sizeof(S::m + 42); // OK
|
| 1009 |
```
|
| 1010 |
|
| 1011 |
— *end example*]
|
| 1012 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1013 |
For an *id-expression* that denotes an overload set, overload resolution
|
| 1014 |
+
is performed to select a unique function [[over.match]], [[over.over]].
|
|
|
|
| 1015 |
|
| 1016 |
+
[*Note 3*:
|
| 1017 |
|
| 1018 |
A program cannot refer to a function with a trailing *requires-clause*
|
| 1019 |
whose *constraint-expression* is not satisfied, because such functions
|
| 1020 |
are never selected by overload resolution.
|
| 1021 |
|
| 1022 |
+
[*Example 4*:
|
| 1023 |
|
| 1024 |
``` cpp
|
| 1025 |
template<typename T> struct A {
|
| 1026 |
static void f(int) requires false;
|
| 1027 |
+
};
|
| 1028 |
|
| 1029 |
void g() {
|
| 1030 |
A<int>::f(0); // error: cannot call f
|
| 1031 |
void (*p1)(int) = A<int>::f; // error: cannot take the address of f
|
| 1032 |
decltype(A<int>::f)* p2 = nullptr; // error: the type decltype(A<int>::f) is invalid
|
| 1033 |
}
|
| 1034 |
```
|
| 1035 |
|
| 1036 |
In each case, the constraints of `f` are not satisfied. In the
|
| 1037 |
declaration of `p2`, those constraints are required to be satisfied even
|
| 1038 |
+
though `f` is an unevaluated operand [[term.unevaluated.operand]].
|
| 1039 |
|
| 1040 |
— *end example*]
|
| 1041 |
|
| 1042 |
— *end note*]
|
| 1043 |
|
|
|
|
| 1064 |
*literal-operator-id*s, see [[over.literal]]; for *template-id*s, see
|
| 1065 |
[[temp.names]]. A *type-name* or *decltype-specifier* prefixed by `~`
|
| 1066 |
denotes the destructor of the type so named; see [[expr.prim.id.dtor]].
|
| 1067 |
Within the definition of a non-static member function, an *identifier*
|
| 1068 |
that names a non-static member is transformed to a class member access
|
| 1069 |
+
expression [[class.mfct.non.static]]. — *end note*]
|
| 1070 |
+
|
| 1071 |
+
A *component name* of an *unqualified-id* U is
|
| 1072 |
+
|
| 1073 |
+
- U if it is a name or
|
| 1074 |
+
- the component name of the *template-id* or *type-name* of U, if any.
|
| 1075 |
+
|
| 1076 |
+
[*Note 2*: Other constructs that contain names to look up can have
|
| 1077 |
+
several component names
|
| 1078 |
+
[[expr.prim.id.qual]], [[dcl.type.simple]], [[dcl.type.elab]], [[dcl.mptr]], [[namespace.udecl]], [[temp.param]], [[temp.names]], [[temp.res]]. — *end note*]
|
| 1079 |
+
|
| 1080 |
+
The *terminal name* of a construct is the component name of that
|
| 1081 |
+
construct that appears lexically last.
|
| 1082 |
+
|
| 1083 |
+
The result is the entity denoted by the *unqualified-id*
|
| 1084 |
+
[[basic.lookup.unqual]]. If the *unqualified-id* appears in a
|
| 1085 |
+
*lambda-expression* at program point P and the entity is a local entity
|
| 1086 |
+
[[basic.pre]] or a variable declared by an *init-capture*
|
| 1087 |
+
[[expr.prim.lambda.capture]], then let S be the *compound-statement* of
|
| 1088 |
+
the innermost enclosing *lambda-expression* of P. If naming the entity
|
| 1089 |
+
from outside of an unevaluated operand within S would refer to an entity
|
| 1090 |
+
captured by copy in some intervening *lambda-expression*, then let E be
|
| 1091 |
+
the innermost such *lambda-expression*.
|
| 1092 |
+
|
| 1093 |
+
- If there is such a *lambda-expression* and if P is in E’s function
|
| 1094 |
+
parameter scope but not its *parameter-declaration-clause*, then the
|
| 1095 |
+
type of the expression is the type of a class member access expression
|
| 1096 |
+
[[expr.ref]] naming the non-static data member that would be declared
|
| 1097 |
+
for such a capture in the object parameter [[dcl.fct]] of the function
|
| 1098 |
+
call operator of E. \[*Note 3*: If E is not declared `mutable`, the
|
| 1099 |
type of such an identifier will typically be `const`
|
| 1100 |
qualified. — *end note*]
|
| 1101 |
+
- Otherwise (if there is no such *lambda-expression* or if P either
|
| 1102 |
+
precedes E’s function parameter scope or is in E’s
|
| 1103 |
+
*parameter-declaration-clause*), the type of the expression is the
|
| 1104 |
+
type of the result.
|
| 1105 |
|
| 1106 |
+
[*Note 4*: If the entity is a template parameter object for a template
|
|
|
|
|
|
|
| 1107 |
parameter of type `T` [[temp.param]], the type of the expression is
|
| 1108 |
`const T`. — *end note*]
|
| 1109 |
|
| 1110 |
+
[*Note 5*: The type will be adjusted as described in [[expr.type]] if
|
| 1111 |
it is cv-qualified or is a reference type. — *end note*]
|
| 1112 |
|
| 1113 |
+
The expression is an xvalue if it is move-eligible (see below); an
|
| 1114 |
+
lvalue if the entity is a function, variable, structured binding
|
| 1115 |
+
[[dcl.struct.bind]], data member, or template parameter object; and a
|
| 1116 |
+
prvalue otherwise [[basic.lval]]; it is a bit-field if the identifier
|
| 1117 |
+
designates a bit-field.
|
| 1118 |
|
| 1119 |
[*Example 1*:
|
| 1120 |
|
| 1121 |
``` cpp
|
| 1122 |
void f() {
|
| 1123 |
float x, &r = x;
|
| 1124 |
+
|
| 1125 |
+
[=]() -> decltype((x)) { // lambda returns float const& because this lambda is not mutable and
|
| 1126 |
+
// x is an lvalue
|
| 1127 |
decltype(x) y1; // y1 has type float
|
| 1128 |
+
decltype((x)) y2 = y1; // y2 has type float const&
|
|
|
|
| 1129 |
decltype(r) r1 = y1; // r1 has type float&
|
| 1130 |
decltype((r)) r2 = y2; // r2 has type float const&
|
| 1131 |
+
return y2;
|
| 1132 |
+
};
|
| 1133 |
+
|
| 1134 |
+
[=](decltype((x)) y) {
|
| 1135 |
+
decltype((x)) z = x; // OK, y has type float&, z has type float const&
|
| 1136 |
+
};
|
| 1137 |
+
|
| 1138 |
+
[=] {
|
| 1139 |
+
[](decltype((x)) y) {}; // OK, lambda takes a parameter of type float const&
|
| 1140 |
+
|
| 1141 |
+
[x=1](decltype((x)) y) {
|
| 1142 |
+
decltype((x)) z = x; // OK, y has type int&, z has type int const&
|
| 1143 |
+
};
|
| 1144 |
};
|
| 1145 |
}
|
| 1146 |
```
|
| 1147 |
|
| 1148 |
— *end example*]
|
| 1149 |
|
| 1150 |
+
An *implicitly movable entity* is a variable of automatic storage
|
| 1151 |
+
duration that is either a non-volatile object or an rvalue reference to
|
| 1152 |
+
a non-volatile object type. In the following contexts, an
|
| 1153 |
+
*id-expression* is *move-eligible*:
|
| 1154 |
+
|
| 1155 |
+
- If the *id-expression* (possibly parenthesized) is the operand of a
|
| 1156 |
+
`return` [[stmt.return]] or `co_return` [[stmt.return.coroutine]]
|
| 1157 |
+
statement, and names an implicitly movable entity declared in the body
|
| 1158 |
+
or *parameter-declaration-clause* of the innermost enclosing function
|
| 1159 |
+
or *lambda-expression*, or
|
| 1160 |
+
- if the *id-expression* (possibly parenthesized) is the operand of a
|
| 1161 |
+
*throw-expression* [[expr.throw]], and names an implicitly movable
|
| 1162 |
+
entity that belongs to a scope that does not contain the
|
| 1163 |
+
*compound-statement* of the innermost *lambda-expression*,
|
| 1164 |
+
*try-block*, or *function-try-block* (if any) whose
|
| 1165 |
+
*compound-statement* or *ctor-initializer* contains the
|
| 1166 |
+
*throw-expression*.
|
| 1167 |
+
|
| 1168 |
#### Qualified names <a id="expr.prim.id.qual">[[expr.prim.id.qual]]</a>
|
| 1169 |
|
| 1170 |
``` bnf
|
| 1171 |
qualified-id:
|
| 1172 |
nested-name-specifier templateₒₚₜ unqualified-id
|
|
|
|
| 1180 |
decltype-specifier '::'
|
| 1181 |
nested-name-specifier identifier '::'
|
| 1182 |
nested-name-specifier templateₒₚₜ simple-template-id '::'
|
| 1183 |
```
|
| 1184 |
|
| 1185 |
+
The component names of a *qualified-id* are those of its
|
| 1186 |
+
*nested-name-specifier* and *unqualified-id*. The component names of a
|
| 1187 |
+
*nested-name-specifier* are its *identifier* (if any) and those of its
|
| 1188 |
+
*type-name*, *namespace-name*, *simple-template-id*, and/or
|
| 1189 |
+
*nested-name-specifier*.
|
| 1190 |
+
|
| 1191 |
+
A *nested-name-specifier* is *declarative* if it is part of
|
| 1192 |
+
|
| 1193 |
+
- a *class-head-name*,
|
| 1194 |
+
- an *enum-head-name*,
|
| 1195 |
+
- a *qualified-id* that is the *id-expression* of a *declarator-id*, or
|
| 1196 |
+
- a declarative *nested-name-specifier*.
|
| 1197 |
+
|
| 1198 |
+
A declarative *nested-name-specifier* shall not have a
|
| 1199 |
+
*decltype-specifier*. A declaration that uses a declarative
|
| 1200 |
+
*nested-name-specifier* shall be a friend declaration or inhabit a scope
|
| 1201 |
+
that contains the entity being redeclared or specialized.
|
| 1202 |
+
|
| 1203 |
+
The *nested-name-specifier* `::` nominates the global namespace. A
|
| 1204 |
+
*nested-name-specifier* with a *decltype-specifier* nominates the type
|
| 1205 |
+
denoted by the *decltype-specifier*, which shall be a class or
|
| 1206 |
+
enumeration type. If a *nested-name-specifier* N is declarative and has
|
| 1207 |
+
a *simple-template-id* with a template argument list A that involves a
|
| 1208 |
+
template parameter, let T be the template nominated by N without A. T
|
| 1209 |
+
shall be a class template.
|
| 1210 |
+
|
| 1211 |
+
- If A is the template argument list [[temp.arg]] of the corresponding
|
| 1212 |
+
*template-head* H [[temp.mem]], N nominates the primary template of T;
|
| 1213 |
+
H shall be equivalent to the *template-head* of T [[temp.over.link]].
|
| 1214 |
+
- Otherwise, N nominates the partial specialization
|
| 1215 |
+
[[temp.spec.partial]] of T whose template argument list is equivalent
|
| 1216 |
+
to A [[temp.over.link]]; the program is ill-formed if no such partial
|
| 1217 |
+
specialization exists.
|
| 1218 |
+
|
| 1219 |
+
Any other *nested-name-specifier* nominates the entity denoted by its
|
| 1220 |
+
*type-name*, *namespace-name*, *identifier*, or *simple-template-id*. If
|
| 1221 |
+
the *nested-name-specifier* is not declarative, the entity shall not be
|
| 1222 |
+
a template.
|
| 1223 |
+
|
| 1224 |
+
A *qualified-id* shall not be of the form *nested-name-specifier*
|
| 1225 |
+
`template`ₒₚₜ `~` *decltype-specifier* nor of the form
|
| 1226 |
+
*decltype-specifier* `::` `~` *type-name*.
|
| 1227 |
+
|
| 1228 |
+
The result of a *qualified-id* Q is the entity it denotes
|
| 1229 |
+
[[basic.lookup.qual]]. The type of the expression is the type of the
|
| 1230 |
+
result. The result is an lvalue if the member is
|
| 1231 |
+
|
| 1232 |
+
- a function other than a non-static member function,
|
| 1233 |
+
- a non-static member function if Q is the operand of a unary `&`
|
| 1234 |
+
operator,
|
| 1235 |
+
- a variable,
|
| 1236 |
+
- a structured binding [[dcl.struct.bind]], or
|
| 1237 |
+
- a data member,
|
| 1238 |
+
|
| 1239 |
and a prvalue otherwise.
|
| 1240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1241 |
#### Destruction <a id="expr.prim.id.dtor">[[expr.prim.id.dtor]]</a>
|
| 1242 |
|
| 1243 |
An *id-expression* that denotes the destructor of a type `T` names the
|
| 1244 |
destructor of `T` if `T` is a class type [[class.dtor]], otherwise the
|
| 1245 |
*id-expression* is said to name a *pseudo-destructor*.
|
|
|
|
| 1247 |
If the *id-expression* names a pseudo-destructor, `T` shall be a scalar
|
| 1248 |
type and the *id-expression* shall appear as the right operand of a
|
| 1249 |
class member access [[expr.ref]] that forms the *postfix-expression* of
|
| 1250 |
a function call [[expr.call]].
|
| 1251 |
|
| 1252 |
+
[*Note 1*: Such a call ends the lifetime of the object
|
| 1253 |
+
[[expr.call]], [[basic.life]]. — *end note*]
|
| 1254 |
|
| 1255 |
[*Example 1*:
|
| 1256 |
|
| 1257 |
``` cpp
|
| 1258 |
struct C { };
|
|
|
|
| 1269 |
|
| 1270 |
— *end example*]
|
| 1271 |
|
| 1272 |
### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
|
| 1273 |
|
| 1274 |
+
#### General <a id="expr.prim.lambda.general">[[expr.prim.lambda.general]]</a>
|
| 1275 |
+
|
| 1276 |
``` bnf
|
| 1277 |
lambda-expression:
|
| 1278 |
+
lambda-introducer attribute-specifier-seqₒₚₜ lambda-declarator compound-statement
|
| 1279 |
+
lambda-introducer '<' template-parameter-list '>' requires-clauseₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1280 |
+
lambda-declarator compound-statement
|
| 1281 |
```
|
| 1282 |
|
| 1283 |
``` bnf
|
| 1284 |
lambda-introducer:
|
| 1285 |
'[' lambda-captureₒₚₜ ']'
|
| 1286 |
```
|
| 1287 |
|
| 1288 |
``` bnf
|
| 1289 |
lambda-declarator:
|
| 1290 |
+
lambda-specifier-seq noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 1291 |
+
noexcept-specifier attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 1292 |
+
trailing-return-typeₒₚₜ
|
| 1293 |
+
'(' parameter-declaration-clause ')' lambda-specifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 1294 |
+
trailing-return-typeₒₚₜ requires-clauseₒₚₜ
|
| 1295 |
+
```
|
| 1296 |
+
|
| 1297 |
+
``` bnf
|
| 1298 |
+
lambda-specifier:
|
| 1299 |
+
consteval
|
| 1300 |
+
constexpr
|
| 1301 |
+
mutable
|
| 1302 |
+
static
|
| 1303 |
+
```
|
| 1304 |
+
|
| 1305 |
+
``` bnf
|
| 1306 |
+
lambda-specifier-seq:
|
| 1307 |
+
lambda-specifier
|
| 1308 |
+
lambda-specifier lambda-specifier-seq
|
| 1309 |
```
|
| 1310 |
|
| 1311 |
A *lambda-expression* provides a concise way to create a simple function
|
| 1312 |
object.
|
| 1313 |
|
|
|
|
| 1327 |
*closure object*.
|
| 1328 |
|
| 1329 |
[*Note 1*: A closure object behaves like a function object
|
| 1330 |
[[function.objects]]. — *end note*]
|
| 1331 |
|
| 1332 |
+
An ambiguity can arise because a *requires-clause* can end in an
|
| 1333 |
+
*attribute-specifier-seq*, which collides with the
|
| 1334 |
+
*attribute-specifier-seq* in *lambda-expression*. In such cases, any
|
| 1335 |
+
attributes are treated as *attribute-specifier-seq* in
|
| 1336 |
+
*lambda-expression*.
|
| 1337 |
|
| 1338 |
+
[*Note 2*: Such ambiguous cases cannot have valid semantics because the
|
| 1339 |
+
constraint expression would not have type `bool`. — *end note*]
|
| 1340 |
+
|
| 1341 |
+
A *lambda-specifier-seq* shall contain at most one of each
|
| 1342 |
+
*lambda-specifier* and shall not contain both `constexpr` and
|
| 1343 |
+
`consteval`. If the *lambda-declarator* contains an explicit object
|
| 1344 |
+
parameter [[dcl.fct]], then no *lambda-specifier* in the
|
| 1345 |
+
*lambda-specifier-seq* shall be `mutable` or `static`. The
|
| 1346 |
+
*lambda-specifier-seq* shall not contain both `mutable` and `static`. If
|
| 1347 |
+
the *lambda-specifier-seq* contains `static`, there shall be no
|
| 1348 |
+
*lambda-capture*.
|
| 1349 |
+
|
| 1350 |
+
[*Note 3*: The trailing *requires-clause* is described in
|
| 1351 |
[[dcl.decl]]. — *end note*]
|
| 1352 |
|
| 1353 |
+
If a *lambda-declarator* does not include a
|
| 1354 |
+
*parameter-declaration-clause*, it is as if `()` were inserted at the
|
| 1355 |
+
start of the *lambda-declarator*. If the *lambda-declarator* does not
|
| 1356 |
+
include a *trailing-return-type*, it is considered to be `-> auto`.
|
| 1357 |
+
|
| 1358 |
+
[*Note 4*: In that case, the return type is deduced from `return`
|
| 1359 |
+
statements as described in [[dcl.spec.auto]]. — *end note*]
|
| 1360 |
|
| 1361 |
[*Example 2*:
|
| 1362 |
|
| 1363 |
``` cpp
|
| 1364 |
+
auto x1 = [](int i) { return i; }; // OK, return type is int
|
| 1365 |
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 1366 |
int j;
|
| 1367 |
+
auto x3 = [&]()->auto&& { return j; }; // OK, return type is int&
|
| 1368 |
```
|
| 1369 |
|
| 1370 |
— *end example*]
|
| 1371 |
|
| 1372 |
A lambda is a *generic lambda* if the *lambda-expression* has any
|
|
|
|
| 1374 |
has a *template-parameter-list*.
|
| 1375 |
|
| 1376 |
[*Example 3*:
|
| 1377 |
|
| 1378 |
``` cpp
|
| 1379 |
+
int i = [](int i, auto a) { return i; }(3, 4); // OK, a generic lambda
|
| 1380 |
+
int j = []<class T>(T t, int i) { return i; }(3, 4); // OK, a generic lambda
|
| 1381 |
```
|
| 1382 |
|
| 1383 |
— *end example*]
|
| 1384 |
|
| 1385 |
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
|
|
|
| 1418 |
call operator template is the *requires-clause* immediately following
|
| 1419 |
`<` *template-parameter-list* `>`, if any. The trailing
|
| 1420 |
*requires-clause* of the function call operator or operator template is
|
| 1421 |
the *requires-clause* of the *lambda-declarator*, if any.
|
| 1422 |
|
| 1423 |
+
[*Note 2*: The function call operator template for a generic lambda can
|
| 1424 |
+
be an abbreviated function template [[dcl.fct]]. — *end note*]
|
| 1425 |
|
| 1426 |
[*Example 1*:
|
| 1427 |
|
| 1428 |
``` cpp
|
| 1429 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
| 1430 |
bool b = glambda(3, 3.14); // OK
|
| 1431 |
|
| 1432 |
auto vglambda = [](auto printer) {
|
| 1433 |
+
return [=](auto&& ... ts) { // OK, ts is a function parameter pack
|
| 1434 |
printer(std::forward<decltype(ts)>(ts)...);
|
| 1435 |
|
| 1436 |
return [=]() {
|
| 1437 |
printer(ts ...);
|
| 1438 |
};
|
| 1439 |
};
|
| 1440 |
};
|
| 1441 |
auto p = vglambda( [](auto v1, auto v2, auto v3)
|
| 1442 |
{ std::cout << v1 << v2 << v3; } );
|
| 1443 |
+
auto q = p(1, 'a', 3.14); // OK, outputs 1a3.14
|
| 1444 |
+
q(); // OK, outputs 1a3.14
|
| 1445 |
+
|
| 1446 |
+
auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
|
| 1447 |
+
return (n <= 1) ? 1 : n * self(n-1);
|
| 1448 |
+
};
|
| 1449 |
+
std::cout << fact(5); // OK, outputs 120
|
| 1450 |
+
```
|
| 1451 |
+
|
| 1452 |
+
— *end example*]
|
| 1453 |
+
|
| 1454 |
+
Given a lambda with a *lambda-capture*, the type of the explicit object
|
| 1455 |
+
parameter, if any, of the lambda’s function call operator (possibly
|
| 1456 |
+
instantiated from a function call operator template) shall be either:
|
| 1457 |
+
|
| 1458 |
+
- the closure type,
|
| 1459 |
+
- a class type derived from the closure type, or
|
| 1460 |
+
- a reference to a possibly cv-qualified such type.
|
| 1461 |
+
|
| 1462 |
+
[*Example 2*:
|
| 1463 |
+
|
| 1464 |
+
``` cpp
|
| 1465 |
+
struct C {
|
| 1466 |
+
template <typename T>
|
| 1467 |
+
C(T);
|
| 1468 |
+
};
|
| 1469 |
+
|
| 1470 |
+
void func(int i) {
|
| 1471 |
+
int x = [=](this auto&&) { return i; }(); // OK
|
| 1472 |
+
int y = [=](this C) { return i; }(); // error
|
| 1473 |
+
int z = [](this C) { return 42; }(); // OK
|
| 1474 |
+
}
|
| 1475 |
```
|
| 1476 |
|
| 1477 |
— *end example*]
|
| 1478 |
|
| 1479 |
+
The function call operator or operator template is a static member
|
| 1480 |
+
function or static member function template [[class.static.mfct]] if the
|
| 1481 |
+
*lambda-expression*’s *parameter-declaration-clause* is followed by
|
| 1482 |
+
`static`. Otherwise, it is a non-static member function or member
|
| 1483 |
+
function template [[class.mfct.non.static]] that is declared `const`
|
| 1484 |
+
[[class.mfct.non.static]] if and only if the *lambda-expression*’s
|
| 1485 |
+
*parameter-declaration-clause* is not followed by `mutable` and the
|
| 1486 |
+
*lambda-declarator* does not contain an explicit object parameter. It is
|
| 1487 |
neither virtual nor declared `volatile`. Any *noexcept-specifier*
|
| 1488 |
specified on a *lambda-expression* applies to the corresponding function
|
| 1489 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 1490 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 1491 |
+
call operator or operator template. An *attribute-specifier-seq* in a
|
| 1492 |
+
*lambda-expression* preceding a *lambda-declarator* appertains to the
|
| 1493 |
+
corresponding function call operator or operator template. The function
|
| 1494 |
+
call operator or any given operator template specialization is a
|
| 1495 |
+
constexpr function if either the corresponding *lambda-expression*'s
|
| 1496 |
+
*parameter-declaration-clause* is followed by `constexpr` or
|
| 1497 |
+
`consteval`, or it is constexpr-suitable [[dcl.constexpr]]. It is an
|
| 1498 |
immediate function [[dcl.constexpr]] if the corresponding
|
| 1499 |
*lambda-expression*'s *parameter-declaration-clause* is followed by
|
| 1500 |
`consteval`.
|
| 1501 |
|
| 1502 |
+
[*Example 3*:
|
|
|
|
|
|
|
|
|
|
| 1503 |
|
| 1504 |
``` cpp
|
| 1505 |
auto ID = [](auto a) { return a; };
|
| 1506 |
static_assert(ID(3) == 3); // OK
|
| 1507 |
|
|
|
|
| 1512 |
static_assert(ID(NonLiteral{3}).n == 3); // error
|
| 1513 |
```
|
| 1514 |
|
| 1515 |
— *end example*]
|
| 1516 |
|
| 1517 |
+
[*Example 4*:
|
| 1518 |
|
| 1519 |
``` cpp
|
| 1520 |
auto monoid = [](auto v) { return [=] { return v; }; };
|
| 1521 |
auto add = [](auto m1) constexpr {
|
| 1522 |
auto ret = m1();
|
|
|
|
| 1541 |
static_assert(add(one)(one)() == monoid(2)()); // OK
|
| 1542 |
```
|
| 1543 |
|
| 1544 |
— *end example*]
|
| 1545 |
|
| 1546 |
+
[*Note 3*:
|
| 1547 |
|
| 1548 |
+
The function call operator or operator template can be constrained
|
| 1549 |
[[temp.constr.decl]] by a *type-constraint* [[temp.param]], a
|
| 1550 |
*requires-clause* [[temp.pre]], or a trailing *requires-clause*
|
| 1551 |
[[dcl.decl]].
|
| 1552 |
|
| 1553 |
+
[*Example 5*:
|
| 1554 |
|
| 1555 |
``` cpp
|
| 1556 |
template <typename T> concept C1 = ...;
|
| 1557 |
template <std::size_t N> concept C2 = ...;
|
| 1558 |
template <typename A, typename B> concept C3 = ...;
|
|
|
|
| 1573 |
*lambda-capture* whose constraints (if any) are satisfied has a
|
| 1574 |
conversion function to pointer to function with C++ language linkage
|
| 1575 |
[[dcl.link]] having the same parameter and return types as the closure
|
| 1576 |
type’s function call operator. The conversion is to “pointer to
|
| 1577 |
`noexcept` function” if the function call operator has a non-throwing
|
| 1578 |
+
exception specification. If the function call operator is a static
|
| 1579 |
+
member function, then the value returned by this conversion function is
|
| 1580 |
+
the address of the function call operator. Otherwise, the value returned
|
| 1581 |
+
by this conversion function is the address of a function `F` that, when
|
| 1582 |
+
invoked, has the same effect as invoking the closure type’s function
|
| 1583 |
+
call operator on a default-constructed instance of the closure type. `F`
|
| 1584 |
+
is a constexpr function if the function call operator is a constexpr
|
| 1585 |
+
function and is an immediate function if the function call operator is
|
| 1586 |
+
an immediate function.
|
| 1587 |
|
| 1588 |
For a generic lambda with no *lambda-capture*, the closure type has a
|
| 1589 |
conversion function template to pointer to function. The conversion
|
| 1590 |
function template has the same invented template parameter list, and the
|
| 1591 |
pointer to function has the same parameter types, as the function call
|
| 1592 |
operator template. The return type of the pointer to function shall
|
| 1593 |
behave as if it were a *decltype-specifier* denoting the return type of
|
| 1594 |
the corresponding function call operator template specialization.
|
| 1595 |
|
| 1596 |
+
[*Note 4*:
|
| 1597 |
|
| 1598 |
If the generic lambda has no *trailing-return-type* or the
|
| 1599 |
*trailing-return-type* contains a placeholder type, return type
|
| 1600 |
deduction of the corresponding function call operator template
|
| 1601 |
specialization has to be done. The corresponding specialization is that
|
|
|
|
| 1627 |
};
|
| 1628 |
```
|
| 1629 |
|
| 1630 |
— *end note*]
|
| 1631 |
|
| 1632 |
+
[*Example 6*:
|
| 1633 |
|
| 1634 |
``` cpp
|
| 1635 |
void f1(int (*)(int)) { }
|
| 1636 |
void f2(char (*)(int)) { }
|
| 1637 |
|
|
|
|
| 1643 |
|
| 1644 |
auto glambda = [](auto a) { return a; };
|
| 1645 |
f1(glambda); // OK
|
| 1646 |
f2(glambda); // error: ID is not convertible
|
| 1647 |
g(glambda); // error: ambiguous
|
| 1648 |
+
h(glambda); // OK, calls #3 since it is convertible from ID
|
| 1649 |
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 1650 |
```
|
| 1651 |
|
| 1652 |
— *end example*]
|
| 1653 |
|
| 1654 |
+
If the function call operator template is a static member function
|
| 1655 |
+
template, then the value returned by any given specialization of this
|
| 1656 |
+
conversion function template is the address of the corresponding
|
| 1657 |
+
function call operator template specialization. Otherwise, the value
|
| 1658 |
+
returned by any given specialization of this conversion function
|
| 1659 |
+
template is the address of a function `F` that, when invoked, has the
|
| 1660 |
+
same effect as invoking the generic lambda’s corresponding function call
|
| 1661 |
+
operator template specialization on a default-constructed instance of
|
| 1662 |
+
the closure type. `F` is a constexpr function if the corresponding
|
| 1663 |
+
specialization is a constexpr function and `F` is an immediate function
|
| 1664 |
+
if the function call operator template specialization is an immediate
|
| 1665 |
+
function.
|
| 1666 |
|
| 1667 |
+
[*Note 5*: This will result in the implicit instantiation of the
|
| 1668 |
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 1669 |
parameter types are required to match the return type and parameter
|
| 1670 |
types of the pointer to function. — *end note*]
|
| 1671 |
|
| 1672 |
+
[*Example 7*:
|
| 1673 |
|
| 1674 |
``` cpp
|
| 1675 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 1676 |
+
int (*GL_int)(int) = GL; // OK, through conversion function template
|
| 1677 |
+
GL_int(3); // OK, same as GL(3)
|
| 1678 |
```
|
| 1679 |
|
| 1680 |
— *end example*]
|
| 1681 |
|
| 1682 |
The conversion function or conversion function template is public,
|
| 1683 |
constexpr, non-virtual, non-explicit, const, and has a non-throwing
|
| 1684 |
exception specification [[except.spec]].
|
| 1685 |
|
| 1686 |
+
[*Example 8*:
|
| 1687 |
|
| 1688 |
``` cpp
|
| 1689 |
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 1690 |
auto C = [](auto a) { return a; };
|
| 1691 |
|
|
|
|
| 1697 |
```
|
| 1698 |
|
| 1699 |
— *end example*]
|
| 1700 |
|
| 1701 |
The *lambda-expression*’s *compound-statement* yields the
|
| 1702 |
+
*function-body* [[dcl.fct.def]] of the function call operator, but it is
|
| 1703 |
+
not within the scope of the closure type.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1704 |
|
| 1705 |
+
[*Example 9*:
|
| 1706 |
|
| 1707 |
``` cpp
|
| 1708 |
struct S1 {
|
| 1709 |
int x, y;
|
| 1710 |
int operator()(int);
|
|
|
|
| 1729 |
constructor and a defaulted move constructor [[class.copy.ctor]]. It has
|
| 1730 |
a deleted copy assignment operator if the *lambda-expression* has a
|
| 1731 |
*lambda-capture* and defaulted copy and move assignment operators
|
| 1732 |
otherwise [[class.copy.assign]].
|
| 1733 |
|
| 1734 |
+
[*Note 6*: These special member functions are implicitly defined as
|
| 1735 |
+
usual, which can result in them being defined as deleted. — *end note*]
|
| 1736 |
|
| 1737 |
The closure type associated with a *lambda-expression* has an
|
| 1738 |
implicitly-declared destructor [[class.dtor]].
|
| 1739 |
|
| 1740 |
A member of a closure type shall not be explicitly instantiated
|
|
|
|
| 1780 |
init-capture:
|
| 1781 |
'...'ₒₚₜ identifier initializer
|
| 1782 |
'&' '...'ₒₚₜ identifier initializer
|
| 1783 |
```
|
| 1784 |
|
| 1785 |
+
The body of a *lambda-expression* may refer to local entities of
|
| 1786 |
+
enclosing block scopes by capturing those entities, as described below.
|
|
|
|
| 1787 |
|
| 1788 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 1789 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 1790 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 1791 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
|
|
|
| 1818 |
*simple-capture* in its *lambda-introducer* unless its innermost
|
| 1819 |
enclosing scope is a block scope [[basic.scope.block]] or it appears
|
| 1820 |
within a default member initializer and its innermost enclosing scope is
|
| 1821 |
the corresponding class scope [[basic.scope.class]].
|
| 1822 |
|
| 1823 |
+
The *identifier* in a *simple-capture* shall denote a local entity
|
| 1824 |
+
[[basic.lookup.unqual]], [[basic.pre]]. The *simple-capture*s `this` and
|
|
|
|
| 1825 |
`* this` denote the local entity `*this`. An entity that is designated
|
| 1826 |
by a *simple-capture* is said to be *explicitly captured*.
|
| 1827 |
|
| 1828 |
+
If an *identifier* in a *capture* appears as the *declarator-id* of a
|
| 1829 |
+
parameter of the *lambda-declarator*’s *parameter-declaration-clause* or
|
| 1830 |
+
as the name of a template parameter of the *lambda-expression*’s
|
| 1831 |
+
*template-parameter-list*, the program is ill-formed.
|
| 1832 |
|
| 1833 |
[*Example 2*:
|
| 1834 |
|
| 1835 |
``` cpp
|
| 1836 |
void f() {
|
| 1837 |
int x = 0;
|
| 1838 |
+
auto g = [x](int x) { return 0; }; // error: parameter and capture have the same name
|
| 1839 |
+
auto h = [y = 0]<typename y>(y) { return 0; }; // error: template parameter and capture
|
| 1840 |
+
// have the same name
|
| 1841 |
}
|
| 1842 |
```
|
| 1843 |
|
| 1844 |
— *end example*]
|
| 1845 |
|
| 1846 |
+
An *init-capture* inhabits the lambda scope [[basic.scope.lambda]] of
|
| 1847 |
+
the *lambda-expression*. An *init-capture* without ellipsis behaves as
|
| 1848 |
+
if it declares and explicitly captures a variable of the form “`auto`
|
| 1849 |
+
*init-capture* `;`”, except that:
|
| 1850 |
|
| 1851 |
- if the capture is by copy (see below), the non-static data member
|
| 1852 |
declared for the capture and the variable are treated as two different
|
| 1853 |
ways of referring to the same object, which has the lifetime of the
|
| 1854 |
non-static data member, and no additional copy and destruction is
|
|
|
|
| 1867 |
auto y = [&r = x, x = x+1]()->int {
|
| 1868 |
r += 2;
|
| 1869 |
return x+2;
|
| 1870 |
}(); // Updates ::x to 6, and initializes y to 7.
|
| 1871 |
|
| 1872 |
+
auto z = [a = 42](int a) { return 1; }; // error: parameter and conceptual local variable have the same name
|
| 1873 |
+
auto counter = [i=0]() mutable -> decltype(i) { // OK, returns int
|
| 1874 |
+
return i++;
|
| 1875 |
+
};
|
| 1876 |
```
|
| 1877 |
|
| 1878 |
— *end example*]
|
| 1879 |
|
| 1880 |
For the purposes of lambda capture, an expression potentially references
|
|
|
|
| 1888 |
*id-expression*. — *end note*]
|
| 1889 |
- A `this` expression potentially references `*this`.
|
| 1890 |
- A *lambda-expression* potentially references the local entities named
|
| 1891 |
by its *simple-capture*s.
|
| 1892 |
|
| 1893 |
+
If an expression potentially references a local entity within a scope in
|
| 1894 |
+
which it is odr-usable [[basic.def.odr]], and the expression would be
|
| 1895 |
+
potentially evaluated if the effect of any enclosing `typeid`
|
| 1896 |
expressions [[expr.typeid]] were ignored, the entity is said to be
|
| 1897 |
*implicitly captured* by each intervening *lambda-expression* with an
|
| 1898 |
associated *capture-default* that does not explicitly capture it. The
|
| 1899 |
implicit capture of `*this` is deprecated when the *capture-default* is
|
| 1900 |
`=`; see [[depr.capture.this]].
|
|
|
|
| 1905 |
void f(int, const int (&)[2] = {}); // #1
|
| 1906 |
void f(const int&, const int (&)[1]); // #2
|
| 1907 |
void test() {
|
| 1908 |
const int x = 17;
|
| 1909 |
auto g = [](auto a) {
|
| 1910 |
+
f(x); // OK, calls #1, does not capture x
|
| 1911 |
};
|
| 1912 |
|
| 1913 |
auto g1 = [=](auto a) {
|
| 1914 |
+
f(x); // OK, calls #1, captures x
|
| 1915 |
};
|
| 1916 |
|
| 1917 |
auto g2 = [=](auto a) {
|
| 1918 |
int selector[sizeof(a) == 1 ? 1 : 2]{};
|
| 1919 |
+
f(x, selector); // OK, captures x, can call #1 or #2
|
| 1920 |
};
|
| 1921 |
|
| 1922 |
auto g3 = [=](auto a) {
|
| 1923 |
typeid(a + x); // captures x regardless of whether a + x is an unevaluated operand
|
| 1924 |
};
|
| 1925 |
}
|
| 1926 |
```
|
| 1927 |
|
| 1928 |
+
Within `g1`, an implementation can optimize away the capture of `x` as
|
| 1929 |
it is not odr-used.
|
| 1930 |
|
| 1931 |
— *end example*]
|
| 1932 |
|
| 1933 |
[*Note 4*:
|
| 1934 |
|
| 1935 |
The set of captured entities is determined syntactically, and entities
|
| 1936 |
+
are implicitly captured even if the expression denoting a local entity
|
| 1937 |
+
is within a discarded statement [[stmt.if]].
|
| 1938 |
|
| 1939 |
[*Example 5*:
|
| 1940 |
|
| 1941 |
``` cpp
|
| 1942 |
template<bool B>
|
|
|
|
| 1952 |
— *end example*]
|
| 1953 |
|
| 1954 |
— *end note*]
|
| 1955 |
|
| 1956 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 1957 |
+
entity captured by a *lambda-expression* is odr-used [[term.odr.use]] by
|
| 1958 |
+
the *lambda-expression*.
|
| 1959 |
|
| 1960 |
[*Note 5*: As a consequence, if a *lambda-expression* explicitly
|
| 1961 |
captures an entity that is not odr-usable, the program is ill-formed
|
| 1962 |
[[basic.def.odr]]. — *end note*]
|
| 1963 |
|
|
|
|
| 1967 |
void f1(int i) {
|
| 1968 |
int const N = 20;
|
| 1969 |
auto m1 = [=]{
|
| 1970 |
int const M = 30;
|
| 1971 |
auto m2 = [i]{
|
| 1972 |
+
int x[N][M]; // OK, N and M are not odr-used
|
| 1973 |
+
x[0][0] = i; // OK, i is explicitly captured by m2 and implicitly captured by m1
|
| 1974 |
};
|
| 1975 |
};
|
| 1976 |
struct s1 {
|
| 1977 |
int f;
|
| 1978 |
void work(int n) {
|
| 1979 |
int m = n*n;
|
| 1980 |
int j = 40;
|
| 1981 |
auto m3 = [this,m] {
|
| 1982 |
auto m4 = [&,j] { // error: j not odr-usable due to intervening lambda m3
|
| 1983 |
int x = n; // error: n is odr-used but not odr-usable due to intervening lambda m3
|
| 1984 |
+
x += m; // OK, m implicitly captured by m4 and explicitly captured by m3
|
| 1985 |
x += i; // error: i is odr-used but not odr-usable
|
| 1986 |
// due to intervening function and class scopes
|
| 1987 |
+
x += f; // OK, this captured implicitly by m4 and explicitly by m3
|
| 1988 |
};
|
| 1989 |
};
|
| 1990 |
}
|
| 1991 |
};
|
| 1992 |
}
|
|
|
|
| 2049 |
referenced function type if the entity is a reference to a function, or
|
| 2050 |
the type of the corresponding captured entity otherwise. A member of an
|
| 2051 |
anonymous union shall not be captured by copy.
|
| 2052 |
|
| 2053 |
Every *id-expression* within the *compound-statement* of a
|
| 2054 |
+
*lambda-expression* that is an odr-use [[term.odr.use]] of an entity
|
| 2055 |
captured by copy is transformed into an access to the corresponding
|
| 2056 |
unnamed data member of the closure type.
|
| 2057 |
|
| 2058 |
[*Note 7*: An *id-expression* that is not an odr-use refers to the
|
| 2059 |
original entity, never to a member of the closure type. However, such an
|
|
|
|
| 2069 |
``` cpp
|
| 2070 |
void f(const int*);
|
| 2071 |
void g() {
|
| 2072 |
const int N = 10;
|
| 2073 |
[=] {
|
| 2074 |
+
int arr[N]; // OK, not an odr-use, refers to automatic variable
|
| 2075 |
+
f(&N); // OK, causes N to be captured; &N points to
|
| 2076 |
// the corresponding member of the closure type
|
| 2077 |
};
|
| 2078 |
}
|
| 2079 |
```
|
| 2080 |
|
|
|
|
| 2122 |
|
| 2123 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 2124 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 2125 |
`m2`’s capture is transformed as follows:
|
| 2126 |
|
| 2127 |
+
- If `m1` captures the entity by copy, `m2` captures the corresponding
|
| 2128 |
+
non-static data member of `m1`’s closure type; if `m1` is not
|
| 2129 |
+
`mutable`, the non-static data member is considered to be
|
| 2130 |
+
const-qualified.
|
| 2131 |
+
- If `m1` captures the entity by reference, `m2` captures the same
|
| 2132 |
entity captured by `m1`.
|
| 2133 |
|
| 2134 |
[*Example 11*:
|
| 2135 |
|
| 2136 |
The nested *lambda-expression*s and invocations below will output
|
|
|
|
| 2171 |
corresponding *lambda-expression* after the lifetime of the entity has
|
| 2172 |
ended is likely to result in undefined behavior. — *end note*]
|
| 2173 |
|
| 2174 |
A *simple-capture* containing an ellipsis is a pack expansion
|
| 2175 |
[[temp.variadic]]. An *init-capture* containing an ellipsis is a pack
|
| 2176 |
+
expansion that declares an *init-capture* pack [[temp.variadic]].
|
|
|
|
| 2177 |
|
| 2178 |
[*Example 12*:
|
| 2179 |
|
| 2180 |
``` cpp
|
| 2181 |
template<class... Args>
|
|
|
|
| 2242 |
|
| 2243 |
— *end example*]
|
| 2244 |
|
| 2245 |
### Requires expressions <a id="expr.prim.req">[[expr.prim.req]]</a>
|
| 2246 |
|
| 2247 |
+
#### General <a id="expr.prim.req.general">[[expr.prim.req.general]]</a>
|
| 2248 |
+
|
| 2249 |
A *requires-expression* provides a concise way to express requirements
|
| 2250 |
on template arguments that can be checked by name lookup
|
| 2251 |
[[basic.lookup]] or by checking properties of types and expressions.
|
| 2252 |
|
| 2253 |
``` bnf
|
|
|
|
| 2255 |
requires requirement-parameter-listₒₚₜ requirement-body
|
| 2256 |
```
|
| 2257 |
|
| 2258 |
``` bnf
|
| 2259 |
requirement-parameter-list:
|
| 2260 |
+
'(' parameter-declaration-clause ')'
|
| 2261 |
```
|
| 2262 |
|
| 2263 |
``` bnf
|
| 2264 |
requirement-body:
|
| 2265 |
'{' requirement-seq '}'
|
| 2266 |
```
|
| 2267 |
|
| 2268 |
``` bnf
|
| 2269 |
requirement-seq:
|
| 2270 |
requirement
|
| 2271 |
+
requirement requirement-seq
|
| 2272 |
```
|
| 2273 |
|
| 2274 |
``` bnf
|
| 2275 |
requirement:
|
| 2276 |
simple-requirement
|
|
|
|
| 2279 |
nested-requirement
|
| 2280 |
```
|
| 2281 |
|
| 2282 |
A *requires-expression* is a prvalue of type `bool` whose value is
|
| 2283 |
described below. Expressions appearing within a *requirement-body* are
|
| 2284 |
+
unevaluated operands [[term.unevaluated.operand]].
|
| 2285 |
|
| 2286 |
[*Example 1*:
|
| 2287 |
|
| 2288 |
A common use of *requires-expression*s is to define requirements in
|
| 2289 |
concepts such as the one below:
|
|
|
|
| 2311 |
|
| 2312 |
— *end example*]
|
| 2313 |
|
| 2314 |
A *requires-expression* may introduce local parameters using a
|
| 2315 |
*parameter-declaration-clause* [[dcl.fct]]. A local parameter of a
|
| 2316 |
+
*requires-expression* shall not have a default argument. These
|
|
|
|
|
|
|
| 2317 |
parameters have no linkage, storage, or lifetime; they are only used as
|
| 2318 |
notation for the purpose of defining *requirement*s. The
|
| 2319 |
*parameter-declaration-clause* of a *requirement-parameter-list* shall
|
| 2320 |
not terminate with an ellipsis.
|
| 2321 |
|
|
|
|
| 2328 |
};
|
| 2329 |
```
|
| 2330 |
|
| 2331 |
— *end example*]
|
| 2332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2333 |
The substitution of template arguments into a *requires-expression* may
|
| 2334 |
result in the formation of invalid types or expressions in its
|
| 2335 |
*requirement*s or the violation of the semantic constraints of those
|
| 2336 |
*requirement*s. In such cases, the *requires-expression* evaluates to
|
| 2337 |
`false`; it does not cause the program to be ill-formed. The
|
|
|
|
| 2370 |
|
| 2371 |
A *simple-requirement* asserts the validity of an *expression*.
|
| 2372 |
|
| 2373 |
[*Note 1*: The enclosing *requires-expression* will evaluate to `false`
|
| 2374 |
if substitution of template arguments into the *expression* fails. The
|
| 2375 |
+
*expression* is an unevaluated operand
|
| 2376 |
+
[[term.unevaluated.operand]]. — *end note*]
|
| 2377 |
|
| 2378 |
[*Example 1*:
|
| 2379 |
|
| 2380 |
``` cpp
|
| 2381 |
template<typename T> concept C =
|
|
|
|
| 2410 |
template<typename T, typename T::type = 0> struct S;
|
| 2411 |
template<typename T> using Ref = T&;
|
| 2412 |
|
| 2413 |
template<typename T> concept C = requires {
|
| 2414 |
typename T::inner; // required nested member name
|
| 2415 |
+
typename S<T>; // required valid[temp.names] template-id;
|
| 2416 |
+
// fails if T::type does not exist as a type to which 0 can be implicitly converted
|
| 2417 |
typename Ref<T>; // required alias template substitution, fails if T is void
|
| 2418 |
};
|
| 2419 |
```
|
| 2420 |
|
| 2421 |
— *end example*]
|
| 2422 |
|
| 2423 |
A *type-requirement* that names a class template specialization does not
|
| 2424 |
+
require that type to be complete [[term.incomplete.type]].
|
| 2425 |
|
| 2426 |
#### Compound requirements <a id="expr.prim.req.compound">[[expr.prim.req.compound]]</a>
|
| 2427 |
|
| 2428 |
``` bnf
|
| 2429 |
compound-requirement:
|
|
|
|
| 2446 |
- If the *return-type-requirement* is present, then:
|
| 2447 |
- Substitution of template arguments (if any) into the
|
| 2448 |
*return-type-requirement* is performed.
|
| 2449 |
- The immediately-declared constraint [[temp.param]] of the
|
| 2450 |
*type-constraint* for `decltype((E))` shall be satisfied.
|
| 2451 |
+
|
| 2452 |
\[*Example 1*:
|
| 2453 |
Given concepts `C` and `D`,
|
| 2454 |
``` cpp
|
| 2455 |
requires {
|
| 2456 |
{ E1 } -> C;
|
|
|
|
| 2529 |
`D<T>` is satisfied if `sizeof(decltype (+t)) == 1`
|
| 2530 |
[[temp.constr.atomic]].
|
| 2531 |
|
| 2532 |
— *end example*]
|
| 2533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2534 |
## Compound expressions <a id="expr.compound">[[expr.compound]]</a>
|
| 2535 |
|
| 2536 |
### Postfix expressions <a id="expr.post">[[expr.post]]</a>
|
| 2537 |
|
| 2538 |
+
#### General <a id="expr.post.general">[[expr.post.general]]</a>
|
| 2539 |
+
|
| 2540 |
Postfix expressions group left-to-right.
|
| 2541 |
|
| 2542 |
``` bnf
|
| 2543 |
postfix-expression:
|
| 2544 |
primary-expression
|
| 2545 |
+
postfix-expression '[' expression-listₒₚₜ ']'
|
| 2546 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 2547 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 2548 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 2549 |
simple-type-specifier braced-init-list
|
| 2550 |
typename-specifier braced-init-list
|
| 2551 |
postfix-expression '.' 'template'ₒₚₜ id-expression
|
| 2552 |
postfix-expression '->' 'template'ₒₚₜ id-expression
|
| 2553 |
postfix-expression '++'
|
| 2554 |
+
postfix-expression '--'
|
| 2555 |
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 2556 |
static_cast '<' type-id '>' '(' expression ')'
|
| 2557 |
reinterpret_cast '<' type-id '>' '(' expression ')'
|
| 2558 |
const_cast '<' type-id '>' '(' expression ')'
|
| 2559 |
typeid '(' expression ')'
|
|
|
|
| 2564 |
expression-list:
|
| 2565 |
initializer-list
|
| 2566 |
```
|
| 2567 |
|
| 2568 |
[*Note 1*: The `>` token following the *type-id* in a `dynamic_cast`,
|
| 2569 |
+
`static_cast`, `reinterpret_cast`, or `const_cast` can be the product of
|
| 2570 |
+
replacing a `>>` token by two consecutive `>` tokens
|
| 2571 |
[[temp.names]]. — *end note*]
|
| 2572 |
|
| 2573 |
#### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 2574 |
|
| 2575 |
+
A *subscript expression* is a postfix expression followed by square
|
| 2576 |
+
brackets containing a possibly empty, comma-separated list of
|
| 2577 |
+
*initializer-clause*s that constitute the arguments to the subscript
|
| 2578 |
+
operator. The *postfix-expression* and the initialization of the object
|
| 2579 |
+
parameter of any applicable subscript operator function is sequenced
|
| 2580 |
+
before each expression in the *expression-list* and also before any
|
| 2581 |
+
default argument. The initialization of a non-object parameter of a
|
| 2582 |
+
subscript operator function `S` [[over.sub]], including every associated
|
| 2583 |
+
value computation and side effect, is indeterminately sequenced with
|
| 2584 |
+
respect to that of any other non-object parameter of `S`.
|
| 2585 |
|
| 2586 |
+
With the built-in subscript operator, an *expression-list* shall be
|
| 2587 |
+
present, consisting of a single *assignment-expression*. One of the
|
| 2588 |
+
expressions shall be a glvalue of type “array of `T`” or a prvalue of
|
| 2589 |
+
type “pointer to `T`” and the other shall be a prvalue of unscoped
|
| 2590 |
+
enumeration or integral type. The result is of type “`T`”. The type
|
| 2591 |
+
“`T`” shall be a completely-defined object type.[^11]
|
| 2592 |
|
| 2593 |
+
The expression `E1[E2]` is identical (by definition) to `*((E1)+(E2))`,
|
| 2594 |
+
except that in the case of an array operand, the result is an lvalue if
|
| 2595 |
+
that operand is an lvalue and an xvalue otherwise.
|
| 2596 |
+
|
| 2597 |
+
[*Note 1*: Despite its asymmetric appearance, subscripting is a
|
| 2598 |
commutative operation except for sequencing. See [[expr.unary]] and
|
| 2599 |
[[expr.add]] for details of `*` and `+` and [[dcl.array]] for details
|
| 2600 |
of array types. — *end note*]
|
| 2601 |
|
|
|
|
|
|
|
|
|
|
| 2602 |
#### Function call <a id="expr.call">[[expr.call]]</a>
|
| 2603 |
|
| 2604 |
A function call is a postfix expression followed by parentheses
|
| 2605 |
containing a possibly empty, comma-separated list of
|
| 2606 |
*initializer-clause*s which constitute the arguments to the function.
|
|
|
|
| 2614 |
function, the postfix expression shall either be an lvalue that refers
|
| 2615 |
to a function (in which case the function-to-pointer standard conversion
|
| 2616 |
[[conv.func]] is suppressed on the postfix expression), or have function
|
| 2617 |
pointer type.
|
| 2618 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2619 |
If the selected function is non-virtual, or if the *id-expression* in
|
| 2620 |
the class member access expression is a *qualified-id*, that function is
|
| 2621 |
called. Otherwise, its final overrider [[class.virtual]] in the dynamic
|
| 2622 |
type of the object expression is called; such a call is referred to as a
|
| 2623 |
*virtual function call*.
|
| 2624 |
|
| 2625 |
+
[*Note 2*: The dynamic type is the type of the object referred to by
|
| 2626 |
the current value of the object expression. [[class.cdtor]] describes
|
| 2627 |
the behavior of virtual function calls when the object expression refers
|
| 2628 |
to an object under construction or destruction. — *end note*]
|
| 2629 |
|
| 2630 |
+
[*Note 3*: If a function or member function name is used, and name
|
| 2631 |
lookup [[basic.lookup]] does not find a declaration of that name, the
|
| 2632 |
program is ill-formed. No function is implicitly declared by such a
|
| 2633 |
call. — *end note*]
|
| 2634 |
|
| 2635 |
If the *postfix-expression* names a destructor or pseudo-destructor
|
| 2636 |
[[expr.prim.id.dtor]], the type of the function call expression is
|
| 2637 |
`void`; otherwise, the type of the function call expression is the
|
| 2638 |
return type of the statically chosen function (i.e., ignoring the
|
| 2639 |
`virtual` keyword), even if the type of the function actually called is
|
| 2640 |
+
different. If the *postfix-expression* names a pseudo-destructor (in
|
|
|
|
| 2641 |
which case the *postfix-expression* is a possibly-parenthesized class
|
| 2642 |
member access), the function call destroys the object of scalar type
|
| 2643 |
+
denoted by the object expression of the class member access
|
| 2644 |
+
[[expr.ref]], [[basic.life]].
|
| 2645 |
|
| 2646 |
+
Calling a function through an expression whose function type `E` is
|
| 2647 |
+
different from the function type `F` of the called function’s definition
|
| 2648 |
+
results in undefined behavior unless the type “pointer to `F`” can be
|
| 2649 |
+
converted to the type “pointer to `E`” via a function pointer conversion
|
| 2650 |
+
[[conv.fctptr]].
|
| 2651 |
|
| 2652 |
+
[*Note 4*: The exception applies when the expression has the type of a
|
| 2653 |
+
potentially-throwing function, but the called function has a
|
| 2654 |
+
non-throwing exception specification, and the function types are
|
| 2655 |
+
otherwise the same. — *end note*]
|
| 2656 |
+
|
| 2657 |
+
When a function is called, each parameter [[dcl.fct]] is initialized
|
| 2658 |
+
[[dcl.init]], [[class.copy.ctor]] with its corresponding argument. If
|
| 2659 |
+
the function is an explicit object member function and there is an
|
| 2660 |
+
implied object argument [[over.call.func]], the list of provided
|
| 2661 |
+
arguments is preceded by the implied object argument for the purposes of
|
| 2662 |
+
this correspondence. If there is no corresponding argument, the default
|
| 2663 |
+
argument for the parameter is used.
|
| 2664 |
|
| 2665 |
[*Example 1*:
|
| 2666 |
|
| 2667 |
``` cpp
|
| 2668 |
template<typename ...T> int f(int n = 0, T ...t);
|
| 2669 |
int x = f<int>(); // error: no argument for second function parameter
|
| 2670 |
```
|
| 2671 |
|
| 2672 |
— *end example*]
|
| 2673 |
|
| 2674 |
+
If the function is an implicit object member function, the `this`
|
| 2675 |
+
parameter of the function [[expr.prim.this]] is initialized with a
|
| 2676 |
+
pointer to the object of the call, converted as if by an explicit type
|
| 2677 |
+
conversion [[expr.cast]].
|
| 2678 |
|
| 2679 |
[*Note 5*: There is no access or ambiguity checking on this conversion;
|
| 2680 |
the access checking and disambiguation are done as part of the (possibly
|
| 2681 |
implicit) class member access operator. See [[class.member.lookup]],
|
| 2682 |
[[class.access.base]], and [[expr.ref]]. — *end note*]
|
|
|
|
| 2694 |
parameter occurs within the context of the calling function.
|
| 2695 |
|
| 2696 |
[*Example 2*: The access of the constructor, conversion functions or
|
| 2697 |
destructor is checked at the point of call in the calling function. If a
|
| 2698 |
constructor or destructor for a function parameter throws an exception,
|
| 2699 |
+
the search for a handler starts in the calling function; in particular,
|
| 2700 |
+
if the function called has a *function-try-block* [[except.pre]] with a
|
| 2701 |
+
handler that can handle the exception, this handler is not
|
| 2702 |
+
considered. — *end example*]
|
| 2703 |
|
| 2704 |
The *postfix-expression* is sequenced before each *expression* in the
|
| 2705 |
*expression-list* and any default argument. The initialization of a
|
| 2706 |
parameter, including every associated value computation and side effect,
|
| 2707 |
is indeterminately sequenced with respect to that of any other
|
|
|
|
| 2757 |
parameters, but these changes cannot affect the values of the arguments
|
| 2758 |
except where a parameter is of a reference type [[dcl.ref]]; if the
|
| 2759 |
reference is to a const-qualified type, `const_cast` is required to be
|
| 2760 |
used to cast away the constness in order to modify the argument’s value.
|
| 2761 |
Where a parameter is of `const` reference type a temporary object is
|
| 2762 |
+
introduced if needed
|
| 2763 |
+
[[dcl.type]], [[lex.literal]], [[lex.string]], [[dcl.array]], [[class.temporary]].
|
| 2764 |
+
In addition, it is possible to modify the values of non-constant objects
|
| 2765 |
+
through pointer parameters. — *end note*]
|
| 2766 |
|
| 2767 |
A function can be declared to accept fewer arguments (by declaring
|
| 2768 |
default arguments [[dcl.fct.default]]) or more arguments (by using the
|
| 2769 |
ellipsis, `...`, or a function parameter pack [[dcl.fct]]) than the
|
| 2770 |
number of parameters in the function definition [[dcl.fct.def]].
|
|
|
|
| 2814 |
by a *braced-init-list* (the initializer) constructs a value of the
|
| 2815 |
specified type given the initializer. If the type is a placeholder for a
|
| 2816 |
deduced class type, it is replaced by the return type of the function
|
| 2817 |
selected by overload resolution for class template deduction
|
| 2818 |
[[over.match.class.deduct]] for the remainder of this subclause.
|
| 2819 |
+
Otherwise, if the type contains a placeholder type, it is replaced by
|
| 2820 |
+
the type determined by placeholder type deduction
|
| 2821 |
+
[[dcl.type.auto.deduct]].
|
| 2822 |
+
|
| 2823 |
+
[*Example 1*:
|
| 2824 |
+
|
| 2825 |
+
``` cpp
|
| 2826 |
+
struct A {};
|
| 2827 |
+
void f(A&); // #1
|
| 2828 |
+
void f(A&&); // #2
|
| 2829 |
+
A& g();
|
| 2830 |
+
void h() {
|
| 2831 |
+
f(g()); // calls #1
|
| 2832 |
+
f(A(g())); // calls #2 with a temporary object
|
| 2833 |
+
f(auto(g())); // calls #2 with a temporary object
|
| 2834 |
+
}
|
| 2835 |
+
```
|
| 2836 |
+
|
| 2837 |
+
— *end example*]
|
| 2838 |
|
| 2839 |
If the initializer is a parenthesized single expression, the type
|
| 2840 |
conversion expression is equivalent to the corresponding cast expression
|
| 2841 |
[[expr.cast]]. Otherwise, if the type is cv `void` and the initializer
|
| 2842 |
is `()` or `{}` (after pack expansion, if any), the expression is a
|
| 2843 |
+
prvalue of type `void` that performs no initialization. Otherwise, the
|
| 2844 |
+
expression is a prvalue of the specified type whose result object is
|
| 2845 |
+
direct-initialized [[dcl.init]] with the initializer. If the initializer
|
| 2846 |
+
is a parenthesized optional *expression-list*, the specified type shall
|
| 2847 |
+
not be an array type.
|
| 2848 |
|
| 2849 |
#### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 2850 |
|
| 2851 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 2852 |
+
followed by the keyword `template`, and then followed by an
|
| 2853 |
+
*id-expression*, is a postfix expression. The postfix expression before
|
| 2854 |
+
the dot or arrow is evaluated;[^12]
|
| 2855 |
+
|
| 2856 |
+
the result of that evaluation, together with the *id-expression*,
|
| 2857 |
+
determines the result of the entire postfix expression.
|
| 2858 |
+
|
| 2859 |
+
[*Note 1*: If the keyword `template` is used, the following unqualified
|
| 2860 |
+
name is considered to refer to a template [[temp.names]]. If a
|
| 2861 |
+
*simple-template-id* results and is followed by a `::`, the
|
| 2862 |
+
*id-expression* is a *qualified-id*. — *end note*]
|
| 2863 |
|
| 2864 |
For the first option (dot) the first expression shall be a glvalue. For
|
| 2865 |
the second option (arrow) the first expression shall be a prvalue having
|
| 2866 |
pointer type. The expression `E1->E2` is converted to the equivalent
|
| 2867 |
form `(*(E1)).E2`; the remainder of [[expr.ref]] will address only the
|
| 2868 |
first option (dot).[^13]
|
| 2869 |
|
| 2870 |
Abbreviating *postfix-expression*`.`*id-expression* as `E1.E2`, `E1` is
|
| 2871 |
called the *object expression*. If the object expression is of scalar
|
| 2872 |
type, `E2` shall name the pseudo-destructor of that same type (ignoring
|
| 2873 |
+
cv-qualifications) and `E1.E2` is a prvalue of type “function of ()
|
| 2874 |
returning `void`”.
|
| 2875 |
|
| 2876 |
+
[*Note 2*: This value can only be used for a notional function call
|
| 2877 |
[[expr.prim.id.dtor]]. — *end note*]
|
| 2878 |
|
| 2879 |
Otherwise, the object expression shall be of class type. The class type
|
| 2880 |
shall be complete unless the class member access appears in the
|
| 2881 |
definition of that class.
|
| 2882 |
|
| 2883 |
+
[*Note 3*: The program is ill-formed if the result differs from that
|
| 2884 |
+
when the class is complete [[class.member.lookup]]. — *end note*]
|
|
|
|
| 2885 |
|
| 2886 |
+
[*Note 4*: [[basic.lookup.qual]] describes how names are looked up
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2887 |
after the `.` and `->` operators. — *end note*]
|
| 2888 |
|
| 2889 |
If `E2` is a bit-field, `E1.E2` is a bit-field. The type and value
|
| 2890 |
category of `E1.E2` are determined as follows. In the remainder of
|
| 2891 |
[[expr.ref]], *cq* represents either `const` or the absence of `const`
|
| 2892 |
and *vq* represents either `volatile` or the absence of `volatile`. *cv*
|
| 2893 |
represents an arbitrary set of cv-qualifiers, as defined in
|
| 2894 |
[[basic.type.qualifier]].
|
| 2895 |
|
| 2896 |
If `E2` is declared to have type “reference to `T`”, then `E1.E2` is an
|
| 2897 |
+
lvalue of type `T`. If `E2` is a static data member, `E1.E2` designates
|
| 2898 |
+
the object or function to which the reference is bound, otherwise
|
| 2899 |
+
`E1.E2` designates the object or function to which the corresponding
|
| 2900 |
+
reference member of `E1` is bound. Otherwise, one of the following rules
|
| 2901 |
+
applies.
|
| 2902 |
|
| 2903 |
- If `E2` is a static data member and the type of `E2` is `T`, then
|
| 2904 |
`E1.E2` is an lvalue; the expression designates the named member of
|
| 2905 |
the class. The type of `E1.E2` is `T`.
|
| 2906 |
- If `E2` is a non-static data member and the type of `E1` is “*cq1 vq1*
|
|
|
|
| 2913 |
*cq12* stand for the “union” of *cq1* and *cq2*; that is, if *cq1* or
|
| 2914 |
*cq2* is `const`, then *cq12* is `const`. If `E2` is declared to be a
|
| 2915 |
`mutable` member, then the type of `E1.E2` is “*vq12* `T`”. If `E2` is
|
| 2916 |
not declared to be a `mutable` member, then the type of `E1.E2` is
|
| 2917 |
“*cq12* *vq12* `T`”.
|
| 2918 |
+
- If `E2` is an overload set, function overload resolution
|
| 2919 |
+
[[over.match]] is used to select the function to which `E2` refers.
|
| 2920 |
+
The type of `E1.E2` is the type of `E2` and `E1.E2` refers to the
|
| 2921 |
+
function referred to by `E2`.
|
| 2922 |
- If `E2` refers to a static member function, `E1.E2` is an lvalue.
|
| 2923 |
- Otherwise (when `E2` refers to a non-static member function),
|
| 2924 |
`E1.E2` is a prvalue. The expression can be used only as the
|
| 2925 |
left-hand operand of a member function call [[class.mfct]].
|
| 2926 |
\[*Note 5*: Any redundant set of parentheses surrounding the
|
| 2927 |
expression is ignored [[expr.prim.paren]]. — *end note*]
|
| 2928 |
- If `E2` is a nested type, the expression `E1.E2` is ill-formed.
|
| 2929 |
- If `E2` is a member enumerator and the type of `E2` is `T`, the
|
| 2930 |
+
expression `E1.E2` is a prvalue of type `T` whose value is the value
|
| 2931 |
+
of the enumerator.
|
| 2932 |
|
| 2933 |
+
If `E2` is a non-static member, the program is ill-formed if the class
|
| 2934 |
+
of which `E2` is directly a member is an ambiguous base
|
| 2935 |
+
[[class.member.lookup]] of the naming class [[class.access.base]] of
|
| 2936 |
+
`E2`.
|
| 2937 |
|
| 2938 |
[*Note 6*: The program is also ill-formed if the naming class is an
|
| 2939 |
ambiguous base of the class type of the object expression; see
|
| 2940 |
[[class.access.base]]. — *end note*]
|
| 2941 |
|
| 2942 |
+
If `E2` is a non-static member and the result of `E1` is an object whose
|
| 2943 |
+
type is not similar [[conv.qual]] to the type of `E1`, the behavior is
|
| 2944 |
+
undefined.
|
| 2945 |
+
|
| 2946 |
+
[*Example 1*:
|
| 2947 |
+
|
| 2948 |
+
``` cpp
|
| 2949 |
+
struct A { int i; };
|
| 2950 |
+
struct B { int j; };
|
| 2951 |
+
struct D : A, B {};
|
| 2952 |
+
void f() {
|
| 2953 |
+
D d;
|
| 2954 |
+
static_cast<B&>(d).j; // OK, object expression designates the B subobject of d
|
| 2955 |
+
reinterpret_cast<B&>(d).j; // undefined behavior
|
| 2956 |
+
}
|
| 2957 |
+
```
|
| 2958 |
+
|
| 2959 |
+
— *end example*]
|
| 2960 |
+
|
| 2961 |
#### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 2962 |
|
| 2963 |
The value of a postfix `++` expression is the value of its operand.
|
| 2964 |
|
| 2965 |
[*Note 1*: The value obtained is a copy of the original
|
|
|
|
| 2982 |
version of the type of the operand. If the operand is a bit-field that
|
| 2983 |
cannot represent the incremented value, the resulting value of the
|
| 2984 |
bit-field is *implementation-defined*. See also [[expr.add]] and
|
| 2985 |
[[expr.ass]].
|
| 2986 |
|
| 2987 |
+
The operand of postfix `--` is decremented analogously to the postfix
|
| 2988 |
`++` operator.
|
| 2989 |
|
| 2990 |
[*Note 3*: For prefix increment and decrement, see
|
| 2991 |
[[expr.pre.incr]]. — *end note*]
|
| 2992 |
|
|
|
|
| 3012 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 3013 |
unique `B` subobject of the `D` object pointed to by `v`, or a null
|
| 3014 |
pointer value if `v` is a null pointer value. Similarly, if `T` is
|
| 3015 |
“reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B` is a
|
| 3016 |
base class of `D`, the result is the unique `B` subobject of the `D`
|
| 3017 |
+
object referred to by `v`.[^14]
|
| 3018 |
+
|
| 3019 |
+
In both the pointer and reference cases, the program is ill-formed if
|
| 3020 |
+
`B` is an inaccessible or ambiguous base class of `D`.
|
| 3021 |
|
| 3022 |
[*Example 1*:
|
| 3023 |
|
| 3024 |
``` cpp
|
| 3025 |
struct B { };
|
|
|
|
| 3039 |
If `T` is “pointer to cv `void`”, then the result is a pointer to the
|
| 3040 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 3041 |
applied to see if the object pointed or referred to by `v` can be
|
| 3042 |
converted to the type pointed or referred to by `T`.
|
| 3043 |
|
| 3044 |
+
Let `C` be the class type to which `T` points or refers. The runtime
|
| 3045 |
check logically executes as follows:
|
| 3046 |
|
| 3047 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 3048 |
points (refers) to a public base class subobject of a `C` object, and
|
| 3049 |
if only one object of type `C` is derived from the subobject pointed
|
|
|
|
| 3097 |
|
| 3098 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 3099 |
`std::type_info` [[type.info]] and dynamic type `const` `std::type_info`
|
| 3100 |
or `const` *name* where *name* is an *implementation-defined* class
|
| 3101 |
publicly derived from `std::type_info` which preserves the behavior
|
| 3102 |
+
described in [[type.info]].[^15]
|
| 3103 |
+
|
| 3104 |
+
The lifetime of the object referred to by the lvalue extends to the end
|
| 3105 |
+
of the program. Whether or not the destructor is called for the
|
| 3106 |
+
`std::type_info` object at the end of the program is unspecified.
|
| 3107 |
+
|
| 3108 |
+
If the type of the *expression* or *type-id* operand is a (possibly
|
| 3109 |
+
cv-qualified) class type or a reference to (possibly cv-qualified) class
|
| 3110 |
+
type, that class shall be completely defined.
|
| 3111 |
|
| 3112 |
When `typeid` is applied to a glvalue whose type is a polymorphic class
|
| 3113 |
type [[class.virtual]], the result refers to a `std::type_info` object
|
| 3114 |
representing the type of the most derived object [[intro.object]] (that
|
| 3115 |
is, the dynamic type) to which the glvalue refers. If the glvalue is
|
| 3116 |
+
obtained by applying the unary `*` operator to a pointer[^16]
|
| 3117 |
+
|
| 3118 |
+
and the pointer is a null pointer value [[basic.compound]], the `typeid`
|
| 3119 |
expression throws an exception [[except.throw]] of a type that would
|
| 3120 |
match a handler of type `std::bad_typeid` exception [[bad.typeid]].
|
| 3121 |
|
| 3122 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 3123 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 3124 |
representing the static type of the expression. Lvalue-to-rvalue
|
| 3125 |
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
| 3126 |
[[conv.func]] conversions are not applied to the expression. If the
|
| 3127 |
expression is a prvalue, the temporary materialization conversion
|
| 3128 |
[[conv.rval]] is applied. The expression is an unevaluated operand
|
| 3129 |
+
[[term.unevaluated.operand]].
|
| 3130 |
|
| 3131 |
When `typeid` is applied to a *type-id*, the result refers to a
|
| 3132 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 3133 |
type of the *type-id* is a reference to a possibly cv-qualified type,
|
| 3134 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 3135 |
+
object representing the cv-unqualified referenced type.
|
|
|
|
|
|
|
| 3136 |
|
| 3137 |
[*Note 1*: The *type-id* cannot denote a function type with a
|
| 3138 |
*cv-qualifier-seq* or a *ref-qualifier* [[dcl.fct]]. — *end note*]
|
| 3139 |
|
| 3140 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
|
|
|
| 3154 |
typeid(D) == typeid(const D&); // yields true
|
| 3155 |
```
|
| 3156 |
|
| 3157 |
— *end example*]
|
| 3158 |
|
| 3159 |
+
The type `std::type_info` [[type.info]] is not predefined; if a standard
|
| 3160 |
+
library declaration [[typeinfo.syn]], [[std.modules]] of
|
| 3161 |
+
`std::type_info` does not precede [[basic.lookup.general]] a `typeid`
|
| 3162 |
+
expression, the program is ill-formed.
|
| 3163 |
|
| 3164 |
[*Note 2*: Subclause [[class.cdtor]] describes the behavior of `typeid`
|
| 3165 |
applied to an object under construction or destruction. — *end note*]
|
| 3166 |
|
| 3167 |
#### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
|
|
|
| 3197 |
static_cast<D&>(br); // produces lvalue denoting the original d object
|
| 3198 |
```
|
| 3199 |
|
| 3200 |
— *end example*]
|
| 3201 |
|
| 3202 |
+
An lvalue of type `T1` can be cast to type “rvalue reference to `T2`” if
|
| 3203 |
+
`T2` is reference-compatible with `T1` [[dcl.init.ref]]. If the value is
|
| 3204 |
+
not a bit-field, the result refers to the object or the specified base
|
| 3205 |
+
class subobject thereof; otherwise, the lvalue-to-rvalue conversion
|
| 3206 |
+
[[conv.lval]] is applied to the bit-field and the resulting prvalue is
|
| 3207 |
+
used as the operand of the `static_cast` for the remainder of this
|
| 3208 |
+
subclause. If `T2` is an inaccessible [[class.access]] or ambiguous
|
| 3209 |
+
[[class.member.lookup]] base class of `T1`, a program that necessitates
|
| 3210 |
+
such a cast is ill-formed.
|
| 3211 |
|
| 3212 |
An expression E can be explicitly converted to a type `T` if there is an
|
| 3213 |
implicit conversion sequence [[over.best.ics]] from E to `T`, if
|
| 3214 |
overload resolution for a direct-initialization [[dcl.init]] of an
|
| 3215 |
object or reference of type `T` from E would find at least one viable
|
|
|
|
| 3238 |
Otherwise, the `static_cast` shall perform one of the conversions listed
|
| 3239 |
below. No other conversion shall be performed explicitly using a
|
| 3240 |
`static_cast`.
|
| 3241 |
|
| 3242 |
Any expression can be explicitly converted to type cv `void`, in which
|
| 3243 |
+
case the operand is a discarded-value expression [[expr.prop]].
|
| 3244 |
|
| 3245 |
+
[*Note 3*: Such a `static_cast` has no result as it is a prvalue of
|
| 3246 |
+
type `void`; see [[basic.lval]]. — *end note*]
|
| 3247 |
+
|
| 3248 |
+
[*Note 4*: However, if the value is in a temporary object
|
| 3249 |
[[class.temporary]], the destructor for that object is not executed
|
| 3250 |
until the usual time, and the value of the object is preserved for the
|
| 3251 |
purpose of executing the destructor. — *end note*]
|
| 3252 |
|
| 3253 |
The inverse of any standard conversion sequence [[conv]] not containing
|
|
|
|
| 3285 |
that of converting from the original value to the floating-point type.
|
| 3286 |
|
| 3287 |
A value of integral or enumeration type can be explicitly converted to a
|
| 3288 |
complete enumeration type. If the enumeration type has a fixed
|
| 3289 |
underlying type, the value is first converted to that type by integral
|
| 3290 |
+
promotion [[conv.prom]] or integral conversion [[conv.integral]], if
|
| 3291 |
+
necessary, and then to the enumeration type. If the enumeration type
|
| 3292 |
+
does not have a fixed underlying type, the value is unchanged if the
|
| 3293 |
+
original value is within the range of the enumeration values
|
| 3294 |
+
[[dcl.enum]], and otherwise, the behavior is undefined. A value of
|
| 3295 |
+
floating-point type can also be explicitly converted to an enumeration
|
| 3296 |
+
type. The resulting value is the same as converting the original value
|
| 3297 |
+
to the underlying type of the enumeration [[conv.fpint]], and
|
| 3298 |
+
subsequently to the enumeration type.
|
| 3299 |
+
|
| 3300 |
+
A prvalue of floating-point type can be explicitly converted to any
|
| 3301 |
+
other floating-point type. If the source value can be exactly
|
| 3302 |
+
represented in the destination type, the result of the conversion has
|
| 3303 |
+
that exact representation. If the source value is between two adjacent
|
| 3304 |
+
destination values, the result of the conversion is an
|
| 3305 |
+
*implementation-defined* choice of either of those values. Otherwise,
|
| 3306 |
+
the behavior is undefined.
|
| 3307 |
|
| 3308 |
A prvalue of type “pointer to *cv1* `B`”, where `B` is a class type, can
|
| 3309 |
be converted to a prvalue of type “pointer to *cv2* `D`”, where `D` is a
|
| 3310 |
complete class derived [[class.derived]] from `B`, if *cv2* is the same
|
| 3311 |
cv-qualification as, or greater cv-qualification than, *cv1*. If `B` is
|
| 3312 |
a virtual base class of `D` or a base class of a virtual base class of
|
| 3313 |
`D`, or if no valid standard conversion from “pointer to `D`” to
|
| 3314 |
“pointer to `B`” exists [[conv.ptr]], the program is ill-formed. The
|
| 3315 |
null pointer value [[basic.compound]] is converted to the null pointer
|
| 3316 |
value of the destination type. If the prvalue of type “pointer to *cv1*
|
| 3317 |
+
`B`” points to a `B` that is actually a base class subobject of an
|
| 3318 |
+
object of type `D`, the resulting pointer points to the enclosing object
|
| 3319 |
+
of type `D`. Otherwise, the behavior is undefined.
|
| 3320 |
|
| 3321 |
A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
|
| 3322 |
converted to a prvalue of type “pointer to member of `B` of type *cv2*
|
| 3323 |
`T`”, where `D` is a complete class type and `B` is a base class
|
| 3324 |
[[class.derived]] of `D`, if *cv2* is the same cv-qualification as, or
|
| 3325 |
greater cv-qualification than, *cv1*.
|
| 3326 |
|
| 3327 |
+
[*Note 5*: Function types (including those used in
|
| 3328 |
pointer-to-member-function types) are never cv-qualified
|
| 3329 |
[[dcl.fct]]. — *end note*]
|
| 3330 |
|
| 3331 |
If no valid standard conversion from “pointer to member of `B` of type
|
| 3332 |
`T`” to “pointer to member of `D` of type `T`” exists [[conv.mem]], the
|
|
|
|
| 3335 |
class `B` contains the original member, or is a base or derived class of
|
| 3336 |
the class containing the original member, the resulting pointer to
|
| 3337 |
member points to the original member. Otherwise, the behavior is
|
| 3338 |
undefined.
|
| 3339 |
|
| 3340 |
+
[*Note 6*: Although class `B` need not contain the original member, the
|
| 3341 |
dynamic type of the object with which indirection through the pointer to
|
| 3342 |
member is performed must contain the original member; see
|
| 3343 |
[[expr.mptr.oper]]. — *end note*]
|
| 3344 |
|
| 3345 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
|
|
|
| 3347 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 3348 |
*cv1*. If the original pointer value represents the address `A` of a
|
| 3349 |
byte in memory and `A` does not satisfy the alignment requirement of
|
| 3350 |
`T`, then the resulting pointer value is unspecified. Otherwise, if the
|
| 3351 |
original pointer value points to an object *a*, and there is an object
|
| 3352 |
+
*b* of type similar to `T` that is pointer-interconvertible
|
| 3353 |
+
[[basic.compound]] with *a*, the result is a pointer to *b*. Otherwise,
|
| 3354 |
+
the pointer value is unchanged by the conversion.
|
|
|
|
| 3355 |
|
| 3356 |
[*Example 3*:
|
| 3357 |
|
| 3358 |
``` cpp
|
| 3359 |
T* p1 = new T;
|
|
|
|
| 3403 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 3404 |
any such exists on the implementation) and back to the same pointer type
|
| 3405 |
will have its original value; mappings between pointers and integers are
|
| 3406 |
otherwise *implementation-defined*.
|
| 3407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3408 |
A function pointer can be explicitly converted to a function pointer of
|
| 3409 |
a different type.
|
| 3410 |
|
| 3411 |
+
[*Note 4*: The effect of calling a function through a pointer to a
|
| 3412 |
function type [[dcl.fct]] that is not the same as the type used in the
|
| 3413 |
definition of the function is undefined [[expr.call]]. — *end note*]
|
| 3414 |
|
| 3415 |
Except that converting a prvalue of type “pointer to `T1`” to the type
|
| 3416 |
“pointer to `T2`” (where `T1` and `T2` are function types) and back to
|
| 3417 |
its original type yields the original pointer value, the result of such
|
| 3418 |
a pointer conversion is unspecified.
|
| 3419 |
|
| 3420 |
+
[*Note 5*: See also [[conv.ptr]] for more details of pointer
|
| 3421 |
conversions. — *end note*]
|
| 3422 |
|
| 3423 |
An object pointer can be explicitly converted to an object pointer of a
|
| 3424 |
+
different type.[^17]
|
| 3425 |
+
|
| 3426 |
+
When a prvalue `v` of object pointer type is converted to the object
|
| 3427 |
+
pointer type “pointer to cv `T`”, the result is
|
| 3428 |
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 3429 |
|
| 3430 |
+
[*Note 6*: Converting a pointer of type “pointer to `T1`” that points
|
| 3431 |
+
to an object of type `T1` to the type “pointer to `T2`” (where `T2` is
|
| 3432 |
+
an object type and the alignment requirements of `T2` are no stricter
|
| 3433 |
+
than those of `T1`) and back to its original type yields the original
|
| 3434 |
+
pointer value. — *end note*]
|
| 3435 |
|
| 3436 |
Converting a function pointer to an object pointer type or vice versa is
|
| 3437 |
conditionally-supported. The meaning of such a conversion is
|
| 3438 |
*implementation-defined*, except that if an implementation supports
|
| 3439 |
conversions in both directions, converting a prvalue of one type to the
|
|
|
|
| 3441 |
yield the original pointer value.
|
| 3442 |
|
| 3443 |
The null pointer value [[basic.compound]] is converted to the null
|
| 3444 |
pointer value of the destination type.
|
| 3445 |
|
| 3446 |
+
[*Note 7*: A null pointer constant of type `std::nullptr_t` cannot be
|
| 3447 |
converted to a pointer type, and a null pointer constant of integral
|
| 3448 |
type is not necessarily converted to a null pointer
|
| 3449 |
value. — *end note*]
|
| 3450 |
|
| 3451 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 3452 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 3453 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 3454 |
+
object types.[^18]
|
| 3455 |
+
|
| 3456 |
+
The null member pointer value [[conv.mem]] is converted to the null
|
| 3457 |
+
member pointer value of the destination type. The result of this
|
| 3458 |
+
conversion is unspecified, except in the following cases:
|
| 3459 |
|
| 3460 |
- Converting a prvalue of type “pointer to member function” to a
|
| 3461 |
different pointer-to-member-function type and back to its original
|
| 3462 |
type yields the original pointer-to-member value.
|
| 3463 |
- Converting a prvalue of type “pointer to data member of `X` of type
|
|
|
|
| 3485 |
Conversions that can be performed explicitly using `const_cast` are
|
| 3486 |
listed below. No other conversion shall be performed explicitly using
|
| 3487 |
`const_cast`.
|
| 3488 |
|
| 3489 |
[*Note 1*: Subject to the restrictions in this subclause, an expression
|
| 3490 |
+
can be cast to its own type using a `const_cast`
|
| 3491 |
operator. — *end note*]
|
| 3492 |
|
| 3493 |
For two similar types `T1` and `T2` [[conv.qual]], a prvalue of type
|
| 3494 |
`T1` may be explicitly converted to the type `T2` using a `const_cast`
|
| 3495 |
+
if, considering the qualification-decompositions of both types, each P¹ᵢ
|
| 3496 |
+
is the same as P²ᵢ for all i. The result of a `const_cast` refers to the
|
| 3497 |
original entity.
|
| 3498 |
|
| 3499 |
[*Example 1*:
|
| 3500 |
|
| 3501 |
``` cpp
|
|
|
|
| 3528 |
A null pointer value [[basic.compound]] is converted to the null pointer
|
| 3529 |
value of the destination type. The null member pointer value
|
| 3530 |
[[conv.mem]] is converted to the null member pointer value of the
|
| 3531 |
destination type.
|
| 3532 |
|
| 3533 |
+
[*Note 2*:
|
| 3534 |
+
|
| 3535 |
+
Depending on the type of the object, a write operation through the
|
| 3536 |
+
pointer, lvalue or pointer to data member resulting from a `const_cast`
|
| 3537 |
+
that casts away a const-qualifier[^20]
|
| 3538 |
+
|
| 3539 |
+
can produce undefined behavior [[dcl.type.cv]].
|
| 3540 |
+
|
| 3541 |
+
— *end note*]
|
| 3542 |
|
| 3543 |
A conversion from a type `T1` to a type `T2` *casts away constness* if
|
| 3544 |
+
`T1` and `T2` are different, there is a qualification-decomposition
|
| 3545 |
+
[[conv.qual]] of `T1` yielding *n* such that `T2` has a
|
| 3546 |
+
qualification-decomposition of the form
|
| 3547 |
|
| 3548 |
and there is no qualification conversion that converts `T1` to
|
| 3549 |
|
| 3550 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 3551 |
lvalue reference cast or casting from an expression of type `T1` to an
|
| 3552 |
xvalue of type `T2` using an rvalue reference cast casts away constness
|
| 3553 |
if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
|
| 3554 |
to `T2`” casts away constness.
|
| 3555 |
|
| 3556 |
[*Note 3*: Some conversions which involve only changes in
|
| 3557 |
+
cv-qualification cannot be done using `const_cast`. For instance,
|
| 3558 |
conversions between pointers to functions are not covered because such
|
| 3559 |
conversions lead to values whose use causes undefined behavior. For the
|
| 3560 |
same reasons, conversions between pointers to member functions, and in
|
| 3561 |
particular, the conversion from a pointer to a const member function to
|
| 3562 |
a pointer to a non-const member function, are not
|
| 3563 |
covered. — *end note*]
|
| 3564 |
|
| 3565 |
### Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 3566 |
|
| 3567 |
+
#### General <a id="expr.unary.general">[[expr.unary.general]]</a>
|
| 3568 |
+
|
| 3569 |
Expressions with unary operators group right-to-left.
|
| 3570 |
|
| 3571 |
``` bnf
|
| 3572 |
+
%% Ed. note: character protrusion would misalign operators.
|
| 3573 |
+
|
| 3574 |
unary-expression:
|
| 3575 |
postfix-expression
|
| 3576 |
unary-operator cast-expression
|
| 3577 |
'++' cast-expression
|
| 3578 |
+
'--' cast-expression
|
| 3579 |
await-expression
|
| 3580 |
sizeof unary-expression
|
| 3581 |
sizeof '(' type-id ')'
|
| 3582 |
sizeof '...' '(' identifier ')'
|
| 3583 |
alignof '(' type-id ')'
|
|
|
|
| 3585 |
new-expression
|
| 3586 |
delete-expression
|
| 3587 |
```
|
| 3588 |
|
| 3589 |
``` bnf
|
| 3590 |
+
%% Ed. note: character protrusion would misalign operators.
|
| 3591 |
+
|
| 3592 |
unary-operator: one of
|
| 3593 |
'* & + - ! ~'
|
| 3594 |
```
|
| 3595 |
|
| 3596 |
#### Unary operators <a id="expr.unary.op">[[expr.unary.op]]</a>
|
| 3597 |
|
| 3598 |
+
The unary `*` operator performs *indirection*. Its operand shall be a
|
| 3599 |
+
prvalue of type “pointer to `T`”, where `T` is an object or function
|
| 3600 |
+
type. The operator yields an lvalue of type `T` denoting the object or
|
| 3601 |
+
function to which the operand points.
|
|
|
|
| 3602 |
|
| 3603 |
[*Note 1*: Indirection through a pointer to an incomplete type (other
|
| 3604 |
than cv `void`) is valid. The lvalue thus obtained can be used in
|
| 3605 |
limited ways (to initialize a reference, for example); this lvalue must
|
| 3606 |
not be converted to a prvalue, see [[conv.lval]]. — *end note*]
|
| 3607 |
|
| 3608 |
+
Each of the following unary operators yields a prvalue.
|
| 3609 |
|
| 3610 |
+
The operand of the unary `&` operator shall be an lvalue of some type
|
| 3611 |
+
`T`. The result is a prvalue.
|
| 3612 |
|
| 3613 |
- If the operand is a *qualified-id* naming a non-static or variant
|
| 3614 |
+
member `m` of some class `C`, other than an explicit object member
|
| 3615 |
+
function, the result has type “pointer to member of class `C` of type
|
| 3616 |
+
`T`” and designates `C::m`.
|
| 3617 |
+
- Otherwise, the result has type “pointer to `T`” and points to the
|
| 3618 |
+
designated object [[intro.memory]] or function [[basic.compound]]. If
|
| 3619 |
+
the operand names an explicit object member function [[dcl.fct]], the
|
| 3620 |
+
operand shall be a *qualified-id*. \[*Note 2*: In particular, taking
|
| 3621 |
+
the address of a variable of type “cv `T`” yields a pointer of type
|
| 3622 |
+
“pointer to cv `T`”. — *end note*]
|
| 3623 |
|
| 3624 |
[*Example 1*:
|
| 3625 |
|
| 3626 |
``` cpp
|
| 3627 |
struct A { int i; };
|
|
|
|
| 3654 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 3655 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 3656 |
the built-in meaning or the operator function is called. The operand of
|
| 3657 |
`&` shall not be a bit-field.
|
| 3658 |
|
| 3659 |
+
[*Note 5*: The address of an overload set [[over]] can be taken only in
|
| 3660 |
+
a context that uniquely determines which function is referred to (see
|
| 3661 |
+
[[over.over]]). Since the context can affect whether the operand is a
|
| 3662 |
+
static or non-static member function, the context can also affect
|
| 3663 |
+
whether the expression has type “pointer to function” or “pointer to
|
| 3664 |
+
member function”. — *end note*]
|
|
|
|
| 3665 |
|
| 3666 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 3667 |
enumeration, or pointer type and the result is the value of the
|
| 3668 |
argument. Integral promotion is performed on integral or enumeration
|
| 3669 |
operands. The type of the result is the type of the promoted operand.
|
| 3670 |
|
| 3671 |
The operand of the unary `-` operator shall have arithmetic or unscoped
|
| 3672 |
+
enumeration type and the result is the negative of its operand. Integral
|
| 3673 |
promotion is performed on integral or enumeration operands. The negative
|
| 3674 |
of an unsigned quantity is computed by subtracting its value from 2ⁿ,
|
| 3675 |
where n is the number of bits in the promoted operand. The type of the
|
| 3676 |
result is the type of the promoted operand.
|
| 3677 |
|
| 3678 |
+
[*Note 6*: The result is the two’s complement of the operand (where
|
| 3679 |
+
operand and result are considered as unsigned). — *end note*]
|
| 3680 |
+
|
| 3681 |
The operand of the logical negation operator `!` is contextually
|
| 3682 |
converted to `bool` [[conv]]; its value is `true` if the converted
|
| 3683 |
operand is `false` and `false` otherwise. The type of the result is
|
| 3684 |
`bool`.
|
| 3685 |
|
| 3686 |
+
The operand of the `~` operator shall have integral or unscoped
|
| 3687 |
+
enumeration type. Integral promotions are performed. The type of the
|
| 3688 |
+
result is the type of the promoted operand. Given the coefficients `xᵢ`
|
| 3689 |
+
of the base-2 representation [[basic.fundamental]] of the promoted
|
| 3690 |
+
operand `x`, the coefficient `rᵢ` of the base-2 representation of the
|
| 3691 |
+
result `r` is 1 if `xᵢ` is 0, and 0 otherwise.
|
| 3692 |
+
|
| 3693 |
+
[*Note 7*: The result is the ones’ complement of the operand (where
|
| 3694 |
+
operand and result are considered as unsigned). — *end note*]
|
| 3695 |
+
|
| 3696 |
There is an ambiguity in the grammar when `~` is followed by a
|
| 3697 |
*type-name* or *decltype-specifier*. The ambiguity is resolved by
|
| 3698 |
+
treating `~` as the operator rather than as the start of an
|
| 3699 |
+
*unqualified-id* naming a destructor.
|
| 3700 |
|
| 3701 |
+
[*Note 8*: Because the grammar does not permit an operator to follow
|
| 3702 |
the `.`, `->`, or `::` tokens, a `~` followed by a *type-name* or
|
| 3703 |
*decltype-specifier* in a member access expression or *qualified-id* is
|
| 3704 |
unambiguously parsed as a destructor name. — *end note*]
|
| 3705 |
|
| 3706 |
#### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
|
|
|
| 3714 |
bit-field. The expression `++x` is equivalent to `x+=1`.
|
| 3715 |
|
| 3716 |
[*Note 1*: See the discussions of addition [[expr.add]] and assignment
|
| 3717 |
operators [[expr.ass]] for information on conversions. — *end note*]
|
| 3718 |
|
| 3719 |
+
The operand of prefix `--` is modified [[defns.access]] by subtracting
|
| 3720 |
+
`1`. The requirements on the operand of prefix `--` and the properties
|
| 3721 |
of its result are otherwise the same as those of prefix `++`.
|
| 3722 |
|
| 3723 |
[*Note 2*: For postfix increment and decrement, see
|
| 3724 |
[[expr.post.incr]]. — *end note*]
|
| 3725 |
|
|
|
|
| 3735 |
```
|
| 3736 |
|
| 3737 |
An *await-expression* shall appear only in a potentially-evaluated
|
| 3738 |
expression within the *compound-statement* of a *function-body* outside
|
| 3739 |
of a *handler* [[except.pre]]. In a *declaration-statement* or in the
|
| 3740 |
+
*simple-declaration* (if any) of an *init-statement*, an
|
| 3741 |
*await-expression* shall appear only in an *initializer* of that
|
| 3742 |
*declaration-statement* or *simple-declaration*. An *await-expression*
|
| 3743 |
shall not appear in a default argument [[dcl.fct.default]]. An
|
| 3744 |
+
*await-expression* shall not appear in the initializer of a block
|
| 3745 |
variable with static or thread storage duration. A context within a
|
| 3746 |
function where an *await-expression* can appear is called a *suspension
|
| 3747 |
context* of the function.
|
| 3748 |
|
| 3749 |
Evaluation of an *await-expression* involves the following auxiliary
|
| 3750 |
types, expressions, and objects:
|
| 3751 |
|
| 3752 |
- *p* is an lvalue naming the promise object [[dcl.fct.def.coroutine]]
|
| 3753 |
of the enclosing coroutine and `P` is the type of that object.
|
| 3754 |
+
- Unless the *await-expression* was implicitly produced by a
|
| 3755 |
+
*yield-expression* [[expr.yield]], an initial await expression, or a
|
| 3756 |
+
final await expression [[dcl.fct.def.coroutine]], a search is
|
| 3757 |
+
performed for the name `await_transform` in the scope of `P`
|
| 3758 |
+
[[class.member.lookup]]. If this search is performed and finds at
|
| 3759 |
+
least one declaration, then *a* is
|
| 3760 |
*p*`.await_transform(`*cast-expression*`)`; otherwise, *a* is the
|
| 3761 |
*cast-expression*.
|
| 3762 |
- *o* is determined by enumerating the applicable `operator co_await`
|
| 3763 |
functions for an argument *a* [[over.match.oper]], and choosing the
|
| 3764 |
best one through overload resolution [[over.match]]. If overload
|
|
|
|
| 3786 |
- If the result of *await-ready* is `false`, the coroutine is considered
|
| 3787 |
suspended. Then:
|
| 3788 |
- If the type of *await-suspend* is `std::coroutine_handle<Z>`,
|
| 3789 |
*await-suspend*`.resume()` is evaluated. \[*Note 1*: This resumes
|
| 3790 |
the coroutine referred to by the result of *await-suspend*. Any
|
| 3791 |
+
number of coroutines can be successively resumed in this fashion,
|
| 3792 |
eventually returning control flow to the current coroutine caller or
|
| 3793 |
resumer [[dcl.fct.def.coroutine]]. — *end note*]
|
| 3794 |
- Otherwise, if the type of *await-suspend* is `bool`, *await-suspend*
|
| 3795 |
is evaluated, and the coroutine is resumed if the result is `false`.
|
| 3796 |
- Otherwise, *await-suspend* is evaluated.
|
| 3797 |
|
| 3798 |
If the evaluation of *await-suspend* exits via an exception, the
|
| 3799 |
exception is caught, the coroutine is resumed, and the exception is
|
| 3800 |
+
immediately rethrown [[except.throw]]. Otherwise, control flow returns
|
| 3801 |
+
to the current coroutine caller or resumer [[dcl.fct.def.coroutine]]
|
| 3802 |
+
without exiting any scopes [[stmt.jump]]. The point in the coroutine
|
| 3803 |
+
immediately prior to control returning to its caller or resumer is a
|
| 3804 |
+
coroutine *suspend point*.
|
| 3805 |
- If the result of *await-ready* is `true`, or when the coroutine is
|
| 3806 |
+
resumed other than by rethrowing an exception from *await-suspend*,
|
| 3807 |
+
the *await-resume* expression is evaluated, and its result is the
|
| 3808 |
+
result of the *await-expression*.
|
| 3809 |
+
|
| 3810 |
+
[*Note 2*: With respect to sequencing, an *await-expression* is
|
| 3811 |
+
indivisible [[intro.execution]]. — *end note*]
|
| 3812 |
|
| 3813 |
[*Example 1*:
|
| 3814 |
|
| 3815 |
``` cpp
|
| 3816 |
template <typename T>
|
|
|
|
| 3837 |
using namespace std::chrono;
|
| 3838 |
|
| 3839 |
my_future<int> h();
|
| 3840 |
|
| 3841 |
my_future<void> g() {
|
| 3842 |
+
std::cout << "just about to go to sleep...\n";
|
| 3843 |
co_await 10ms;
|
| 3844 |
std::cout << "resumed\n";
|
| 3845 |
co_await h();
|
| 3846 |
}
|
| 3847 |
|
|
|
|
| 3854 |
#### Sizeof <a id="expr.sizeof">[[expr.sizeof]]</a>
|
| 3855 |
|
| 3856 |
The `sizeof` operator yields the number of bytes occupied by a
|
| 3857 |
non-potentially-overlapping object of the type of its operand. The
|
| 3858 |
operand is either an expression, which is an unevaluated operand
|
| 3859 |
+
[[term.unevaluated.operand]], or a parenthesized *type-id*. The `sizeof`
|
| 3860 |
+
operator shall not be applied to an expression that has function or
|
| 3861 |
+
incomplete type, to the parenthesized name of such types, or to a
|
| 3862 |
+
glvalue that designates a bit-field. The result of `sizeof` applied to
|
| 3863 |
+
any of the narrow character types is `1`. The result of `sizeof` applied
|
| 3864 |
+
to any other fundamental type [[basic.fundamental]] is
|
| 3865 |
+
*implementation-defined*.
|
| 3866 |
|
| 3867 |
+
[*Note 1*:
|
| 3868 |
+
|
| 3869 |
+
In particular, the values of `sizeof(bool)`, `sizeof(char16_t)`,
|
| 3870 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 3871 |
+
implementation-defined.[^21]
|
| 3872 |
+
|
| 3873 |
+
— *end note*]
|
| 3874 |
|
| 3875 |
[*Note 2*: See [[intro.memory]] for the definition of byte and
|
| 3876 |
+
[[term.object.representation]] for the definition of object
|
| 3877 |
representation. — *end note*]
|
| 3878 |
|
| 3879 |
When applied to a reference type, the result is the size of the
|
| 3880 |
referenced type. When applied to a class, the result is the number of
|
| 3881 |
bytes in an object of that class including any padding required for
|
| 3882 |
placing objects of that type in an array. The result of applying
|
| 3883 |
`sizeof` to a potentially-overlapping subobject is the size of the type,
|
| 3884 |
+
not the size of the subobject.[^22]
|
| 3885 |
+
|
| 3886 |
+
When applied to an array, the result is the total number of bytes in the
|
| 3887 |
+
array. This implies that the size of an array of n elements is n times
|
| 3888 |
+
the size of an element.
|
| 3889 |
|
| 3890 |
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 3891 |
function-to-pointer [[conv.func]] standard conversions are not applied
|
| 3892 |
to the operand of `sizeof`. If the operand is a prvalue, the temporary
|
| 3893 |
materialization conversion [[conv.rval]] is applied.
|
|
|
|
| 3911 |
The result of `sizeof` and `sizeof...` is a prvalue of type
|
| 3912 |
`std::size_t`.
|
| 3913 |
|
| 3914 |
[*Note 3*: A `sizeof` expression is an integral constant expression
|
| 3915 |
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 3916 |
+
`<cstddef>` [[cstddef.syn]], [[support.types.layout]]. — *end note*]
|
| 3917 |
|
| 3918 |
#### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
| 3919 |
|
| 3920 |
An `alignof` expression yields the alignment requirement of its operand
|
| 3921 |
type. The operand shall be a *type-id* representing a complete object
|
|
|
|
| 3923 |
|
| 3924 |
The result is a prvalue of type `std::size_t`.
|
| 3925 |
|
| 3926 |
[*Note 1*: An `alignof` expression is an integral constant expression
|
| 3927 |
[[expr.const]]. The type `std::size_t` is defined in the standard header
|
| 3928 |
+
`<cstddef>` [[cstddef.syn]], [[support.types.layout]]. — *end note*]
|
| 3929 |
|
| 3930 |
When `alignof` is applied to a reference type, the result is the
|
| 3931 |
alignment of the referenced type. When `alignof` is applied to an array
|
| 3932 |
type, the result is the alignment of the element type.
|
| 3933 |
|
| 3934 |
#### `noexcept` operator <a id="expr.unary.noexcept">[[expr.unary.noexcept]]</a>
|
| 3935 |
|
| 3936 |
The `noexcept` operator determines whether the evaluation of its
|
| 3937 |
+
operand, which is an unevaluated operand [[term.unevaluated.operand]],
|
| 3938 |
+
can throw an exception [[except.throw]].
|
| 3939 |
|
| 3940 |
``` bnf
|
| 3941 |
noexcept-expression:
|
| 3942 |
noexcept '(' expression ')'
|
| 3943 |
```
|
|
|
|
| 3953 |
#### New <a id="expr.new">[[expr.new]]</a>
|
| 3954 |
|
| 3955 |
The *new-expression* attempts to create an object of the *type-id*
|
| 3956 |
[[dcl.name]] or *new-type-id* to which it is applied. The type of that
|
| 3957 |
object is the *allocated type*. This type shall be a complete object
|
| 3958 |
+
type [[term.incomplete.type]], but not an abstract class type
|
| 3959 |
+
[[class.abstract]] or array thereof [[intro.object]].
|
| 3960 |
|
| 3961 |
[*Note 1*: Because references are not objects, references cannot be
|
| 3962 |
created by *new-expression*s. — *end note*]
|
| 3963 |
|
| 3964 |
+
[*Note 2*: The *type-id* can be a cv-qualified type, in which case the
|
| 3965 |
object created by the *new-expression* has a cv-qualified
|
| 3966 |
type. — *end note*]
|
| 3967 |
|
| 3968 |
``` bnf
|
| 3969 |
new-expression:
|
|
|
|
| 4069 |
|
| 4070 |
— *end example*]
|
| 4071 |
|
| 4072 |
— *end note*]
|
| 4073 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4074 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 4075 |
the associated array type.
|
| 4076 |
|
| 4077 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 4078 |
converted constant expression [[expr.const]] of type `std::size_t` and
|
|
|
|
| 4084 |
`n` is not a constant expression). — *end example*]
|
| 4085 |
|
| 4086 |
If the *type-id* or *new-type-id* denotes an array type of unknown bound
|
| 4087 |
[[dcl.array]], the *new-initializer* shall not be omitted; the allocated
|
| 4088 |
object is an array with `n` elements, where `n` is determined from the
|
| 4089 |
+
number of initial elements supplied in the *new-initializer*
|
| 4090 |
+
[[dcl.init.aggr]], [[dcl.init.string]].
|
| 4091 |
|
| 4092 |
If the *expression* in a *noptr-new-declarator* is present, it is
|
| 4093 |
implicitly converted to `std::size_t`. The *expression* is erroneous if:
|
| 4094 |
|
| 4095 |
- the expression is of non-class type and its value before converting to
|
|
|
|
| 4104 |
terminating `'\0'` in a *string-literal* [[lex.string]]) exceeds the
|
| 4105 |
number of elements to initialize.
|
| 4106 |
|
| 4107 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 4108 |
|
| 4109 |
+
- if the *expression* is a potentially-evaluated core constant
|
| 4110 |
+
expression, the program is ill-formed;
|
| 4111 |
- otherwise, an allocation function is not called; instead
|
| 4112 |
- if the allocation function that would have been called has a
|
| 4113 |
non-throwing exception specification [[except.spec]], the value of
|
| 4114 |
the *new-expression* is the null pointer value of the required
|
| 4115 |
result type;
|
|
|
|
| 4118 |
`std::bad_array_new_length` [[new.badlength]].
|
| 4119 |
|
| 4120 |
When the value of the *expression* is zero, the allocation function is
|
| 4121 |
called to allocate an array with no elements.
|
| 4122 |
|
| 4123 |
+
Objects created by a *new-expression* have dynamic storage duration
|
| 4124 |
+
[[basic.stc.dynamic]].
|
| 4125 |
+
|
| 4126 |
+
[*Note 5*: The lifetime of such an object is not necessarily
|
| 4127 |
+
restricted to the scope in which it is created. — *end note*]
|
| 4128 |
+
|
| 4129 |
+
When the allocated type is “array of `N` `T`” (that is, the
|
| 4130 |
+
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 4131 |
+
denotes an array type), the *new-expression* yields a prvalue of type
|
| 4132 |
+
“pointer to `T`” that points to the initial element (if any) of the
|
| 4133 |
+
array. Otherwise, let `T` be the allocated type; the *new-expression* is
|
| 4134 |
+
a prvalue of type “pointer to T” that points to the object created.
|
| 4135 |
+
|
| 4136 |
+
[*Note 6*: Both `new int` and `new int[10]` have type `int*` and the
|
| 4137 |
+
type of `new int[i][10]` is `int (*)[10]`. — *end note*]
|
| 4138 |
+
|
| 4139 |
A *new-expression* may obtain storage for the object by calling an
|
| 4140 |
allocation function [[basic.stc.dynamic.allocation]]. If the
|
| 4141 |
*new-expression* terminates by throwing an exception, it may release
|
| 4142 |
storage by calling a deallocation function
|
| 4143 |
[[basic.stc.dynamic.deallocation]]. If the allocated type is a non-array
|
|
|
|
| 4145 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 4146 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 4147 |
the deallocation function’s name is `operator delete[]`.
|
| 4148 |
|
| 4149 |
[*Note 7*: An implementation is required to provide default definitions
|
| 4150 |
+
for the global allocation functions
|
| 4151 |
+
[[basic.stc.dynamic]], [[new.delete.single]], [[new.delete.array]]. A
|
| 4152 |
+
C++ program can provide alternative definitions of these functions
|
| 4153 |
+
[[replacement.functions]] and/or class-specific versions [[class.free]].
|
| 4154 |
+
The set of allocation and deallocation functions that can be called by a
|
| 4155 |
+
*new-expression* can include functions that do not perform allocation or
|
| 4156 |
+
deallocation; for example, see [[new.delete.placement]]. — *end note*]
|
| 4157 |
|
| 4158 |
+
If the *new-expression* does not begin with a unary `::` operator and
|
| 4159 |
+
the allocated type is a class type `T` or array thereof, a search is
|
| 4160 |
+
performed for the allocation function’s name in the scope of `T`
|
| 4161 |
+
[[class.member.lookup]]. Otherwise, or if nothing is found, the
|
| 4162 |
+
allocation function’s name is looked up by searching for it in the
|
| 4163 |
+
global scope.
|
| 4164 |
|
| 4165 |
An implementation is allowed to omit a call to a replaceable global
|
| 4166 |
+
allocation function [[new.delete.single]], [[new.delete.array]]. When it
|
| 4167 |
+
does so, the storage is instead provided by the implementation or
|
| 4168 |
provided by extending the allocation of another *new-expression*.
|
| 4169 |
|
| 4170 |
During an evaluation of a constant expression, a call to an allocation
|
| 4171 |
function is always omitted.
|
| 4172 |
|
|
|
|
| 4297 |
|
| 4298 |
— *end example*]
|
| 4299 |
|
| 4300 |
[*Note 10*: Unless an allocation function has a non-throwing exception
|
| 4301 |
specification [[except.spec]], it indicates failure to allocate storage
|
| 4302 |
+
by throwing a `std::bad_alloc` exception
|
| 4303 |
+
[[basic.stc.dynamic.allocation]], [[except]], [[bad.alloc]]; it returns
|
| 4304 |
a non-null pointer otherwise. If the allocation function has a
|
| 4305 |
non-throwing exception specification, it returns null to indicate
|
| 4306 |
failure to allocate storage and a non-null pointer
|
| 4307 |
otherwise. — *end note*]
|
| 4308 |
|
|
|
|
| 4333 |
the allocated object is sequenced before the value computation of the
|
| 4334 |
*new-expression*.
|
| 4335 |
|
| 4336 |
If the *new-expression* creates an object or an array of objects of
|
| 4337 |
class type, access and ambiguity control are done for the allocation
|
| 4338 |
+
function, the deallocation function [[basic.stc.dynamic.deallocation]],
|
| 4339 |
+
and the constructor [[class.ctor]] selected for the initialization (if
|
| 4340 |
+
any). If the *new-expression* creates an array of objects of class type,
|
| 4341 |
+
the destructor is potentially invoked [[class.dtor]].
|
| 4342 |
|
| 4343 |
+
If any part of the object initialization described above[^24]
|
| 4344 |
+
|
| 4345 |
+
terminates by throwing an exception and a suitable deallocation function
|
| 4346 |
+
can be found, the deallocation function is called to free the memory in
|
| 4347 |
+
which the object was being constructed, after which the exception
|
| 4348 |
+
continues to propagate in the context of the *new-expression*. If no
|
| 4349 |
+
unambiguous matching deallocation function can be found, propagating the
|
| 4350 |
+
exception does not cause the object’s memory to be freed.
|
| 4351 |
|
| 4352 |
[*Note 13*: This is appropriate when the called allocation function
|
| 4353 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 4354 |
leak. — *end note*]
|
| 4355 |
|
| 4356 |
+
If the *new-expression* does not begin with a unary `::` operator and
|
| 4357 |
+
the allocated type is a class type `T` or an array thereof, a search is
|
| 4358 |
+
performed for the deallocation function’s name in the scope of `T`.
|
| 4359 |
+
Otherwise, or if nothing is found, the deallocation function’s name is
|
| 4360 |
+
looked up by searching for it in the global scope.
|
|
|
|
|
|
|
| 4361 |
|
| 4362 |
A declaration of a placement deallocation function matches the
|
| 4363 |
declaration of a placement allocation function if it has the same number
|
| 4364 |
of parameters and, after parameter transformations [[dcl.fct]], all
|
| 4365 |
parameter types except the first are identical. If the lookup finds a
|
|
|
|
| 4410 |
```
|
| 4411 |
|
| 4412 |
The first alternative is a *single-object delete expression*, and the
|
| 4413 |
second is an *array delete expression*. Whenever the `delete` keyword is
|
| 4414 |
immediately followed by empty square brackets, it shall be interpreted
|
| 4415 |
+
as the second alternative.[^25]
|
| 4416 |
+
|
| 4417 |
+
The operand shall be of pointer to object type or of class type. If of
|
| 4418 |
+
class type, the operand is contextually implicitly converted [[conv]] to
|
| 4419 |
+
a pointer to object type.[^26]
|
| 4420 |
+
|
| 4421 |
+
The *delete-expression* has type `void`.
|
| 4422 |
|
| 4423 |
If the operand has a class type, the operand is converted to a pointer
|
| 4424 |
type by calling the above-mentioned conversion function, and the
|
| 4425 |
converted operand is used in place of the original operand for the
|
| 4426 |
remainder of this subclause. In a single-object delete expression, the
|
| 4427 |
value of the operand of `delete` may be a null pointer value, a pointer
|
| 4428 |
+
value that resulted from a previous non-array *new-expression*, or a
|
| 4429 |
+
pointer to a base class subobject of an object created by such a
|
| 4430 |
+
*new-expression*. If not, the behavior is undefined. In an array delete
|
| 4431 |
+
expression, the value of the operand of `delete` may be a null pointer
|
| 4432 |
+
value or a pointer value that resulted from a previous array
|
| 4433 |
+
*new-expression* whose allocation function was not a non-allocating form
|
| 4434 |
+
[[new.delete.placement]].[^27]
|
| 4435 |
+
|
| 4436 |
+
If not, the behavior is undefined.
|
| 4437 |
|
| 4438 |
[*Note 1*: This means that the syntax of the *delete-expression* must
|
| 4439 |
match the type of the object allocated by `new`, not the syntax of the
|
| 4440 |
*new-expression*. — *end note*]
|
| 4441 |
|
|
|
|
| 4443 |
*delete-expression*; it is not necessary to cast away the constness
|
| 4444 |
[[expr.const.cast]] of the pointer expression before it is used as the
|
| 4445 |
operand of the *delete-expression*. — *end note*]
|
| 4446 |
|
| 4447 |
In a single-object delete expression, if the static type of the object
|
| 4448 |
+
to be deleted is not similar [[conv.qual]] to its dynamic type and the
|
| 4449 |
+
selected deallocation function (see below) is not a destroying operator
|
| 4450 |
+
delete, the static type shall be a base class of the dynamic type of the
|
| 4451 |
+
object to be deleted and the static type shall have a virtual destructor
|
| 4452 |
+
or the behavior is undefined. In an array delete expression, if the
|
| 4453 |
+
dynamic type of the object to be deleted is not similar to its static
|
| 4454 |
+
type, the behavior is undefined.
|
| 4455 |
|
| 4456 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 4457 |
exactly once.
|
| 4458 |
|
| 4459 |
If the object being deleted has incomplete class type at the point of
|
|
|
|
| 4494 |
|
| 4495 |
If the value of the operand of the *delete-expression* is a null pointer
|
| 4496 |
value, it is unspecified whether a deallocation function will be called
|
| 4497 |
as described above.
|
| 4498 |
|
| 4499 |
+
If a deallocation function is called, it is `operator delete` for a
|
| 4500 |
+
single-object delete expression or `operator delete[]` for an array
|
| 4501 |
+
delete expression.
|
|
|
|
|
|
|
|
|
|
| 4502 |
|
| 4503 |
+
[*Note 4*: An implementation provides default definitions of the
|
| 4504 |
+
global deallocation functions
|
| 4505 |
+
[[new.delete.single]], [[new.delete.array]]. A C++ program can provide
|
| 4506 |
+
alternative definitions of these functions [[replacement.functions]],
|
| 4507 |
+
and/or class-specific versions [[class.free]]. — *end note*]
|
|
|
|
| 4508 |
|
| 4509 |
+
If the keyword `delete` in a *delete-expression* is not preceded by the
|
| 4510 |
+
unary `::` operator and the type of the operand is a pointer to a
|
| 4511 |
+
(possibly cv-qualified) class type `T` or (possibly multidimensional)
|
| 4512 |
+
array thereof:
|
| 4513 |
+
|
| 4514 |
+
- For a single-object delete expression, if the operand is a pointer to
|
| 4515 |
+
cv `T` and `T` has a virtual destructor, the deallocation function is
|
| 4516 |
+
the one selected at the point of definition of the dynamic type’s
|
| 4517 |
+
virtual destructor [[class.dtor]].
|
| 4518 |
+
- Otherwise, a search is performed for the deallocation function’s name
|
| 4519 |
+
in the scope of `T`.
|
| 4520 |
+
|
| 4521 |
+
Otherwise, or if nothing is found, the deallocation function’s name is
|
| 4522 |
+
looked up by searching for it in the global scope. In any case, any
|
| 4523 |
+
declarations other than of usual deallocation functions
|
| 4524 |
+
[[basic.stc.dynamic.deallocation]] are discarded.
|
| 4525 |
+
|
| 4526 |
+
[*Note 5*: If only a placement deallocation function is found in a
|
| 4527 |
+
class, the program is ill-formed because the lookup set is empty
|
| 4528 |
+
[[basic.lookup]]. — *end note*]
|
| 4529 |
+
|
| 4530 |
+
If more than one deallocation function is found, the function to be
|
| 4531 |
+
called is selected as follows:
|
| 4532 |
|
| 4533 |
- If any of the deallocation functions is a destroying operator delete,
|
| 4534 |
all deallocation functions that are not destroying operator deletes
|
| 4535 |
are eliminated from further consideration.
|
| 4536 |
- If the type has new-extended alignment, a function with a parameter of
|
| 4537 |
type `std::align_val_t` is preferred; otherwise a function without
|
| 4538 |
such a parameter is preferred. If any preferred functions are found,
|
| 4539 |
all non-preferred functions are eliminated from further consideration.
|
| 4540 |
- If exactly one function remains, that function is selected and the
|
| 4541 |
selection process terminates.
|
| 4542 |
+
- If the deallocation functions belong to a class scope, the one without
|
| 4543 |
+
a parameter of type `std::size_t` is selected.
|
| 4544 |
- If the type is complete and if, for an array delete expression only,
|
| 4545 |
the operand is a pointer to a class type with a non-trivial destructor
|
| 4546 |
+
or a (possibly multidimensional) array thereof, the function with a
|
| 4547 |
parameter of type `std::size_t` is selected.
|
| 4548 |
- Otherwise, it is unspecified whether a deallocation function with a
|
| 4549 |
parameter of type `std::size_t` is selected.
|
| 4550 |
|
| 4551 |
For a single-object delete expression, the deleted object is the object
|
| 4552 |
+
A pointed to by the operand if the static type of A does not have a
|
| 4553 |
+
virtual destructor, and the most-derived object of A otherwise.
|
| 4554 |
|
| 4555 |
+
[*Note 6*: If the deallocation function is not a destroying operator
|
| 4556 |
delete and the deleted object is not the most derived object in the
|
| 4557 |
former case, the behavior is undefined, as stated above. — *end note*]
|
| 4558 |
|
| 4559 |
For an array delete expression, the deleted object is the array object.
|
| 4560 |
When a *delete-expression* is executed, the selected deallocation
|
| 4561 |
function shall be called with the address of the deleted object in a
|
| 4562 |
single-object delete expression, or the address of the deleted object
|
| 4563 |
suitably adjusted for the array allocation overhead [[expr.new]] in an
|
| 4564 |
array delete expression, as its first argument.
|
| 4565 |
|
| 4566 |
+
[*Note 7*: Any cv-qualifiers in the type of the deleted object are
|
| 4567 |
ignored when forming this argument. — *end note*]
|
| 4568 |
|
| 4569 |
If a destroying operator delete is used, an unspecified value is passed
|
| 4570 |
as the argument corresponding to the parameter of type
|
| 4571 |
`std::destroying_delete_t`. If a deallocation function with a parameter
|
|
|
|
| 4574 |
deallocation function with a parameter of type `std::size_t` is used,
|
| 4575 |
the size of the deleted object in a single-object delete expression, or
|
| 4576 |
of the array plus allocation overhead in an array delete expression, is
|
| 4577 |
passed as the corresponding argument.
|
| 4578 |
|
| 4579 |
+
[*Note 8*: If this results in a call to a replaceable deallocation
|
| 4580 |
function, and either the first argument was not the result of a prior
|
| 4581 |
call to a replaceable allocation function or the second or third
|
| 4582 |
argument was not the corresponding argument in said call, the behavior
|
| 4583 |
+
is undefined [[new.delete.single]], [[new.delete.array]]. — *end note*]
|
|
|
|
| 4584 |
|
| 4585 |
Access and ambiguity control are done for both the deallocation function
|
| 4586 |
+
and the destructor [[class.dtor]], [[class.free]].
|
| 4587 |
|
| 4588 |
### Explicit type conversion (cast notation) <a id="expr.cast">[[expr.cast]]</a>
|
| 4589 |
|
| 4590 |
The result of the expression `(T)` *cast-expression* is of type `T`. The
|
| 4591 |
result is an lvalue if `T` is an lvalue reference type or an rvalue
|
|
|
|
| 4689 |
type “pointer to `U`” where `U` is either `T` or a class of which `T` is
|
| 4690 |
an unambiguous and accessible base class. The expression `E1->*E2` is
|
| 4691 |
converted into the equivalent form `(*(E1)).*E2`.
|
| 4692 |
|
| 4693 |
Abbreviating *pm-expression*`.*`*cast-expression* as `E1.*E2`, `E1` is
|
| 4694 |
+
called the *object expression*. If the result of `E1` is an object whose
|
| 4695 |
+
type is not similar to the type of `E1`, or whose most derived object
|
| 4696 |
+
does not contain the member to which `E2` refers, the behavior is
|
| 4697 |
+
undefined. The expression `E1` is sequenced before the expression `E2`.
|
| 4698 |
|
| 4699 |
The restrictions on cv-qualification, and the manner in which the
|
| 4700 |
cv-qualifiers of the operands are combined to produce the cv-qualifiers
|
| 4701 |
of the result, are the same as the rules for `E1.E2` given in
|
| 4702 |
[[expr.ref]].
|
|
|
|
| 4768 |
|
| 4769 |
The binary `/` operator yields the quotient, and the binary `%` operator
|
| 4770 |
yields the remainder from the division of the first expression by the
|
| 4771 |
second. If the second operand of `/` or `%` is zero the behavior is
|
| 4772 |
undefined. For integral operands the `/` operator yields the algebraic
|
| 4773 |
+
quotient with any fractional part discarded;[^28]
|
| 4774 |
+
|
| 4775 |
+
if the quotient `a/b` is representable in the type of the result,
|
| 4776 |
+
`(a/b)*b + a%b` is equal to `a`; otherwise, the behavior of both `a/b`
|
| 4777 |
+
and `a%b` is undefined.
|
| 4778 |
|
| 4779 |
### Additive operators <a id="expr.add">[[expr.add]]</a>
|
| 4780 |
|
| 4781 |
The additive operators `+` and `-` group left-to-right. The usual
|
| 4782 |
arithmetic conversions [[expr.arith.conv]] are performed for operands of
|
|
|
|
| 4817 |
(possibly-hypothetical) array element i + j of `x` if 0 ≤ i + j ≤ n
|
| 4818 |
and the expression `P - J` points to the (possibly-hypothetical) array
|
| 4819 |
element i - j of `x` if 0 ≤ i - j ≤ n.
|
| 4820 |
- Otherwise, the behavior is undefined.
|
| 4821 |
|
| 4822 |
+
[*Note 1*: Adding a value other than 0 or 1 to a pointer to a base
|
| 4823 |
+
class subobject, a member subobject, or a complete object results in
|
| 4824 |
+
undefined behavior. — *end note*]
|
| 4825 |
+
|
| 4826 |
When two pointer expressions `P` and `Q` are subtracted, the type of the
|
| 4827 |
result is an *implementation-defined* signed integral type; this type
|
| 4828 |
shall be the same type that is defined as `std::ptrdiff_t` in the
|
| 4829 |
`<cstddef>` header [[support.types.layout]].
|
| 4830 |
|
| 4831 |
- If `P` and `Q` both evaluate to null pointer values, the result is 0.
|
| 4832 |
- Otherwise, if `P` and `Q` point to, respectively, array elements i and
|
| 4833 |
j of the same array object `x`, the expression `P - Q` has the value
|
| 4834 |
i - j.
|
| 4835 |
+
- Otherwise, the behavior is undefined. \[*Note 2*: If the value i - j
|
| 4836 |
is not in the range of representable values of type `std::ptrdiff_t`,
|
| 4837 |
the behavior is undefined. — *end note*]
|
| 4838 |
|
| 4839 |
For addition or subtraction, if the expressions `P` or `Q` have type
|
| 4840 |
“pointer to cv `T`”, where `T` and the array element type are not
|
| 4841 |
similar [[conv.qual]], the behavior is undefined.
|
| 4842 |
|
| 4843 |
+
[*Example 1*:
|
| 4844 |
+
|
| 4845 |
+
``` cpp
|
| 4846 |
+
int arr[5] = {1, 2, 3, 4, 5};
|
| 4847 |
+
unsigned int *p = reinterpret_cast<unsigned int*>(arr + 1);
|
| 4848 |
+
unsigned int k = *p; // OK, value of k is 2[conv.lval]
|
| 4849 |
+
unsigned int *q = p + 1; // undefined behavior: p points to an int, not an unsigned int object
|
| 4850 |
+
```
|
| 4851 |
+
|
| 4852 |
+
— *end example*]
|
| 4853 |
|
| 4854 |
### Shift operators <a id="expr.shift">[[expr.shift]]</a>
|
| 4855 |
|
| 4856 |
The shift operators `<<` and `>>` group left-to-right.
|
| 4857 |
|
|
|
|
| 4920 |
|
| 4921 |
If both operands have the same enumeration type `E`, the operator yields
|
| 4922 |
the result of converting the operands to the underlying type of `E` and
|
| 4923 |
applying `<=>` to the converted operands.
|
| 4924 |
|
| 4925 |
+
If at least one of the operands is of object pointer type and the other
|
| 4926 |
+
operand is of object pointer or array type, array-to-pointer conversions
|
| 4927 |
[[conv.array]], pointer conversions [[conv.ptr]], and qualification
|
| 4928 |
conversions [[conv.qual]] are performed on both operands to bring them
|
| 4929 |
to their composite pointer type [[expr.type]]. After the conversions,
|
| 4930 |
the operands shall have the same type.
|
| 4931 |
|
| 4932 |
[*Note 1*: If both of the operands are arrays, array-to-pointer
|
| 4933 |
conversions [[conv.array]] are not applied. — *end note*]
|
| 4934 |
|
| 4935 |
+
In this case, `p <=> q` is of type `std::strong_ordering` and the result
|
| 4936 |
+
is defined by the following rules:
|
| 4937 |
+
|
| 4938 |
+
- If two pointer operands `p` and `q` compare equal [[expr.eq]],
|
| 4939 |
+
`p <=> q` yields `std::strong_ordering::equal`;
|
| 4940 |
+
- otherwise, if `p` and `q` compare unequal, `p <=> q` yields
|
| 4941 |
`std::strong_ordering::less` if `q` compares greater than `p` and
|
| 4942 |
`std::strong_ordering::greater` if `p` compares greater than `q`
|
| 4943 |
+
[[expr.rel]];
|
| 4944 |
+
- otherwise, the result is unspecified.
|
| 4945 |
|
| 4946 |
Otherwise, the program is ill-formed.
|
| 4947 |
|
| 4948 |
The three comparison category types [[cmp.categories]] (the types
|
| 4949 |
`std::strong_ordering`, `std::weak_ordering`, and
|
| 4950 |
+
`std::partial_ordering`) are not predefined; if a standard library
|
| 4951 |
+
declaration [[compare.syn]], [[std.modules]] of such a class type does
|
| 4952 |
+
not precede [[basic.lookup.general]] a use of that type — even an
|
| 4953 |
+
implicit use in which the type is not named (e.g., via the `auto`
|
| 4954 |
specifier [[dcl.spec.auto]] in a defaulted three-way comparison
|
| 4955 |
+
[[class.spaceship]] or use of the built-in operator) — the program is
|
| 4956 |
ill-formed.
|
| 4957 |
|
| 4958 |
### Relational operators <a id="expr.rel">[[expr.rel]]</a>
|
| 4959 |
|
| 4960 |
The relational operators group left-to-right.
|
|
|
|
| 4986 |
pointers, pointer conversions [[conv.ptr]] and qualification conversions
|
| 4987 |
[[conv.qual]] are performed to bring them to their composite pointer
|
| 4988 |
type [[expr.type]]. After conversions, the operands shall have the same
|
| 4989 |
type.
|
| 4990 |
|
| 4991 |
+
The result of comparing unequal pointers to objects[^30]
|
| 4992 |
+
|
| 4993 |
+
is defined in terms of a partial order consistent with the following
|
| 4994 |
+
rules:
|
| 4995 |
|
| 4996 |
- If two pointers point to different elements of the same array, or to
|
| 4997 |
subobjects thereof, the pointer to the element with the higher
|
| 4998 |
subscript is required to compare greater.
|
| 4999 |
- If two pointers point to different non-static data members of the same
|
| 5000 |
object, or to subobjects of such members, recursively, the pointer to
|
| 5001 |
+
the later declared member is required to compare greater provided
|
| 5002 |
+
neither member is a subobject of zero size and their class is not a
|
| 5003 |
+
union.
|
| 5004 |
- Otherwise, neither pointer is required to compare greater than the
|
| 5005 |
other.
|
| 5006 |
|
| 5007 |
If two operands `p` and `q` compare equal [[expr.eq]], `p<=q` and `p>=q`
|
| 5008 |
both yield `true` and `p<q` and `p>q` both yield `false`. Otherwise, if
|
| 5009 |
+
a pointer to object `p` compares greater than a pointer `q`, `p>=q`,
|
| 5010 |
+
`p>q`, `q<=p`, and `q<p` all yield `true` and `p<=q`, `p<q`, `q>=p`, and
|
| 5011 |
+
`q>p` all yield `false`. Otherwise, the result of each of the operators
|
| 5012 |
+
is unspecified.
|
| 5013 |
+
|
| 5014 |
+
[*Note 1*: A relational operator applied to unequal function pointers
|
| 5015 |
+
or to unequal pointers to `void` yields an unspecified
|
| 5016 |
+
result. — *end note*]
|
| 5017 |
|
| 5018 |
If both operands (after conversions) are of arithmetic or enumeration
|
| 5019 |
type, each of the operators shall yield `true` if the specified
|
| 5020 |
relationship is true and `false` if it is false.
|
| 5021 |
|
|
|
|
| 5153 |
The `^` operator groups left-to-right. The operands shall be of integral
|
| 5154 |
or unscoped enumeration type. The usual arithmetic conversions
|
| 5155 |
[[expr.arith.conv]] are performed. Given the coefficients `xᵢ` and `yᵢ`
|
| 5156 |
of the base-2 representation [[basic.fundamental]] of the converted
|
| 5157 |
operands `x` and `y`, the coefficient `rᵢ` of the base-2 representation
|
| 5158 |
+
of the result `r` is 1 if either (but not both) of `xᵢ` and `yᵢ` is 1,
|
| 5159 |
and 0 otherwise.
|
| 5160 |
|
| 5161 |
[*Note 1*: The result is the bitwise exclusive function of the
|
| 5162 |
operands. — *end note*]
|
| 5163 |
|
|
|
|
| 5172 |
The `|` operator groups left-to-right. The operands shall be of integral
|
| 5173 |
or unscoped enumeration type. The usual arithmetic conversions
|
| 5174 |
[[expr.arith.conv]] are performed. Given the coefficients `xᵢ` and `yᵢ`
|
| 5175 |
of the base-2 representation [[basic.fundamental]] of the converted
|
| 5176 |
operands `x` and `y`, the coefficient `rᵢ` of the base-2 representation
|
| 5177 |
+
of the result `r` is 1 if at least one of `xᵢ` and `yᵢ` is 1, and 0
|
| 5178 |
otherwise.
|
| 5179 |
|
| 5180 |
[*Note 1*: The result is the bitwise inclusive function of the
|
| 5181 |
operands. — *end note*]
|
| 5182 |
|
|
|
|
| 5261 |
Attempts are made to form an implicit conversion sequence from an
|
| 5262 |
operand expression `E1` of type `T1` to a target type related to the
|
| 5263 |
type `T2` of the operand expression `E2` as follows:
|
| 5264 |
|
| 5265 |
- If `E2` is an lvalue, the target type is “lvalue reference to `T2`”,
|
| 5266 |
+
but an implicit conversion sequence can only be formed if the
|
| 5267 |
+
reference would bind directly [[dcl.init.ref]] to a glvalue.
|
| 5268 |
- If `E2` is an xvalue, the target type is “rvalue reference to `T2`”,
|
| 5269 |
+
but an implicit conversion sequence can only be formed if the
|
| 5270 |
+
reference would bind directly.
|
| 5271 |
- If `E2` is a prvalue or if neither of the conversion sequences above
|
| 5272 |
can be formed and at least one of the operands has (possibly
|
| 5273 |
cv-qualified) class type:
|
| 5274 |
- if `T1` and `T2` are the same class type (ignoring cv-qualification)
|
| 5275 |
and `T2` is at least as cv-qualified as `T1`, the target type is
|
|
|
|
| 5301 |
or if both are bit-fields.
|
| 5302 |
|
| 5303 |
Otherwise, the result is a prvalue. If the second and third operands do
|
| 5304 |
not have the same type, and either has (possibly cv-qualified) class
|
| 5305 |
type, overload resolution is used to determine the conversions (if any)
|
| 5306 |
+
to be applied to the operands [[over.match.oper]], [[over.built]]. If
|
| 5307 |
the overload resolution fails, the program is ill-formed. Otherwise, the
|
| 5308 |
conversions thus determined are applied, and the converted operands are
|
| 5309 |
used in place of the original operands for the remainder of this
|
| 5310 |
subclause.
|
| 5311 |
|
|
|
|
| 5345 |
A *yield-expression* shall appear only within a suspension context of a
|
| 5346 |
function [[expr.await]]. Let *e* be the operand of the
|
| 5347 |
*yield-expression* and *p* be an lvalue naming the promise object of the
|
| 5348 |
enclosing coroutine [[dcl.fct.def.coroutine]], then the
|
| 5349 |
*yield-expression* is equivalent to the expression
|
| 5350 |
+
`co_await p.yield_value(e)`.
|
| 5351 |
|
| 5352 |
[*Example 1*:
|
| 5353 |
|
| 5354 |
``` cpp
|
| 5355 |
template <typename T>
|
|
|
|
| 5366 |
iterator begin();
|
| 5367 |
iterator end();
|
| 5368 |
};
|
| 5369 |
|
| 5370 |
my_generator<pair<int,int>> g1() {
|
| 5371 |
+
for (int i = 0; i < 10; ++i) co_yield {i,i};
|
| 5372 |
}
|
| 5373 |
my_generator<pair<int,int>> g2() {
|
| 5374 |
+
for (int i = 0; i < 10; ++i) co_yield make_pair(i,i);
|
| 5375 |
}
|
| 5376 |
|
| 5377 |
auto f(int x = co_yield 5); // error: yield-expression outside of function suspension context
|
| 5378 |
int a[] = { co_yield 1 }; // error: yield-expression outside of function suspension context
|
| 5379 |
|
|
|
|
| 5406 |
existing exception object; no new exception object is created. The
|
| 5407 |
exception is no longer considered to be caught.
|
| 5408 |
|
| 5409 |
[*Example 1*:
|
| 5410 |
|
| 5411 |
+
An exception handler that cannot completely handle the exception itself
|
| 5412 |
+
can be written like this:
|
| 5413 |
|
| 5414 |
``` cpp
|
| 5415 |
try {
|
| 5416 |
// ...
|
| 5417 |
} catch (...) { // catch all exceptions
|
|
|
|
| 5428 |
|
| 5429 |
### Assignment and compound assignment operators <a id="expr.ass">[[expr.ass]]</a>
|
| 5430 |
|
| 5431 |
The assignment operator (`=`) and the compound assignment operators all
|
| 5432 |
group right-to-left. All require a modifiable lvalue as their left
|
| 5433 |
+
operand; their result is an lvalue of the type of the left operand,
|
| 5434 |
+
referring to the left operand. The result in all cases is a bit-field if
|
| 5435 |
+
the left operand is a bit-field. In all cases, the assignment is
|
| 5436 |
+
sequenced after the value computation of the right and left operands,
|
| 5437 |
+
and before the value computation of the assignment expression. The right
|
| 5438 |
+
operand is sequenced before the left operand. With respect to an
|
| 5439 |
+
indeterminately-sequenced function call, the operation of a compound
|
| 5440 |
+
assignment is a single evaluation.
|
| 5441 |
|
| 5442 |
[*Note 1*: Therefore, a function call cannot intervene between the
|
| 5443 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 5444 |
single compound assignment operator. — *end note*]
|
| 5445 |
|
|
|
|
| 5465 |
|
| 5466 |
When the left operand of an assignment operator is a bit-field that
|
| 5467 |
cannot represent the value of the expression, the resulting value of the
|
| 5468 |
bit-field is *implementation-defined*.
|
| 5469 |
|
| 5470 |
+
An assignment whose left operand is of a volatile-qualified type is
|
| 5471 |
+
deprecated [[depr.volatile.type]] unless the (possibly parenthesized)
|
| 5472 |
+
assignment is a discarded-value expression or an unevaluated operand
|
| 5473 |
+
[[term.unevaluated.operand]].
|
| 5474 |
|
| 5475 |
The behavior of an expression of the form `E1 op= E2` is equivalent to
|
| 5476 |
+
`E1 = E1 op E2` except that `E1` is evaluated only once.
|
| 5477 |
+
|
| 5478 |
+
[*Note 2*: The object designated by `E1` is accessed
|
| 5479 |
+
twice. — *end note*]
|
| 5480 |
+
|
| 5481 |
+
For `+=` and `-=`, `E1` shall either have arithmetic type or be a
|
| 5482 |
+
pointer to a possibly cv-qualified completely-defined object type. In
|
| 5483 |
+
all other cases, `E1` shall have arithmetic type.
|
| 5484 |
|
| 5485 |
If the value being stored in an object is read via another object that
|
| 5486 |
overlaps in any way the storage of the first object, then the overlap
|
| 5487 |
shall be exact and the two objects shall have the same type, otherwise
|
| 5488 |
the behavior is undefined.
|
| 5489 |
|
| 5490 |
+
[*Note 3*: This restriction applies to the relationship between the
|
| 5491 |
left and right sides of the assignment operation; it is not a statement
|
| 5492 |
+
about how the target of the assignment can be aliased in general. See
|
| 5493 |
[[basic.lval]]. — *end note*]
|
| 5494 |
|
| 5495 |
A *braced-init-list* may appear on the right-hand side of
|
| 5496 |
|
| 5497 |
- an assignment to a scalar, in which case the initializer list shall
|
| 5498 |
have at most a single element. The meaning of `x = {v}`, where `T` is
|
| 5499 |
the scalar type of the expression `x`, is that of `x = T{v}`. The
|
| 5500 |
meaning of `x = {}` is `x = T{}`.
|
| 5501 |
- an assignment to an object of class type, in which case the
|
| 5502 |
initializer list is passed as the argument to the assignment operator
|
| 5503 |
+
function selected by overload resolution [[over.ass]], [[over.match]].
|
|
|
|
| 5504 |
|
| 5505 |
[*Example 1*:
|
| 5506 |
|
| 5507 |
``` cpp
|
| 5508 |
complex<double> z;
|
|
|
|
| 5531 |
[[intro.execution]]. The type and value of the result are the type and
|
| 5532 |
value of the right operand; the result is of the same value category as
|
| 5533 |
its right operand, and is a bit-field if its right operand is a
|
| 5534 |
bit-field.
|
| 5535 |
|
| 5536 |
+
[*Note 1*:
|
| 5537 |
|
| 5538 |
+
In contexts where the comma token is given special meaning (e.g.,
|
| 5539 |
+
function calls [[expr.call]], subscript expressions [[expr.sub]], lists
|
| 5540 |
+
of initializers [[dcl.init]], or *template-argument-list*s
|
| 5541 |
+
[[temp.names]]), the comma operator as described in this subclause can
|
| 5542 |
+
appear only in parentheses.
|
| 5543 |
|
| 5544 |
+
[*Example 1*:
|
|
|
|
|
|
|
|
|
|
| 5545 |
|
| 5546 |
``` cpp
|
| 5547 |
f(a, (t=3, t+2), c);
|
| 5548 |
```
|
| 5549 |
|
| 5550 |
has three arguments, the second of which has the value `5`.
|
| 5551 |
|
| 5552 |
— *end example*]
|
| 5553 |
|
| 5554 |
+
— *end note*]
|
|
|
|
|
|
|
| 5555 |
|
| 5556 |
## Constant expressions <a id="expr.const">[[expr.const]]</a>
|
| 5557 |
|
| 5558 |
Certain contexts require expressions that satisfy additional
|
| 5559 |
requirements as detailed in this subclause; other contexts have
|
|
|
|
| 5576 |
some initialization being performed, and
|
| 5577 |
- the full-expression of its initialization is a constant expression
|
| 5578 |
when interpreted as a *constant-expression*, except that if `o` is an
|
| 5579 |
object, that full-expression may also invoke constexpr constructors
|
| 5580 |
for `o` and its subobjects even if those objects are of non-literal
|
| 5581 |
+
class types. \[*Note 2*: Such a class can have a non-trivial
|
| 5582 |
destructor. Within this evaluation, `std::is_constant_evaluated()`
|
| 5583 |
[[meta.const.eval]] returns `true`. — *end note*]
|
| 5584 |
|
| 5585 |
A variable is *potentially-constant* if it is constexpr or it has
|
| 5586 |
+
reference or non-volatile const-qualified integral or enumeration type.
|
| 5587 |
|
| 5588 |
+
A constant-initialized potentially-constant variable V is *usable in
|
| 5589 |
+
constant expressions* at a point P if V’s initializing declaration D is
|
| 5590 |
reachable from P and
|
| 5591 |
|
| 5592 |
+
- V is constexpr,
|
| 5593 |
+
- V is not initialized to a TU-local value, or
|
| 5594 |
- P is in the same translation unit as D.
|
| 5595 |
|
| 5596 |
An object or reference is *usable in constant expressions* if it is
|
| 5597 |
|
| 5598 |
- a variable that is usable in constant expressions, or
|
|
|
|
| 5605 |
|
| 5606 |
An expression E is a *core constant expression* unless the evaluation of
|
| 5607 |
E, following the rules of the abstract machine [[intro.execution]],
|
| 5608 |
would evaluate one of the following:
|
| 5609 |
|
| 5610 |
+
- `this` [[expr.prim.this]], except
|
| 5611 |
+
- in a constexpr function [[dcl.constexpr]] that is being evaluated as
|
| 5612 |
+
part of E or
|
| 5613 |
+
- when appearing as the *postfix-expression* of an implicit or
|
| 5614 |
+
explicit class member access expression [[expr.ref]];
|
| 5615 |
+
- a control flow that passes through a declaration of a variable with
|
| 5616 |
+
static [[basic.stc.static]] or thread [[basic.stc.thread]] storage
|
| 5617 |
+
duration, unless that variable is usable in constant expressions;
|
| 5618 |
+
\[*Example 1*:
|
| 5619 |
+
``` cpp
|
| 5620 |
+
constexpr char test() {
|
| 5621 |
+
static const int x = 5;
|
| 5622 |
+
static constexpr char c[] = "Hello World";
|
| 5623 |
+
return *(c + x);
|
| 5624 |
+
}
|
| 5625 |
+
static_assert(' ' == test());
|
| 5626 |
+
```
|
| 5627 |
+
|
| 5628 |
+
— *end example*]
|
| 5629 |
+
- an invocation of a non-constexpr function;[^32]
|
| 5630 |
- an invocation of an undefined constexpr function;
|
| 5631 |
+
- an invocation of an instantiated constexpr function that is not
|
| 5632 |
+
constexpr-suitable;
|
| 5633 |
- an invocation of a virtual function [[class.virtual]] for an object
|
| 5634 |
+
whose dynamic type is constexpr-unknown;
|
|
|
|
|
|
|
| 5635 |
- an expression that would exceed the implementation-defined limits (see
|
| 5636 |
[[implimits]]);
|
| 5637 |
- an operation that would have undefined behavior as specified in
|
| 5638 |
+
[[intro]] through [[cpp]], excluding [[dcl.attr.assume]];[^33]
|
|
|
|
|
|
|
|
|
|
| 5639 |
- an lvalue-to-rvalue conversion [[conv.lval]] unless it is applied to
|
| 5640 |
- a non-volatile glvalue that refers to an object that is usable in
|
| 5641 |
constant expressions, or
|
| 5642 |
- a non-volatile glvalue of literal type that refers to a non-volatile
|
| 5643 |
object whose lifetime began within the evaluation of E;
|
| 5644 |
+
- an lvalue-to-rvalue conversion that is applied to a glvalue that
|
| 5645 |
+
refers to a non-active member of a union or a subobject thereof;
|
|
|
|
| 5646 |
- an lvalue-to-rvalue conversion that is applied to an object with an
|
| 5647 |
indeterminate value [[basic.indet]];
|
| 5648 |
- an invocation of an implicitly-defined copy/move constructor or
|
| 5649 |
copy/move assignment operator for a union whose active member (if any)
|
| 5650 |
is mutable, unless the lifetime of the union object began within the
|
| 5651 |
evaluation of E;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5652 |
- in a *lambda-expression*, a reference to `this` or to a variable with
|
| 5653 |
automatic storage duration defined outside that *lambda-expression*,
|
| 5654 |
+
where the reference would be an odr-use
|
| 5655 |
+
[[term.odr.use]], [[expr.prim.lambda]];
|
| 5656 |
+
\[*Example 2*:
|
| 5657 |
``` cpp
|
| 5658 |
void g() {
|
| 5659 |
const int n = 0;
|
| 5660 |
[=] {
|
| 5661 |
constexpr int i = n; // OK, n is not odr-used here
|
|
|
|
| 5663 |
};
|
| 5664 |
}
|
| 5665 |
```
|
| 5666 |
|
| 5667 |
— *end example*]
|
| 5668 |
+
\[*Note 3*:
|
| 5669 |
If the odr-use occurs in an invocation of a function call operator of
|
| 5670 |
a closure type, it no longer refers to `this` or to an enclosing
|
| 5671 |
automatic variable due to the transformation
|
| 5672 |
[[expr.prim.lambda.capture]] of the *id-expression* into an access of
|
| 5673 |
the corresponding data member.
|
| 5674 |
+
\[*Example 3*:
|
| 5675 |
``` cpp
|
| 5676 |
auto monad = [](auto v) { return [=] { return v; }; };
|
| 5677 |
auto bind = [](auto m) {
|
| 5678 |
return [=](auto fvm) { return fvm(m()); };
|
| 5679 |
};
|
|
|
|
| 5684 |
|
| 5685 |
— *end example*]
|
| 5686 |
— *end note*]
|
| 5687 |
- a conversion from type cv `void*` to a pointer-to-object type;
|
| 5688 |
- a `reinterpret_cast` [[expr.reinterpret.cast]];
|
| 5689 |
+
- a modification of an object
|
| 5690 |
+
[[expr.ass]], [[expr.post.incr]], [[expr.pre.incr]] unless it is
|
| 5691 |
+
applied to a non-volatile lvalue of literal type that refers to a
|
| 5692 |
+
non-volatile object whose lifetime began within the evaluation of E;
|
| 5693 |
+
- an invocation of a destructor [[class.dtor]] or a function call whose
|
| 5694 |
+
*postfix-expression* names a pseudo-destructor [[expr.call]], in
|
| 5695 |
+
either case for an object whose lifetime did not begin within the
|
| 5696 |
+
evaluation of E;
|
| 5697 |
- a *new-expression* [[expr.new]], unless the selected allocation
|
| 5698 |
+
function is a replaceable global allocation function
|
| 5699 |
+
[[new.delete.single]], [[new.delete.array]] and the allocated storage
|
| 5700 |
is deallocated within the evaluation of E;
|
| 5701 |
- a *delete-expression* [[expr.delete]], unless it deallocates a region
|
| 5702 |
of storage allocated within the evaluation of E;
|
| 5703 |
- a call to an instance of `std::allocator<T>::allocate`
|
| 5704 |
[[allocator.members]], unless the allocated storage is deallocated
|
|
|
|
| 5708 |
allocated within the evaluation of E;
|
| 5709 |
- an *await-expression* [[expr.await]];
|
| 5710 |
- a *yield-expression* [[expr.yield]];
|
| 5711 |
- a three-way comparison [[expr.spaceship]], relational [[expr.rel]], or
|
| 5712 |
equality [[expr.eq]] operator where the result is unspecified;
|
| 5713 |
+
- a *throw-expression* [[expr.throw]];
|
| 5714 |
+
- a `dynamic_cast` [[expr.dynamic.cast]] or `typeid` [[expr.typeid]]
|
| 5715 |
+
expression on a glvalue that refers to an object whose dynamic type is
|
| 5716 |
+
constexpr-unknown or that would throw an exception;
|
| 5717 |
+
- an *asm-declaration* [[dcl.asm]];
|
| 5718 |
+
- an invocation of the `va_arg` macro [[cstdarg.syn]];
|
| 5719 |
+
- a non-constant library call [[defns.nonconst.libcall]]; or
|
| 5720 |
+
- a `goto` statement [[stmt.goto]].
|
| 5721 |
|
| 5722 |
+
It is unspecified whether E is a core constant expression if E satisfies
|
| 5723 |
+
the constraints of a core constant expression, but evaluation of E would
|
| 5724 |
+
evaluate
|
|
|
|
|
|
|
| 5725 |
|
| 5726 |
+
- an operation that has undefined behavior as specified in [[library]]
|
| 5727 |
+
through [[thread]],
|
| 5728 |
+
- an invocation of the `va_start` macro [[cstdarg.syn]], or
|
| 5729 |
+
- a statement with an assumption [[dcl.attr.assume]] whose converted
|
| 5730 |
+
*conditional-expression*, if evaluated where the assumption appears,
|
| 5731 |
+
would not disqualify E from being a core constant expression and would
|
| 5732 |
+
not evaluate to `true`. \[*Note 4*: E is not disqualified from being a
|
| 5733 |
+
core constant expression if the hypothetical evaluation of the
|
| 5734 |
+
converted *conditional-expression* would disqualify E from being a
|
| 5735 |
+
core constant expression. — *end note*]
|
| 5736 |
+
|
| 5737 |
+
[*Example 4*:
|
| 5738 |
|
| 5739 |
``` cpp
|
| 5740 |
int x; // not constant
|
| 5741 |
struct A {
|
| 5742 |
constexpr A(bool b) : m(b?42:x) { }
|
| 5743 |
int m;
|
| 5744 |
};
|
| 5745 |
+
constexpr int v = A(true).m; // OK, constructor call initializes m with the value 42
|
| 5746 |
|
| 5747 |
constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
|
| 5748 |
|
| 5749 |
constexpr int f1(int k) {
|
| 5750 |
constexpr int x = k; // error: x is not initialized by a constant expression
|
| 5751 |
// because lifetime of k began outside the initializer of x
|
| 5752 |
return x;
|
| 5753 |
}
|
| 5754 |
constexpr int f2(int k) {
|
| 5755 |
+
int x = k; // OK, not required to be a constant expression
|
| 5756 |
// because x is not constexpr
|
| 5757 |
return x;
|
| 5758 |
}
|
| 5759 |
|
| 5760 |
constexpr int incr(int &n) {
|
|
|
|
| 5764 |
constexpr int x = incr(k); // error: incr(k) is not a core constant expression
|
| 5765 |
// because lifetime of k began outside the expression incr(k)
|
| 5766 |
return x;
|
| 5767 |
}
|
| 5768 |
constexpr int h(int k) {
|
| 5769 |
+
int x = incr(k); // OK, incr(k) is not required to be a core constant expression
|
| 5770 |
return x;
|
| 5771 |
}
|
| 5772 |
+
constexpr int y = h(1); // OK, initializes y with the value 2
|
| 5773 |
// h(1) is a core constant expression because
|
| 5774 |
// the lifetime of k begins inside h(1)
|
| 5775 |
```
|
| 5776 |
|
| 5777 |
— *end example*]
|
| 5778 |
|
| 5779 |
For the purposes of determining whether an expression E is a core
|
| 5780 |
+
constant expression, the evaluation of the body of a member function of
|
| 5781 |
`std::allocator<T>` as defined in [[allocator.members]], where `T` is a
|
| 5782 |
+
literal type, is ignored. Similarly, the evaluation of the body of
|
| 5783 |
+
`std::construct_at` or `std::ranges::construct_at` is considered to
|
| 5784 |
+
include only the underlying constructor call if the first argument (of
|
| 5785 |
+
type `T*`) points to storage allocated with `std::allocator<T>` or to an
|
| 5786 |
+
object whose lifetime began within the evaluation of E.
|
| 5787 |
+
|
| 5788 |
+
For the purposes of determining whether E is a core constant expression,
|
| 5789 |
+
the evaluation of a call to a trivial copy/move constructor or copy/move
|
| 5790 |
+
assignment operator of a union is considered to copy/move the active
|
| 5791 |
+
member of the union, if any.
|
| 5792 |
+
|
| 5793 |
+
[*Note 5*: The copy/move of the active member is
|
| 5794 |
+
trivial. — *end note*]
|
| 5795 |
+
|
| 5796 |
+
During the evaluation of an expression E as a core constant expression,
|
| 5797 |
+
all *id-expression*s and uses of `*this` that refer to an object or
|
| 5798 |
+
reference whose lifetime did not begin with the evaluation of E are
|
| 5799 |
+
treated as referring to a specific instance of that object or reference
|
| 5800 |
+
whose lifetime and that of all subobjects (including all union members)
|
| 5801 |
+
includes the entire constant evaluation. For such an object that is not
|
| 5802 |
+
usable in constant expressions, the dynamic type of the object is
|
| 5803 |
+
*constexpr-unknown*. For such a reference that is not usable in constant
|
| 5804 |
+
expressions, the reference is treated as binding to an unspecified
|
| 5805 |
+
object of the referenced type whose lifetime and that of all subobjects
|
| 5806 |
+
includes the entire constant evaluation and whose dynamic type is
|
| 5807 |
+
constexpr-unknown.
|
| 5808 |
+
|
| 5809 |
+
[*Example 5*:
|
| 5810 |
+
|
| 5811 |
+
``` cpp
|
| 5812 |
+
template <typename T, size_t N>
|
| 5813 |
+
constexpr size_t array_size(T (&)[N]) {
|
| 5814 |
+
return N;
|
| 5815 |
+
}
|
| 5816 |
+
|
| 5817 |
+
void use_array(int const (&gold_medal_mel)[2]) {
|
| 5818 |
+
constexpr auto gold = array_size(gold_medal_mel); // OK
|
| 5819 |
+
}
|
| 5820 |
+
|
| 5821 |
+
constexpr auto olympic_mile() {
|
| 5822 |
+
const int ledecky = 1500;
|
| 5823 |
+
return []{ return ledecky; };
|
| 5824 |
+
}
|
| 5825 |
+
static_assert(olympic_mile()() == 1500); // OK
|
| 5826 |
+
|
| 5827 |
+
struct Swim {
|
| 5828 |
+
constexpr int phelps() { return 28; }
|
| 5829 |
+
virtual constexpr int lochte() { return 12; }
|
| 5830 |
+
int coughlin = 12;
|
| 5831 |
+
};
|
| 5832 |
+
|
| 5833 |
+
constexpr int how_many(Swim& swam) {
|
| 5834 |
+
Swim* p = &swam;
|
| 5835 |
+
return (p + 1 - 1)->phelps();
|
| 5836 |
+
}
|
| 5837 |
+
|
| 5838 |
+
void splash(Swim& swam) {
|
| 5839 |
+
static_assert(swam.phelps() == 28); // OK
|
| 5840 |
+
static_assert((&swam)->phelps() == 28); // OK
|
| 5841 |
+
|
| 5842 |
+
Swim* pswam = &swam;
|
| 5843 |
+
static_assert(pswam->phelps() == 28); // error: lvalue-to-rvalue conversion on a pointer
|
| 5844 |
+
// not usable in constant expressions
|
| 5845 |
+
|
| 5846 |
+
static_assert(how_many(swam) == 28); // OK
|
| 5847 |
+
static_assert(Swim().lochte() == 12); // OK
|
| 5848 |
+
|
| 5849 |
+
static_assert(swam.lochte() == 12); // error: invoking virtual function on reference
|
| 5850 |
+
// with constexpr-unknown dynamic type
|
| 5851 |
+
|
| 5852 |
+
static_assert(swam.coughlin == 12); // error: lvalue-to-rvalue conversion on an object
|
| 5853 |
+
// not usable in constant expressions
|
| 5854 |
+
}
|
| 5855 |
+
|
| 5856 |
+
extern Swim dc;
|
| 5857 |
+
extern Swim& trident;
|
| 5858 |
+
|
| 5859 |
+
constexpr auto& sandeno = typeid(dc); // OK, can only be typeid(Swim)
|
| 5860 |
+
constexpr auto& gallagher = typeid(trident); // error: constexpr-unknown dynamic type
|
| 5861 |
+
```
|
| 5862 |
+
|
| 5863 |
+
— *end example*]
|
| 5864 |
|
| 5865 |
An object `a` is said to have *constant destruction* if:
|
| 5866 |
|
| 5867 |
+
- it is not of class type nor (possibly multidimensional) array thereof,
|
| 5868 |
+
or
|
| 5869 |
+
- it is of class type or (possibly multidimensional) array thereof, that
|
| 5870 |
+
class type has a constexpr destructor, and for a hypothetical
|
| 5871 |
expression E whose only effect is to destroy `a`, E would be a core
|
| 5872 |
constant expression if the lifetime of `a` and its non-mutable
|
| 5873 |
subobjects (but not its mutable subobjects) were considered to start
|
| 5874 |
within E.
|
| 5875 |
|
| 5876 |
An *integral constant expression* is an expression of integral or
|
| 5877 |
unscoped enumeration type, implicitly converted to a prvalue, where the
|
| 5878 |
converted expression is a core constant expression.
|
| 5879 |
|
| 5880 |
+
[*Note 6*: Such expressions can be used as bit-field lengths
|
| 5881 |
[[class.bit]], as enumerator initializers if the underlying type is not
|
| 5882 |
fixed [[dcl.enum]], and as alignments [[dcl.align]]. — *end note*]
|
| 5883 |
|
| 5884 |
If an expression of literal class type is used in a context where an
|
| 5885 |
integral constant expression is required, then that expression is
|
| 5886 |
contextually implicitly converted [[conv]] to an integral or unscoped
|
| 5887 |
enumeration type and the selected conversion function shall be
|
| 5888 |
`constexpr`.
|
| 5889 |
|
| 5890 |
+
[*Example 6*:
|
| 5891 |
|
| 5892 |
``` cpp
|
| 5893 |
struct A {
|
| 5894 |
constexpr A(int i) : val(i) { }
|
| 5895 |
constexpr operator int() const { return val; }
|
|
|
|
| 5921 |
and
|
| 5922 |
- function pointer conversions [[conv.fctptr]],
|
| 5923 |
|
| 5924 |
and where the reference binding (if any) binds directly.
|
| 5925 |
|
| 5926 |
+
[*Note 7*: Such expressions can be used in `new` expressions
|
| 5927 |
[[expr.new]], as case expressions [[stmt.switch]], as enumerator
|
| 5928 |
initializers if the underlying type is fixed [[dcl.enum]], as array
|
| 5929 |
bounds [[dcl.array]], and as non-type template arguments
|
| 5930 |
[[temp.arg]]. — *end note*]
|
| 5931 |
|
|
|
|
| 5940 |
whose value satisfies the following constraints:
|
| 5941 |
|
| 5942 |
- if the value is an object of class type, each non-static data member
|
| 5943 |
of reference type refers to an entity that is a permitted result of a
|
| 5944 |
constant expression,
|
| 5945 |
+
- if the value is an object of scalar type, it does not have an
|
| 5946 |
+
indeterminate value [[basic.indet]],
|
| 5947 |
- if the value is of pointer type, it contains the address of an object
|
| 5948 |
with static storage duration, the address past the end of such an
|
| 5949 |
object [[expr.add]], the address of a non-immediate function, or a
|
| 5950 |
null pointer value,
|
| 5951 |
- if the value is of pointer-to-member-function type, it does not
|
|
|
|
| 5956 |
An entity is a *permitted result of a constant expression* if it is an
|
| 5957 |
object with static storage duration that either is not a temporary
|
| 5958 |
object or is a temporary object whose value satisfies the above
|
| 5959 |
constraints, or if it is a non-immediate function.
|
| 5960 |
|
| 5961 |
+
[*Note 8*: A glvalue core constant expression that either refers to or
|
| 5962 |
+
points to an unspecified object is not a constant
|
| 5963 |
+
expression. — *end note*]
|
| 5964 |
+
|
| 5965 |
+
[*Example 7*:
|
| 5966 |
|
| 5967 |
``` cpp
|
| 5968 |
consteval int f() { return 42; }
|
| 5969 |
consteval auto g() { return f; }
|
| 5970 |
consteval int h(int (*p)() = g()) { return p(); }
|
|
|
|
| 5973 |
// not a permitted result of a constant expression
|
| 5974 |
```
|
| 5975 |
|
| 5976 |
— *end example*]
|
| 5977 |
|
| 5978 |
+
*Recommended practice:* Implementations should provide consistent
|
| 5979 |
+
results of floating-point evaluations, irrespective of whether the
|
| 5980 |
+
evaluation is performed during translation or during program execution.
|
| 5981 |
+
|
| 5982 |
+
[*Note 9*:
|
| 5983 |
|
| 5984 |
Since this document imposes no restrictions on the accuracy of
|
| 5985 |
floating-point operations, it is unspecified whether the evaluation of a
|
| 5986 |
floating-point expression during translation yields the same result as
|
| 5987 |
the evaluation of the same expression (or the same operations on the
|
| 5988 |
+
same values) during program execution.
|
| 5989 |
|
| 5990 |
+
[*Example 8*:
|
| 5991 |
|
| 5992 |
``` cpp
|
| 5993 |
bool f() {
|
| 5994 |
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
|
| 5995 |
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
|
|
|
|
| 6002 |
— *end example*]
|
| 6003 |
|
| 6004 |
— *end note*]
|
| 6005 |
|
| 6006 |
An expression or conversion is in an *immediate function context* if it
|
| 6007 |
+
is potentially evaluated and either:
|
| 6008 |
+
|
| 6009 |
+
- its innermost enclosing non-block scope is a function parameter scope
|
| 6010 |
+
of an immediate function,
|
| 6011 |
+
- it is a subexpression of a manifestly constant-evaluated expression or
|
| 6012 |
+
conversion, or
|
| 6013 |
+
- its enclosing statement is enclosed [[stmt.pre]] by the
|
| 6014 |
+
*compound-statement* of a consteval if statement [[stmt.if]].
|
| 6015 |
+
|
| 6016 |
+
An invocation is an *immediate invocation* if it is a
|
| 6017 |
+
potentially-evaluated explicit or implicit invocation of an immediate
|
| 6018 |
+
function and is not in an immediate function context. An aggregate
|
| 6019 |
+
initialization is an immediate invocation if it evaluates a default
|
| 6020 |
+
member initializer that has a subexpression that is an
|
| 6021 |
+
immediate-escalating expression.
|
| 6022 |
+
|
| 6023 |
+
An expression or conversion is *immediate-escalating* if it is not
|
| 6024 |
+
initially in an immediate function context and it is either
|
| 6025 |
+
|
| 6026 |
+
- a potentially-evaluated *id-expression* that denotes an immediate
|
| 6027 |
+
function that is not a subexpression of an immediate invocation, or
|
| 6028 |
+
- an immediate invocation that is not a constant expression and is not a
|
| 6029 |
+
subexpression of an immediate invocation.
|
| 6030 |
+
|
| 6031 |
+
An *immediate-escalating* function is
|
| 6032 |
+
|
| 6033 |
+
- the call operator of a lambda that is not declared with the
|
| 6034 |
+
`consteval` specifier,
|
| 6035 |
+
- a defaulted special member function that is not declared with the
|
| 6036 |
+
`consteval` specifier, or
|
| 6037 |
+
- a function that results from the instantiation of a templated entity
|
| 6038 |
+
defined with the `constexpr` specifier.
|
| 6039 |
+
|
| 6040 |
+
An immediate-escalating expression shall appear only in an
|
| 6041 |
+
immediate-escalating function.
|
| 6042 |
+
|
| 6043 |
+
An *immediate function* is a function or constructor that is
|
| 6044 |
+
|
| 6045 |
+
- declared with the `consteval` specifier, or
|
| 6046 |
+
- an immediate-escalating function `F` whose function body contains an
|
| 6047 |
+
immediate-escalating expression `E` such that `E`’s innermost
|
| 6048 |
+
enclosing non-block scope is `F`’s function parameter scope.
|
| 6049 |
+
|
| 6050 |
+
[*Example 9*:
|
| 6051 |
+
|
| 6052 |
+
``` cpp
|
| 6053 |
+
consteval int id(int i) { return i; }
|
| 6054 |
+
constexpr char id(char c) { return c; }
|
| 6055 |
+
|
| 6056 |
+
template<class T>
|
| 6057 |
+
constexpr int f(T t) {
|
| 6058 |
+
return t + id(t);
|
| 6059 |
+
}
|
| 6060 |
+
|
| 6061 |
+
auto a = &f<char>; // OK, f<char> is not an immediate function
|
| 6062 |
+
auto b = &f<int>; // error: f<int> is an immediate function
|
| 6063 |
+
|
| 6064 |
+
static_assert(f(3) == 6); // OK
|
| 6065 |
+
|
| 6066 |
+
template<class T>
|
| 6067 |
+
constexpr int g(T t) { // g<int> is not an immediate function
|
| 6068 |
+
return t + id(42); // because id(42) is already a constant
|
| 6069 |
+
}
|
| 6070 |
+
|
| 6071 |
+
template<class T, class F>
|
| 6072 |
+
constexpr bool is_not(T t, F f) {
|
| 6073 |
+
return not f(t);
|
| 6074 |
+
}
|
| 6075 |
+
|
| 6076 |
+
consteval bool is_even(int i) { return i % 2 == 0; }
|
| 6077 |
+
|
| 6078 |
+
static_assert(is_not(5, is_even)); // OK
|
| 6079 |
+
|
| 6080 |
+
int x = 0;
|
| 6081 |
+
|
| 6082 |
+
template<class T>
|
| 6083 |
+
constexpr T h(T t = id(x)) { // h<int> is not an immediate function
|
| 6084 |
+
return t;
|
| 6085 |
+
}
|
| 6086 |
+
|
| 6087 |
+
template<class T>
|
| 6088 |
+
constexpr T hh() { // hh<int> is an immediate function
|
| 6089 |
+
return h<T>();
|
| 6090 |
+
}
|
| 6091 |
+
|
| 6092 |
+
int i = hh<int>(); // error: hh<int>() is an immediate-escalating expression
|
| 6093 |
+
// outside of an immediate-escalating function
|
| 6094 |
+
|
| 6095 |
+
struct A {
|
| 6096 |
+
int x;
|
| 6097 |
+
int y = id(x);
|
| 6098 |
+
};
|
| 6099 |
+
|
| 6100 |
+
template<class T>
|
| 6101 |
+
constexpr int k(int) { // k<int> is not an immediate function because A(42) is a
|
| 6102 |
+
return A(42).y; // constant expression and thus not immediate-escalating
|
| 6103 |
+
}
|
| 6104 |
+
```
|
| 6105 |
+
|
| 6106 |
+
— *end example*]
|
| 6107 |
|
| 6108 |
An expression or conversion is *manifestly constant-evaluated* if it is:
|
| 6109 |
|
| 6110 |
- a *constant-expression*, or
|
| 6111 |
- the condition of a constexpr if statement [[stmt.if]], or
|
| 6112 |
- an immediate invocation, or
|
| 6113 |
- the result of substitution into an atomic constraint expression to
|
| 6114 |
determine whether it is satisfied [[temp.constr.atomic]], or
|
| 6115 |
- the initializer of a variable that is usable in constant expressions
|
| 6116 |
+
or has constant initialization [[basic.start.static]].[^34]
|
| 6117 |
+
\[*Example 10*:
|
| 6118 |
``` cpp
|
| 6119 |
template<bool> struct X {};
|
| 6120 |
X<std::is_constant_evaluated()> x; // type X<true>
|
| 6121 |
int y;
|
| 6122 |
const int a = std::is_constant_evaluated() ? y : 1; // dynamic initialization to 1
|
|
|
|
| 6125 |
const int b = std::is_constant_evaluated() ? 2 : y; // static initialization to 2
|
| 6126 |
int c = y + (std::is_constant_evaluated() ? 2 : y); // dynamic initialization to y+y
|
| 6127 |
|
| 6128 |
constexpr int f() {
|
| 6129 |
const int n = std::is_constant_evaluated() ? 13 : 17; // n is 13
|
| 6130 |
+
int m = std::is_constant_evaluated() ? 13 : 17; // m can be 13 or 17 (see below)
|
| 6131 |
char arr[n] = {}; // char[13]
|
| 6132 |
return m + sizeof(arr);
|
| 6133 |
}
|
| 6134 |
int p = f(); // m is 13; initialized to 26
|
| 6135 |
int q = p + f(); // m is 17 for this call; initialized to 56
|
| 6136 |
```
|
| 6137 |
|
| 6138 |
— *end example*]
|
| 6139 |
|
| 6140 |
+
[*Note 10*: A manifestly constant-evaluated expression is evaluated
|
| 6141 |
+
even in an unevaluated operand
|
| 6142 |
+
[[term.unevaluated.operand]]. — *end note*]
|
| 6143 |
|
| 6144 |
An expression or conversion is *potentially constant evaluated* if it
|
| 6145 |
is:
|
| 6146 |
|
| 6147 |
- a manifestly constant-evaluated expression,
|
| 6148 |
- a potentially-evaluated expression [[basic.def.odr]],
|
| 6149 |
+
- an immediate subexpression of a *braced-init-list*,[^35]
|
| 6150 |
- an expression of the form `&` *cast-expression* that occurs within a
|
| 6151 |
+
templated entity,[^36] or
|
| 6152 |
+
- a potentially-evaluated subexpression [[intro.execution]] of one of
|
| 6153 |
+
the above.
|
| 6154 |
|
| 6155 |
A function or variable is *needed for constant evaluation* if it is:
|
| 6156 |
|
| 6157 |
- a constexpr function that is named by an expression [[basic.def.odr]]
|
| 6158 |
that is potentially constant evaluated, or
|
| 6159 |
+
- a potentially-constant variable named by a potentially constant
|
| 6160 |
+
evaluated expression.
|
|
|
|
| 6161 |
|
| 6162 |
<!-- Link reference definitions -->
|
| 6163 |
+
[allocator.members]: mem.md#allocator.members
|
| 6164 |
[bad.alloc]: support.md#bad.alloc
|
| 6165 |
[bad.cast]: support.md#bad.cast
|
| 6166 |
[bad.typeid]: support.md#bad.typeid
|
| 6167 |
[basic.align]: basic.md#basic.align
|
| 6168 |
[basic.compound]: basic.md#basic.compound
|
|
|
|
| 6170 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 6171 |
[basic.indet]: basic.md#basic.indet
|
| 6172 |
[basic.life]: basic.md#basic.life
|
| 6173 |
[basic.lookup]: basic.md#basic.lookup
|
| 6174 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 6175 |
+
[basic.lookup.general]: basic.md#basic.lookup.general
|
| 6176 |
+
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 6177 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 6178 |
[basic.lval]: #basic.lval
|
| 6179 |
+
[basic.pre]: basic.md#basic.pre
|
| 6180 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 6181 |
[basic.scope.class]: basic.md#basic.scope.class
|
| 6182 |
+
[basic.scope.lambda]: basic.md#basic.scope.lambda
|
| 6183 |
[basic.start.main]: basic.md#basic.start.main
|
| 6184 |
+
[basic.start.static]: basic.md#basic.start.static
|
| 6185 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 6186 |
[basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
|
| 6187 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 6188 |
+
[basic.stc.static]: basic.md#basic.stc.static
|
| 6189 |
+
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 6190 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
|
|
|
| 6191 |
[class]: class.md#class
|
| 6192 |
[class.abstract]: class.md#class.abstract
|
| 6193 |
[class.access]: class.md#class.access
|
| 6194 |
[class.access.base]: class.md#class.access.base
|
| 6195 |
[class.base.init]: class.md#class.base.init
|
|
|
|
| 6204 |
[class.derived]: class.md#class.derived
|
| 6205 |
[class.dtor]: class.md#class.dtor
|
| 6206 |
[class.free]: class.md#class.free
|
| 6207 |
[class.friend]: class.md#class.friend
|
| 6208 |
[class.mem]: class.md#class.mem
|
| 6209 |
+
[class.member.lookup]: basic.md#class.member.lookup
|
| 6210 |
[class.mfct]: class.md#class.mfct
|
| 6211 |
+
[class.mfct.non.static]: class.md#class.mfct.non.static
|
| 6212 |
[class.mi]: class.md#class.mi
|
| 6213 |
[class.prop]: class.md#class.prop
|
|
|
|
| 6214 |
[class.spaceship]: class.md#class.spaceship
|
| 6215 |
+
[class.static.mfct]: class.md#class.static.mfct
|
| 6216 |
[class.temporary]: basic.md#class.temporary
|
|
|
|
| 6217 |
[class.union]: class.md#class.union
|
| 6218 |
+
[class.union.anon]: class.md#class.union.anon
|
| 6219 |
[class.virtual]: class.md#class.virtual
|
| 6220 |
[cmp.categories]: support.md#cmp.categories
|
| 6221 |
+
[compare.syn]: support.md#compare.syn
|
| 6222 |
[conv]: #conv
|
| 6223 |
[conv.array]: #conv.array
|
| 6224 |
[conv.bool]: #conv.bool
|
| 6225 |
[conv.double]: #conv.double
|
| 6226 |
[conv.fctptr]: #conv.fctptr
|
| 6227 |
[conv.fpint]: #conv.fpint
|
| 6228 |
[conv.fpprom]: #conv.fpprom
|
| 6229 |
[conv.func]: #conv.func
|
| 6230 |
+
[conv.general]: #conv.general
|
| 6231 |
[conv.integral]: #conv.integral
|
| 6232 |
[conv.lval]: #conv.lval
|
| 6233 |
[conv.mem]: #conv.mem
|
| 6234 |
[conv.prom]: #conv.prom
|
| 6235 |
[conv.ptr]: #conv.ptr
|
|
|
|
| 6240 |
[cstdarg.syn]: support.md#cstdarg.syn
|
| 6241 |
[cstddef.syn]: support.md#cstddef.syn
|
| 6242 |
[dcl.align]: dcl.md#dcl.align
|
| 6243 |
[dcl.array]: dcl.md#dcl.array
|
| 6244 |
[dcl.asm]: dcl.md#dcl.asm
|
| 6245 |
+
[dcl.attr.assume]: dcl.md#dcl.attr.assume
|
| 6246 |
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 6247 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 6248 |
[dcl.decl]: dcl.md#dcl.decl
|
| 6249 |
[dcl.enum]: dcl.md#dcl.enum
|
| 6250 |
[dcl.fct]: dcl.md#dcl.fct
|
|
|
|
| 6264 |
[dcl.ref]: dcl.md#dcl.ref
|
| 6265 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 6266 |
[dcl.stc]: dcl.md#dcl.stc
|
| 6267 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 6268 |
[dcl.type]: dcl.md#dcl.type
|
| 6269 |
+
[dcl.type.auto.deduct]: dcl.md#dcl.type.auto.deduct
|
| 6270 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 6271 |
+
[dcl.type.decltype]: dcl.md#dcl.type.decltype
|
| 6272 |
+
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 6273 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 6274 |
[defns.access]: intro.md#defns.access
|
| 6275 |
+
[defns.nonconst.libcall]: intro.md#defns.nonconst.libcall
|
| 6276 |
[depr.arith.conv.enum]: future.md#depr.arith.conv.enum
|
| 6277 |
[depr.array.comp]: future.md#depr.array.comp
|
| 6278 |
[depr.capture.this]: future.md#depr.capture.this
|
|
|
|
| 6279 |
[depr.volatile.type]: future.md#depr.volatile.type
|
| 6280 |
[except]: except.md#except
|
| 6281 |
[except.handle]: except.md#except.handle
|
| 6282 |
[except.pre]: except.md#except.pre
|
| 6283 |
[except.spec]: except.md#except.spec
|
|
|
|
| 6306 |
[expr.mptr.oper]: #expr.mptr.oper
|
| 6307 |
[expr.mul]: #expr.mul
|
| 6308 |
[expr.new]: #expr.new
|
| 6309 |
[expr.or]: #expr.or
|
| 6310 |
[expr.post]: #expr.post
|
| 6311 |
+
[expr.post.general]: #expr.post.general
|
| 6312 |
[expr.post.incr]: #expr.post.incr
|
| 6313 |
[expr.pre]: #expr.pre
|
| 6314 |
[expr.pre.incr]: #expr.pre.incr
|
| 6315 |
[expr.prim]: #expr.prim
|
| 6316 |
[expr.prim.fold]: #expr.prim.fold
|
| 6317 |
[expr.prim.id]: #expr.prim.id
|
| 6318 |
[expr.prim.id.dtor]: #expr.prim.id.dtor
|
| 6319 |
+
[expr.prim.id.general]: #expr.prim.id.general
|
| 6320 |
[expr.prim.id.qual]: #expr.prim.id.qual
|
| 6321 |
[expr.prim.id.unqual]: #expr.prim.id.unqual
|
| 6322 |
[expr.prim.lambda]: #expr.prim.lambda
|
| 6323 |
[expr.prim.lambda.capture]: #expr.prim.lambda.capture
|
| 6324 |
[expr.prim.lambda.closure]: #expr.prim.lambda.closure
|
| 6325 |
+
[expr.prim.lambda.general]: #expr.prim.lambda.general
|
| 6326 |
[expr.prim.literal]: #expr.prim.literal
|
| 6327 |
[expr.prim.paren]: #expr.prim.paren
|
| 6328 |
[expr.prim.req]: #expr.prim.req
|
| 6329 |
[expr.prim.req.compound]: #expr.prim.req.compound
|
| 6330 |
+
[expr.prim.req.general]: #expr.prim.req.general
|
| 6331 |
[expr.prim.req.nested]: #expr.prim.req.nested
|
| 6332 |
[expr.prim.req.simple]: #expr.prim.req.simple
|
| 6333 |
[expr.prim.req.type]: #expr.prim.req.type
|
| 6334 |
[expr.prim.this]: #expr.prim.this
|
| 6335 |
[expr.prop]: #expr.prop
|
|
|
|
| 6344 |
[expr.throw]: #expr.throw
|
| 6345 |
[expr.type]: #expr.type
|
| 6346 |
[expr.type.conv]: #expr.type.conv
|
| 6347 |
[expr.typeid]: #expr.typeid
|
| 6348 |
[expr.unary]: #expr.unary
|
| 6349 |
+
[expr.unary.general]: #expr.unary.general
|
| 6350 |
[expr.unary.noexcept]: #expr.unary.noexcept
|
| 6351 |
[expr.unary.op]: #expr.unary.op
|
| 6352 |
[expr.xor]: #expr.xor
|
| 6353 |
[expr.yield]: #expr.yield
|
| 6354 |
[function.objects]: utilities.md#function.objects
|
|
|
|
| 6360 |
[lex.ext]: lex.md#lex.ext
|
| 6361 |
[lex.icon]: lex.md#lex.icon
|
| 6362 |
[lex.literal]: lex.md#lex.literal
|
| 6363 |
[lex.string]: lex.md#lex.string
|
| 6364 |
[library]: library.md#library
|
| 6365 |
+
[meta.const.eval]: meta.md#meta.const.eval
|
| 6366 |
+
[namespace.udecl]: dcl.md#namespace.udecl
|
| 6367 |
[new.badlength]: support.md#new.badlength
|
| 6368 |
[new.delete.array]: support.md#new.delete.array
|
| 6369 |
[new.delete.placement]: support.md#new.delete.placement
|
| 6370 |
[new.delete.single]: support.md#new.delete.single
|
| 6371 |
[over]: over.md#over
|
| 6372 |
[over.ass]: over.md#over.ass
|
| 6373 |
[over.best.ics]: over.md#over.best.ics
|
| 6374 |
[over.built]: over.md#over.built
|
| 6375 |
[over.call]: over.md#over.call
|
| 6376 |
+
[over.call.func]: over.md#over.call.func
|
| 6377 |
[over.ics.user]: over.md#over.ics.user
|
| 6378 |
[over.literal]: over.md#over.literal
|
| 6379 |
[over.match]: over.md#over.match
|
| 6380 |
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 6381 |
[over.match.oper]: over.md#over.match.oper
|
| 6382 |
[over.match.viable]: over.md#over.match.viable
|
| 6383 |
[over.oper]: over.md#over.oper
|
| 6384 |
[over.over]: over.md#over.over
|
| 6385 |
+
[over.sub]: over.md#over.sub
|
| 6386 |
[replacement.functions]: library.md#replacement.functions
|
| 6387 |
[special]: class.md#special
|
| 6388 |
+
[std.modules]: library.md#std.modules
|
| 6389 |
+
[stmt.goto]: stmt.md#stmt.goto
|
| 6390 |
[stmt.if]: stmt.md#stmt.if
|
| 6391 |
[stmt.iter]: stmt.md#stmt.iter
|
| 6392 |
[stmt.jump]: stmt.md#stmt.jump
|
| 6393 |
+
[stmt.pre]: stmt.md#stmt.pre
|
| 6394 |
[stmt.return]: stmt.md#stmt.return
|
| 6395 |
+
[stmt.return.coroutine]: stmt.md#stmt.return.coroutine
|
| 6396 |
[stmt.switch]: stmt.md#stmt.switch
|
| 6397 |
[support.runtime]: support.md#support.runtime
|
| 6398 |
[support.types.layout]: support.md#support.types.layout
|
| 6399 |
[temp.arg]: temp.md#temp.arg
|
| 6400 |
[temp.concept]: temp.md#temp.concept
|
|
|
|
| 6402 |
[temp.constr.constr]: temp.md#temp.constr.constr
|
| 6403 |
[temp.constr.decl]: temp.md#temp.constr.decl
|
| 6404 |
[temp.dep.constexpr]: temp.md#temp.dep.constexpr
|
| 6405 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 6406 |
[temp.explicit]: temp.md#temp.explicit
|
| 6407 |
+
[temp.mem]: temp.md#temp.mem
|
| 6408 |
[temp.names]: temp.md#temp.names
|
| 6409 |
+
[temp.over.link]: temp.md#temp.over.link
|
| 6410 |
[temp.param]: temp.md#temp.param
|
| 6411 |
[temp.pre]: temp.md#temp.pre
|
| 6412 |
[temp.res]: temp.md#temp.res
|
| 6413 |
+
[temp.spec.partial]: temp.md#temp.spec.partial
|
| 6414 |
[temp.variadic]: temp.md#temp.variadic
|
| 6415 |
+
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 6416 |
+
[term.object.representation]: basic.md#term.object.representation
|
| 6417 |
+
[term.odr.use]: basic.md#term.odr.use
|
| 6418 |
+
[term.unevaluated.operand]: #term.unevaluated.operand
|
| 6419 |
[thread]: thread.md#thread
|
| 6420 |
[type.info]: support.md#type.info
|
| 6421 |
+
[typeinfo.syn]: support.md#typeinfo.syn
|
| 6422 |
|
| 6423 |
[^1]: The precedence of operators is not directly specified, but it can
|
| 6424 |
be derived from the syntax.
|
| 6425 |
|
| 6426 |
[^2]: Overloaded operators are never assumed to be associative or
|
|
|
|
| 6429 |
[^3]: The cast and assignment operators must still perform their
|
| 6430 |
specific conversions as described in [[expr.type.conv]],
|
| 6431 |
[[expr.cast]], [[expr.static.cast]] and [[expr.ass]].
|
| 6432 |
|
| 6433 |
[^4]: The intent of this list is to specify those circumstances in which
|
| 6434 |
+
an object can or cannot be aliased.
|
| 6435 |
|
| 6436 |
[^5]: For historical reasons, this conversion is called the
|
| 6437 |
“lvalue-to-rvalue” conversion, even though that name does not
|
| 6438 |
accurately reflect the taxonomy of expressions described in
|
| 6439 |
[[basic.lval]].
|
|
|
|
| 6447 |
be obtained.
|
| 6448 |
|
| 6449 |
[^8]: The rule for conversion of pointers to members (from pointer to
|
| 6450 |
member of base to pointer to member of derived) appears inverted
|
| 6451 |
compared to the rule for pointers to objects (from pointer to
|
| 6452 |
+
derived to pointer to base) [[conv.ptr]], [[class.derived]]. This
|
| 6453 |
inversion is necessary to ensure type safety. Note that a pointer to
|
| 6454 |
member is not an object pointer or a function pointer and the rules
|
| 6455 |
for conversions of such pointers do not apply to pointers to
|
| 6456 |
members. In particular, a pointer to member cannot be converted to a
|
| 6457 |
`void*`.
|
| 6458 |
|
| 6459 |
[^9]: As a consequence, operands of type `bool`, `char8_t`, `char16_t`,
|
| 6460 |
+
`char32_t`, `wchar_t`, or of enumeration type are converted to some
|
| 6461 |
integral type.
|
| 6462 |
|
| 6463 |
[^10]: This also applies when the object expression is an implicit
|
| 6464 |
+
`(*this)` [[class.mfct.non.static]].
|
| 6465 |
|
| 6466 |
[^11]: This is true even if the subscript operator is used in the
|
| 6467 |
following common idiom: `&x[0]`.
|
| 6468 |
|
| 6469 |
[^12]: If the class member access expression is evaluated, the
|
|
|
|
| 6480 |
[^15]: The recommended name for such a class is `extended_type_info`.
|
| 6481 |
|
| 6482 |
[^16]: If `p` is an expression of pointer type, then `*p`, `(*p)`,
|
| 6483 |
`*(p)`, `((*p))`, `*((p))`, and so on all meet this requirement.
|
| 6484 |
|
| 6485 |
+
[^17]: The types can have different cv-qualifiers, subject to the
|
| 6486 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 6487 |
constness.
|
| 6488 |
|
| 6489 |
+
[^18]: `T1` and `T2` can have different cv-qualifiers, subject to the
|
| 6490 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 6491 |
constness.
|
| 6492 |
|
| 6493 |
+
[^19]: This is sometimes referred to as a type pun when the result
|
| 6494 |
refers to the same object as the source glvalue.
|
| 6495 |
|
| 6496 |
+
[^20]: `const_cast` is not limited to conversions that cast away a
|
| 6497 |
+
const-qualifier.
|
| 6498 |
|
| 6499 |
+
[^21]: `sizeof``(``bool``)` is not required to be `1`.
|
| 6500 |
|
| 6501 |
+
[^22]: The actual size of a potentially-overlapping subobject can be
|
|
|
|
|
|
|
| 6502 |
less than the result of applying `sizeof` to the subobject, due to
|
| 6503 |
virtual base classes and less strict padding requirements on
|
| 6504 |
potentially-overlapping subobjects.
|
| 6505 |
|
| 6506 |
[^23]: If the conversion function returns a signed integer type, the
|
| 6507 |
second standard conversion converts to the unsigned type
|
| 6508 |
`std::size_t` and thus thwarts any attempt to detect a negative
|
| 6509 |
value afterwards.
|
| 6510 |
|
| 6511 |
+
[^24]: This can include evaluating a *new-initializer* and/or calling a
|
| 6512 |
constructor.
|
| 6513 |
|
| 6514 |
[^25]: A *lambda-expression* with a *lambda-introducer* that consists of
|
| 6515 |
empty square brackets can follow the `delete` keyword if the
|
| 6516 |
*lambda-expression* is enclosed in parentheses.
|
|
|
|
| 6538 |
|
| 6539 |
[^31]: As specified in [[basic.compound]], an object that is not an
|
| 6540 |
array element is considered to belong to a single-element array for
|
| 6541 |
this purpose.
|
| 6542 |
|
| 6543 |
+
[^32]: Overload resolution [[over.match]] is applied as usual.
|
|
|
|
|
|
|
| 6544 |
|
| 6545 |
+
[^33]: This includes, for example, signed integer overflow [[expr.pre]],
|
| 6546 |
+
certain pointer arithmetic [[expr.add]], division by zero
|
| 6547 |
+
[[expr.mul]], or certain shift operations [[expr.shift]].
|
| 6548 |
+
|
| 6549 |
+
[^34]: Testing this condition can involve a trial evaluation of its
|
| 6550 |
initializer as described above.
|
| 6551 |
|
| 6552 |
+
[^35]: In some cases, constant evaluation is needed to determine whether
|
| 6553 |
+
a narrowing conversion is performed [[dcl.init.list]].
|
| 6554 |
|
| 6555 |
+
[^36]: In some cases, constant evaluation is needed to determine whether
|
| 6556 |
+
such an expression is value-dependent [[temp.dep.constexpr]].
|