- tmp/tmpclfsgmur/{from.md → to.md} +3203 -1578
tmp/tmpclfsgmur/{from.md → to.md}
RENAMED
|
@@ -1,67 +1,159 @@
|
|
| 1 |
# Expressions <a id="expr">[[expr]]</a>
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
value and can cause side effects. — *end note*]
|
| 7 |
|
| 8 |
[*Note 2*: Operators can be overloaded, that is, given meaning when
|
| 9 |
-
applied to expressions of class type
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
but the requirements of operand type and value category are replaced by
|
| 14 |
the rules for function call. Relations between operators, such as `++a`
|
| 15 |
-
meaning `a+=1`, are not guaranteed for overloaded operators
|
| 16 |
-
[[over.oper]]
|
| 17 |
|
| 18 |
-
|
| 19 |
-
for which they have not been overloaded. Operator
|
| 20 |
-
modify the rules for the *built-in operators*,
|
| 21 |
-
applied to types for which they are defined by
|
| 22 |
-
these built-in operators participate in overload
|
| 23 |
-
of that process user-defined conversions will be
|
| 24 |
-
necessary to convert the operands to types appropriate
|
| 25 |
-
operator. If a built-in operator is selected, such
|
| 26 |
-
applied to the operands before the operation is
|
| 27 |
-
according to the rules in
|
| 28 |
-
[[over.built]].
|
| 29 |
|
| 30 |
If during the evaluation of an expression, the result is not
|
| 31 |
mathematically defined or not in the range of representable values for
|
| 32 |
its type, the behavior is undefined.
|
| 33 |
|
| 34 |
[*Note 3*: Treatment of division by zero, forming a remainder using a
|
| 35 |
-
zero divisor, and all floating-point exceptions
|
| 36 |
-
is sometimes adjustable by a library function. — *end note*]
|
| 37 |
|
| 38 |
-
|
| 39 |
-
[[dcl.ref]], [[dcl.init.ref]]), the type is adjusted to `T` prior to
|
| 40 |
-
any further analysis. The expression designates the object or function
|
| 41 |
-
denoted by the reference, and the expression is an lvalue or an xvalue,
|
| 42 |
-
depending on the expression.
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
An expression is an xvalue if it is:
|
| 55 |
|
| 56 |
- the result of calling a function, whether implicitly or explicitly,
|
| 57 |
-
whose return type is an rvalue reference to object type,
|
| 58 |
-
- a cast to an rvalue reference to object type,
|
|
|
|
|
|
|
|
|
|
| 59 |
- a class member access expression designating a non-static data member
|
| 60 |
-
of non-reference type in which the object expression is an xvalue
|
|
|
|
| 61 |
- a `.*` pointer-to-member expression in which the first operand is an
|
| 62 |
-
xvalue and the second operand is a pointer to data member
|
|
|
|
| 63 |
|
| 64 |
In general, the effect of this rule is that named rvalue references are
|
| 65 |
treated as lvalues and unnamed rvalue references to objects are treated
|
| 66 |
as xvalues; rvalue references to functions are treated as lvalues
|
| 67 |
whether named or not.
|
|
@@ -84,54 +176,649 @@ A&& ar = static_cast<A&&>(a);
|
|
| 84 |
The expressions `f()`, `f().m`, `static_cast<A&&>(a)`, and `a + a` are
|
| 85 |
xvalues. The expression `ar` is an lvalue.
|
| 86 |
|
| 87 |
— *end example*]
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
[*Note
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
[[
|
| 98 |
|
| 99 |
-
Whenever a glvalue
|
| 100 |
-
|
| 101 |
-
[[conv.
|
| 102 |
-
|
| 103 |
-
convert the expression to a prvalue.
|
| 104 |
|
| 105 |
-
[*Note
|
|
|
|
|
|
|
|
|
|
| 106 |
expression of non-class type when the expression is converted to a
|
| 107 |
-
prvalue, an lvalue
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
Many binary operators that expect operands of arithmetic or enumeration
|
| 117 |
type cause conversions and yield result types in a similar way. The
|
| 118 |
purpose is to yield a common type, which is also the type of the result.
|
| 119 |
This pattern is called the *usual arithmetic conversions*, which are
|
| 120 |
defined as follows:
|
| 121 |
|
| 122 |
-
- If either operand is of scoped enumeration type
|
| 123 |
conversions are performed; if the other operand does not have the same
|
| 124 |
type, the expression is ill-formed.
|
| 125 |
- If either operand is of type `long double`, the other shall be
|
| 126 |
converted to `long double`.
|
| 127 |
- Otherwise, if either operand is `double`, the other shall be converted
|
| 128 |
to `double`.
|
| 129 |
- Otherwise, if either operand is `float`, the other shall be converted
|
| 130 |
to `float`.
|
| 131 |
-
- Otherwise, the integral promotions
|
| 132 |
-
|
| 133 |
promoted operands:
|
| 134 |
- If both operands have the same type, no further conversion is
|
| 135 |
needed.
|
| 136 |
- Otherwise, if both operands have signed integer types or both have
|
| 137 |
unsigned integer types, the operand with the type of lesser integer
|
|
@@ -147,120 +834,40 @@ defined as follows:
|
|
| 147 |
converted to the type of the operand with signed integer type.
|
| 148 |
- Otherwise, both operands shall be converted to the unsigned integer
|
| 149 |
type corresponding to the type of the operand with signed integer
|
| 150 |
type.
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
[[conv.func]]) standard conversions are not applied. The
|
| 156 |
-
lvalue-to-rvalue conversion ([[conv.lval]]) is applied if and only if
|
| 157 |
-
the expression is a glvalue of volatile-qualified type and it is one of
|
| 158 |
-
the following:
|
| 159 |
-
|
| 160 |
-
- `(` *expression* `)`, where *expression* is one of these expressions,
|
| 161 |
-
- *id-expression* ([[expr.prim.id]]),
|
| 162 |
-
- subscripting ([[expr.sub]]),
|
| 163 |
-
- class member access ([[expr.ref]]),
|
| 164 |
-
- indirection ([[expr.unary.op]]),
|
| 165 |
-
- pointer-to-member operation ([[expr.mptr.oper]]),
|
| 166 |
-
- conditional expression ([[expr.cond]]) where both the second and the
|
| 167 |
-
third operands are one of these expressions, or
|
| 168 |
-
- comma expression ([[expr.comma]]) where the right operand is one of
|
| 169 |
-
these expressions.
|
| 170 |
-
|
| 171 |
-
[*Note 8*: Using an overloaded operator causes a function call; the
|
| 172 |
-
above covers only operators with built-in meaning. — *end note*]
|
| 173 |
-
|
| 174 |
-
If the expression is a prvalue after this optional conversion, the
|
| 175 |
-
temporary materialization conversion ([[conv.rval]]) is applied.
|
| 176 |
-
|
| 177 |
-
[*Note 9*: If the expression is an lvalue of class type, it must have a
|
| 178 |
-
volatile copy constructor to initialize the temporary that is the result
|
| 179 |
-
object of the lvalue-to-rvalue conversion. — *end note*]
|
| 180 |
-
|
| 181 |
-
The glvalue expression is evaluated and its value is discarded.
|
| 182 |
-
|
| 183 |
-
The values of the floating operands and the results of floating
|
| 184 |
-
expressions may be represented in greater precision and range than that
|
| 185 |
-
required by the type; the types are not changed thereby.[^3]
|
| 186 |
-
|
| 187 |
-
The *cv-combined type* of two types `T1` and `T2` is a type `T3` similar
|
| 188 |
-
to `T1` whose cv-qualification signature ([[conv.qual]]) is:
|
| 189 |
-
|
| 190 |
-
- for every i > 0, cv³ᵢ is the union of cv¹ᵢ and cv²ᵢ;
|
| 191 |
-
- if the resulting cv³ᵢ is different from cv¹ᵢ or cv²ᵢ, then `const` is
|
| 192 |
-
added to every cv³ₖ for 0 < k < i.
|
| 193 |
-
|
| 194 |
-
[*Note 10*: Given similar types `T1` and `T2`, this construction
|
| 195 |
-
ensures that both can be converted to `T3`. — *end note*]
|
| 196 |
-
|
| 197 |
-
The *composite pointer type* of two operands `p1` and `p2` having types
|
| 198 |
-
`T1` and `T2`, respectively, where at least one is a pointer or pointer
|
| 199 |
-
to member type or `std::nullptr_t`, is:
|
| 200 |
-
|
| 201 |
-
- if both `p1` and `p2` are null pointer constants, `std::nullptr_t`;
|
| 202 |
-
- if either `p1` or `p2` is a null pointer constant, `T2` or `T1`,
|
| 203 |
-
respectively;
|
| 204 |
-
- if `T1` or `T2` is “pointer to *cv1* `void`” and the other type is
|
| 205 |
-
“pointer to *cv2* T”, where `T` is an object type or `void`, “pointer
|
| 206 |
-
to *cv12* `void`”, where *cv12* is the union of *cv1* and *cv2*;
|
| 207 |
-
- if `T1` or `T2` is “pointer to `noexcept` function” and the other type
|
| 208 |
-
is “pointer to function”, where the function types are otherwise the
|
| 209 |
-
same, “pointer to function”;
|
| 210 |
-
- if `T1` is “pointer to *cv1* `C1`” and `T2` is “pointer to *cv2*
|
| 211 |
-
`C2`”, where `C1` is reference-related to `C2` or `C2` is
|
| 212 |
-
reference-related to `C1` ([[dcl.init.ref]]), the cv-combined type of
|
| 213 |
-
`T1` and `T2` or the cv-combined type of `T2` and `T1`, respectively;
|
| 214 |
-
- if `T1` is “pointer to member of `C1` of type *cv1* `U1`” and `T2` is
|
| 215 |
-
“pointer to member of `C2` of type *cv2* `U2`” where `C1` is
|
| 216 |
-
reference-related to `C2` or `C2` is reference-related to `C1` (
|
| 217 |
-
[[dcl.init.ref]]), the cv-combined type of `T2` and `T1` or the
|
| 218 |
-
cv-combined type of `T1` and `T2`, respectively;
|
| 219 |
-
- if `T1` and `T2` are similar types ([[conv.qual]]), the cv-combined
|
| 220 |
-
type of `T1` and `T2`;
|
| 221 |
-
- otherwise, a program that necessitates the determination of a
|
| 222 |
-
composite pointer type is ill-formed.
|
| 223 |
-
|
| 224 |
-
[*Example 2*:
|
| 225 |
-
|
| 226 |
-
``` cpp
|
| 227 |
-
typedef void *p;
|
| 228 |
-
typedef const int *q;
|
| 229 |
-
typedef int **pi;
|
| 230 |
-
typedef const int **pci;
|
| 231 |
-
```
|
| 232 |
-
|
| 233 |
-
The composite pointer type of `p` and `q` is “pointer to `const void`”;
|
| 234 |
-
the composite pointer type of `pi` and `pci` is “pointer to `const`
|
| 235 |
-
pointer to `const int`”.
|
| 236 |
-
|
| 237 |
-
— *end example*]
|
| 238 |
|
| 239 |
## Primary expressions <a id="expr.prim">[[expr.prim]]</a>
|
| 240 |
|
| 241 |
``` bnf
|
| 242 |
primary-expression:
|
| 243 |
literal
|
| 244 |
-
|
| 245 |
'(' expression ')'
|
| 246 |
id-expression
|
| 247 |
lambda-expression
|
| 248 |
fold-expression
|
|
|
|
| 249 |
```
|
| 250 |
|
| 251 |
### Literals <a id="expr.prim.literal">[[expr.prim.literal]]</a>
|
| 252 |
|
| 253 |
-
A *literal* is a primary expression.
|
| 254 |
-
[[lex.literal]]
|
| 255 |
-
|
|
|
|
|
|
|
| 256 |
|
| 257 |
### This <a id="expr.prim.this">[[expr.prim.this]]</a>
|
| 258 |
|
| 259 |
The keyword `this` names a pointer to the object for which a non-static
|
| 260 |
-
member function
|
| 261 |
-
|
| 262 |
|
| 263 |
If a declaration declares a member function or member function template
|
| 264 |
of a class `X`, the expression `this` is a prvalue of type “pointer to
|
| 265 |
*cv-qualifier-seq* `X`” between the optional *cv-qualifier-seq* and the
|
| 266 |
end of the *function-definition*, *member-declarator*, or *declarator*.
|
|
@@ -270,16 +877,15 @@ its type and value category are defined within a static member function
|
|
| 270 |
as they are within a non-static member function).
|
| 271 |
|
| 272 |
[*Note 1*: This is because declaration matching does not occur until
|
| 273 |
the complete declarator is known. — *end note*]
|
| 274 |
|
| 275 |
-
|
| 276 |
-
to be of complete type for purposes of class member access (
|
| 277 |
-
[[expr.ref]]) outside the member function body.
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
|
|
|
| 281 |
|
| 282 |
[*Example 1*:
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
struct A {
|
|
@@ -290,15 +896,16 @@ struct A {
|
|
| 290 |
template auto A::f(int t) -> decltype(t + g());
|
| 291 |
```
|
| 292 |
|
| 293 |
— *end example*]
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
[[class.mem]]
|
| 299 |
-
|
|
|
|
| 300 |
|
| 301 |
The expression `this` shall not appear in any other context.
|
| 302 |
|
| 303 |
[*Example 2*:
|
| 304 |
|
|
@@ -320,14 +927,13 @@ class Outer {
|
|
| 320 |
— *end example*]
|
| 321 |
|
| 322 |
### Parentheses <a id="expr.prim.paren">[[expr.prim.paren]]</a>
|
| 323 |
|
| 324 |
A parenthesized expression `(E)` is a primary expression whose type,
|
| 325 |
-
value, and value category are identical to those of
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
otherwise indicated.
|
| 329 |
|
| 330 |
### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
|
| 331 |
|
| 332 |
``` bnf
|
| 333 |
id-expression:
|
|
@@ -335,20 +941,20 @@ id-expression:
|
|
| 335 |
qualified-id
|
| 336 |
```
|
| 337 |
|
| 338 |
An *id-expression* is a restricted form of a *primary-expression*.
|
| 339 |
|
| 340 |
-
[*Note 1*: An *id-expression* can appear after `.` and `->` operators
|
| 341 |
-
[[expr.ref]]
|
| 342 |
|
| 343 |
An *id-expression* that denotes a non-static data member or non-static
|
| 344 |
member function of a class can only be used:
|
| 345 |
|
| 346 |
-
- as part of a class member access
|
| 347 |
-
expression refers to the member’s class[^
|
| 348 |
that class, or
|
| 349 |
-
- to form a pointer to member
|
| 350 |
- if that *id-expression* denotes a non-static data member and it
|
| 351 |
appears in an unevaluated operand.
|
| 352 |
\[*Example 1*:
|
| 353 |
``` cpp
|
| 354 |
struct S {
|
|
@@ -358,124 +964,234 @@ member function of a class can only be used:
|
|
| 358 |
int j = sizeof(S::m + 42); // OK
|
| 359 |
```
|
| 360 |
|
| 361 |
— *end example*]
|
| 362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
#### Unqualified names <a id="expr.prim.id.unqual">[[expr.prim.id.unqual]]</a>
|
| 364 |
|
| 365 |
``` bnf
|
| 366 |
unqualified-id:
|
| 367 |
identifier
|
| 368 |
operator-function-id
|
| 369 |
conversion-function-id
|
| 370 |
literal-operator-id
|
| 371 |
-
'~'
|
| 372 |
'~' decltype-specifier
|
| 373 |
template-id
|
| 374 |
```
|
| 375 |
|
| 376 |
-
An *identifier* is an *id-expression*
|
| 377 |
-
declared
|
|
|
|
|
|
|
| 378 |
|
| 379 |
[*Note 1*: For *operator-function-id*s, see [[over.oper]]; for
|
| 380 |
*conversion-function-id*s, see [[class.conv.fct]]; for
|
| 381 |
*literal-operator-id*s, see [[over.literal]]; for *template-id*s, see
|
| 382 |
-
[[temp.names]]. A *
|
| 383 |
-
denotes
|
| 384 |
-
non-static member function, an *identifier*
|
| 385 |
-
member is transformed to a class member access
|
| 386 |
-
[[class.mfct.non-static]]). — *end note*]
|
| 387 |
-
|
| 388 |
-
The
|
| 389 |
-
|
| 390 |
-
the
|
| 391 |
-
|
| 392 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
#### Qualified names <a id="expr.prim.id.qual">[[expr.prim.id.qual]]</a>
|
| 395 |
|
| 396 |
``` bnf
|
| 397 |
qualified-id:
|
| 398 |
-
nested-name-specifier
|
| 399 |
```
|
| 400 |
|
| 401 |
``` bnf
|
| 402 |
nested-name-specifier:
|
| 403 |
'::'
|
| 404 |
type-name '::'
|
| 405 |
namespace-name '::'
|
| 406 |
decltype-specifier '::'
|
| 407 |
nested-name-specifier identifier '::'
|
| 408 |
-
nested-name-specifier
|
| 409 |
```
|
| 410 |
|
| 411 |
The type denoted by a *decltype-specifier* in a *nested-name-specifier*
|
| 412 |
shall be a class or enumeration type.
|
| 413 |
|
| 414 |
A *nested-name-specifier* that denotes a class, optionally followed by
|
| 415 |
-
the keyword `template`
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
|
| 423 |
[*Note 1*: A class member can be referred to using a *qualified-id* at
|
| 424 |
-
any point in its potential scope
|
| 425 |
-
[[basic.scope.class]]). — *end note*]
|
| 426 |
|
| 427 |
-
Where *
|
| 428 |
-
|
| 429 |
-
[[
|
| 430 |
-
|
| 431 |
-
*
|
| 432 |
-
|
| 433 |
-
[*Note 2*: A *typedef-name* that names a class is a *class-name* (
|
| 434 |
-
[[class.name]]). — *end note*]
|
| 435 |
|
| 436 |
The *nested-name-specifier* `::` names the global namespace. A
|
| 437 |
-
*nested-name-specifier* that names a namespace
|
| 438 |
-
optionally followed by the keyword `template`
|
| 439 |
-
|
| 440 |
-
|
| 441 |
*qualified-id*; [[namespace.qual]] describes name lookup for namespace
|
| 442 |
members that appear in *qualified-id*s. The result is the member. The
|
| 443 |
type of the result is the type of the member. The result is an lvalue if
|
| 444 |
-
the member is a function
|
|
|
|
| 445 |
|
| 446 |
-
A *nested-name-specifier* that denotes an enumeration
|
| 447 |
followed by the name of an enumerator of that enumeration, is a
|
| 448 |
*qualified-id* that refers to the enumerator. The result is the
|
| 449 |
enumerator. The type of the result is the type of the enumeration. The
|
| 450 |
result is a prvalue.
|
| 451 |
|
| 452 |
In a *qualified-id*, if the *unqualified-id* is a
|
| 453 |
-
*conversion-function-id*, its *conversion-type-id*
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
|
| 458 |
|
| 459 |
``` bnf
|
| 460 |
lambda-expression:
|
| 461 |
lambda-introducer lambda-declaratorₒₚₜ compound-statement
|
|
|
|
| 462 |
```
|
| 463 |
|
| 464 |
``` bnf
|
| 465 |
lambda-introducer:
|
| 466 |
'[' lambda-captureₒₚₜ ']'
|
| 467 |
```
|
| 468 |
|
| 469 |
``` bnf
|
| 470 |
lambda-declarator:
|
| 471 |
'(' parameter-declaration-clause ')' decl-specifier-seqₒₚₜ
|
| 472 |
-
noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 473 |
```
|
| 474 |
|
| 475 |
-
|
| 476 |
-
|
| 477 |
|
| 478 |
[*Example 1*:
|
| 479 |
|
| 480 |
``` cpp
|
| 481 |
#include <algorithm>
|
|
@@ -486,24 +1202,20 @@ void abssort(float* x, unsigned N) {
|
|
| 486 |
```
|
| 487 |
|
| 488 |
— *end example*]
|
| 489 |
|
| 490 |
A *lambda-expression* is a prvalue whose result object is called the
|
| 491 |
-
*closure object*.
|
| 492 |
-
unevaluated operand (Clause [[expr]]), in a *template-argument*, in an
|
| 493 |
-
*alias-declaration*, in a typedef declaration, or in the declaration of
|
| 494 |
-
a function or function template outside its function body and default
|
| 495 |
-
arguments.
|
| 496 |
|
| 497 |
-
[*Note 1*:
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
[*Note 2*: A closure object behaves like a function object (
|
| 501 |
-
[[function.objects]]). — *end note*]
|
| 502 |
|
| 503 |
In the *decl-specifier-seq* of the *lambda-declarator*, each
|
| 504 |
-
*decl-specifier* shall
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 507 |
as if the *lambda-declarator* were `()`. The lambda return type is
|
| 508 |
`auto`, which is replaced by the type specified by the
|
| 509 |
*trailing-return-type* if provided and/or deduced from `return`
|
|
@@ -518,54 +1230,63 @@ int j;
|
|
| 518 |
auto x3 = []()->auto&& { return j; }; // OK: return type is int&
|
| 519 |
```
|
| 520 |
|
| 521 |
— *end example*]
|
| 522 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
| 524 |
|
| 525 |
The type of a *lambda-expression* (which is also the type of the closure
|
| 526 |
object) is a unique, unnamed non-union class type, called the *closure
|
| 527 |
type*, whose properties are described below.
|
| 528 |
|
| 529 |
The closure type is declared in the smallest block scope, class scope,
|
| 530 |
or namespace scope that contains the corresponding *lambda-expression*.
|
| 531 |
|
| 532 |
[*Note 1*: This determines the set of namespaces and classes associated
|
| 533 |
-
with the closure type
|
| 534 |
-
|
| 535 |
classes. — *end note*]
|
| 536 |
|
| 537 |
-
The closure type is not an aggregate type
|
| 538 |
implementation may define the closure type differently from what is
|
| 539 |
described below provided this does not alter the observable behavior of
|
| 540 |
the program other than by changing:
|
| 541 |
|
| 542 |
- the size and/or alignment of the closure type,
|
| 543 |
-
- whether the closure type is trivially copyable
|
| 544 |
-
- whether the closure type is a standard-layout class
|
| 545 |
-
[[class]]), or
|
| 546 |
-
- whether the closure type is a POD class (Clause [[class]]).
|
| 547 |
|
| 548 |
An implementation shall not add members of rvalue reference type to the
|
| 549 |
closure type.
|
| 550 |
|
| 551 |
-
The closure type for a
|
| 552 |
-
|
|
|
|
| 553 |
return type are described by the *lambda-expression*’s
|
| 554 |
-
*parameter-declaration-clause* and *trailing-return-type* respectively
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
*parameter-declaration-clause* by replacing each occurrence of `auto` in
|
| 565 |
-
the *decl-specifier*s of the *parameter-declaration-clause* with the
|
| 566 |
-
name of the corresponding invented *template-parameter*.
|
| 567 |
|
| 568 |
[*Example 1*:
|
| 569 |
|
| 570 |
``` cpp
|
| 571 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
|
@@ -596,14 +1317,17 @@ specified on a *lambda-expression* applies to the corresponding function
|
|
| 596 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 597 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 598 |
call operator or operator template. The function call operator or any
|
| 599 |
given operator template specialization is a constexpr function if either
|
| 600 |
the corresponding *lambda-expression*'s *parameter-declaration-clause*
|
| 601 |
-
is followed by `constexpr`, or it satisfies the
|
| 602 |
-
constexpr function
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
-
[*Note
|
| 605 |
the context in which the *lambda-expression* appears. — *end note*]
|
| 606 |
|
| 607 |
[*Example 2*:
|
| 608 |
|
| 609 |
``` cpp
|
|
@@ -612,11 +1336,11 @@ static_assert(ID(3) == 3); // OK
|
|
| 612 |
|
| 613 |
struct NonLiteral {
|
| 614 |
NonLiteral(int n) : n(n) { }
|
| 615 |
int n;
|
| 616 |
};
|
| 617 |
-
static_assert(ID(NonLiteral{3}).n == 3);
|
| 618 |
```
|
| 619 |
|
| 620 |
— *end example*]
|
| 621 |
|
| 622 |
[*Example 3*:
|
|
@@ -640,35 +1364,65 @@ static_assert(add(one)(zero)() == one()); // OK
|
|
| 640 |
// Since two below is not declared constexpr, an evaluation of its constexpr member function call operator
|
| 641 |
// cannot perform an lvalue-to-rvalue conversion on one of its subobjects (that represents its capture)
|
| 642 |
// in a constant expression.
|
| 643 |
auto two = monoid(2);
|
| 644 |
assert(two() == 2); // OK, not a constant expression.
|
| 645 |
-
static_assert(add(one)(one)() == two());
|
| 646 |
static_assert(add(one)(one)() == monoid(2)()); // OK
|
| 647 |
```
|
| 648 |
|
| 649 |
— *end example*]
|
| 650 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
The closure type for a non-generic *lambda-expression* with no
|
| 652 |
-
*lambda-capture*
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
parameter types, as the function call operator template. The return type
|
| 665 |
-
of the pointer to function shall behave as if it were a
|
| 666 |
-
*decltype-specifier* denoting the return type of the corresponding
|
| 667 |
-
function call operator template specialization.
|
| 668 |
|
| 669 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
|
| 671 |
If the generic lambda has no *trailing-return-type* or the
|
| 672 |
*trailing-return-type* contains a placeholder type, return type
|
| 673 |
deduction of the corresponding function call operator template
|
| 674 |
specialization has to be done. The corresponding specialization is that
|
|
@@ -700,11 +1454,11 @@ struct Closure {
|
|
| 700 |
};
|
| 701 |
```
|
| 702 |
|
| 703 |
— *end note*]
|
| 704 |
|
| 705 |
-
[*Example
|
| 706 |
|
| 707 |
``` cpp
|
| 708 |
void f1(int (*)(int)) { }
|
| 709 |
void f2(char (*)(int)) { }
|
| 710 |
|
|
@@ -725,19 +1479,22 @@ int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
|
| 725 |
— *end example*]
|
| 726 |
|
| 727 |
The value returned by any given specialization of this conversion
|
| 728 |
function template is the address of a function `F` that, when invoked,
|
| 729 |
has the same effect as invoking the generic lambda’s corresponding
|
| 730 |
-
function call operator template specialization
|
| 731 |
-
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
-
[*Note
|
| 734 |
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 735 |
-
parameter types
|
| 736 |
-
pointer to function. — *end note*]
|
| 737 |
|
| 738 |
-
[*Example
|
| 739 |
|
| 740 |
``` cpp
|
| 741 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 742 |
int (*GL_int)(int) = GL; // OK: through conversion function template
|
| 743 |
GL_int(3); // OK: same as GL(3)
|
|
@@ -745,37 +1502,36 @@ GL_int(3); // OK: same as GL(3)
|
|
| 745 |
|
| 746 |
— *end example*]
|
| 747 |
|
| 748 |
The conversion function or conversion function template is public,
|
| 749 |
constexpr, non-virtual, non-explicit, const, and has a non-throwing
|
| 750 |
-
exception specification
|
| 751 |
|
| 752 |
-
[*Example
|
| 753 |
|
| 754 |
``` cpp
|
| 755 |
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 756 |
auto C = [](auto a) { return a; };
|
| 757 |
|
| 758 |
static_assert(Fwd(C,3) == 3); // OK
|
| 759 |
|
| 760 |
// No specialization of the function call operator template can be constexpr (due to the local static).
|
| 761 |
auto NC = [](auto a) { static int s; return a; };
|
| 762 |
-
static_assert(Fwd(NC,3) == 3);
|
| 763 |
```
|
| 764 |
|
| 765 |
— *end example*]
|
| 766 |
|
| 767 |
The *lambda-expression*’s *compound-statement* yields the
|
| 768 |
-
*function-body*
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
*lambda-expression*.
|
| 775 |
|
| 776 |
-
[*Example
|
| 777 |
|
| 778 |
``` cpp
|
| 779 |
struct S1 {
|
| 780 |
int x, y;
|
| 781 |
int operator()(int);
|
|
@@ -793,22 +1549,26 @@ struct S1 {
|
|
| 793 |
Further, a variable `__func__` is implicitly defined at the beginning of
|
| 794 |
the *compound-statement* of the *lambda-expression*, with semantics as
|
| 795 |
described in [[dcl.fct.def.general]].
|
| 796 |
|
| 797 |
The closure type associated with a *lambda-expression* has no default
|
| 798 |
-
constructor
|
| 799 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
|
| 801 |
-
[*Note
|
| 802 |
usual, and might therefore be defined as deleted. — *end note*]
|
| 803 |
|
| 804 |
The closure type associated with a *lambda-expression* has an
|
| 805 |
-
implicitly-declared destructor
|
| 806 |
|
| 807 |
-
A member of a closure type shall not be explicitly instantiated
|
| 808 |
-
[[temp.explicit]]
|
| 809 |
-
|
| 810 |
|
| 811 |
#### Captures <a id="expr.prim.lambda.capture">[[expr.prim.lambda.capture]]</a>
|
| 812 |
|
| 813 |
``` bnf
|
| 814 |
lambda-capture:
|
|
@@ -823,43 +1583,43 @@ capture-default:
|
|
| 823 |
'='
|
| 824 |
```
|
| 825 |
|
| 826 |
``` bnf
|
| 827 |
capture-list:
|
| 828 |
-
capture
|
| 829 |
-
capture-list ',' capture
|
| 830 |
```
|
| 831 |
|
| 832 |
``` bnf
|
| 833 |
capture:
|
| 834 |
simple-capture
|
| 835 |
init-capture
|
| 836 |
```
|
| 837 |
|
| 838 |
``` bnf
|
| 839 |
simple-capture:
|
| 840 |
-
identifier
|
| 841 |
-
'&' identifier
|
| 842 |
-
|
| 843 |
-
'* this'
|
| 844 |
```
|
| 845 |
|
| 846 |
``` bnf
|
| 847 |
init-capture:
|
| 848 |
-
identifier initializer
|
| 849 |
-
'&' identifier initializer
|
| 850 |
```
|
| 851 |
|
| 852 |
The body of a *lambda-expression* may refer to variables with automatic
|
| 853 |
storage duration and the `*this` object (if any) of enclosing block
|
| 854 |
scopes by capturing those entities, as described below.
|
| 855 |
|
| 856 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 857 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 858 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 859 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 860 |
-
form “`&` *identifier*” or “`* this`”.
|
| 861 |
|
| 862 |
[*Note 1*: The form `[&,this]` is redundant but accepted for
|
| 863 |
compatibility with ISO C++14. — *end note*]
|
| 864 |
|
| 865 |
Ignoring appearances in *initializer*s of *init-capture*s, an identifier
|
|
@@ -869,66 +1629,62 @@ or `this` shall not appear more than once in a *lambda-capture*.
|
|
| 869 |
|
| 870 |
``` cpp
|
| 871 |
struct S2 { void f(int i); };
|
| 872 |
void S2::f(int i) {
|
| 873 |
[&, i]{ }; // OK
|
|
|
|
| 874 |
[&, &i]{ }; // error: i preceded by & when & is the default
|
| 875 |
[=, *this]{ }; // OK
|
| 876 |
-
[=, this]{ }; //
|
| 877 |
[i, i]{ }; // error: i repeated
|
| 878 |
[this, *this]{ }; // error: this appears twice
|
| 879 |
}
|
| 880 |
```
|
| 881 |
|
| 882 |
— *end example*]
|
| 883 |
|
| 884 |
-
A *lambda-expression*
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
including the innermost enclosing function and its parameters.
|
| 890 |
-
|
| 891 |
-
[*Note 2*: This reaching scope includes any intervening
|
| 892 |
-
*lambda-expression*s. — *end note*]
|
| 893 |
|
| 894 |
The *identifier* in a *simple-capture* is looked up using the usual
|
| 895 |
-
rules for unqualified name lookup
|
| 896 |
-
lookup shall find
|
| 897 |
-
*
|
| 898 |
-
|
| 899 |
-
variable with automatic storage duration declared in the reaching scope
|
| 900 |
-
of the local lambda expression.
|
| 901 |
|
| 902 |
If an *identifier* in a *simple-capture* appears as the *declarator-id*
|
| 903 |
of a parameter of the *lambda-declarator*'s
|
| 904 |
*parameter-declaration-clause*, the program is ill-formed.
|
| 905 |
|
| 906 |
[*Example 2*:
|
| 907 |
|
| 908 |
``` cpp
|
| 909 |
void f() {
|
| 910 |
int x = 0;
|
| 911 |
-
auto g = [x](int x) { return 0; } // error: parameter and simple-capture have the same name
|
| 912 |
}
|
| 913 |
```
|
| 914 |
|
| 915 |
— *end example*]
|
| 916 |
|
| 917 |
-
An *init-capture* behaves as if it declares and
|
| 918 |
-
variable of the form “`auto` *init-capture* `;`”
|
| 919 |
-
region is the *lambda-expression*’s
|
|
|
|
| 920 |
|
| 921 |
- if the capture is by copy (see below), the non-static data member
|
| 922 |
declared for the capture and the variable are treated as two different
|
| 923 |
ways of referring to the same object, which has the lifetime of the
|
| 924 |
non-static data member, and no additional copy and destruction is
|
| 925 |
performed, and
|
| 926 |
- if the capture is by reference, the variable’s lifetime ends when the
|
| 927 |
closure object’s lifetime ends.
|
| 928 |
|
| 929 |
-
[*Note
|
| 930 |
the second “`x`” must bind to a declaration in the surrounding
|
| 931 |
context. — *end note*]
|
| 932 |
|
| 933 |
[*Example 3*:
|
| 934 |
|
|
@@ -937,71 +1693,100 @@ int x = 4;
|
|
| 937 |
auto y = [&r = x, x = x+1]()->int {
|
| 938 |
r += 2;
|
| 939 |
return x+2;
|
| 940 |
}(); // Updates ::x to 6, and initializes y to 7.
|
| 941 |
|
| 942 |
-
auto z = [a = 42](int a) { return 1; }
|
| 943 |
```
|
| 944 |
|
| 945 |
— *end example*]
|
| 946 |
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
(this excludes any *id-expression* that has been found to refer to an
|
| 950 |
-
*init-capture*'s associated non-static data member), is said to
|
| 951 |
-
*implicitly capture* the entity (i.e., `*this` or a variable) if the
|
| 952 |
-
*compound-statement*:
|
| 953 |
|
| 954 |
-
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 961 |
|
| 962 |
[*Example 4*:
|
| 963 |
|
| 964 |
``` cpp
|
| 965 |
-
void f(int, const int (&)[2] = {})
|
| 966 |
-
void f(const int&, const int (&)[1])
|
| 967 |
void test() {
|
| 968 |
const int x = 17;
|
| 969 |
auto g = [](auto a) {
|
| 970 |
f(x); // OK: calls #1, does not capture x
|
| 971 |
};
|
| 972 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 973 |
auto g2 = [=](auto a) {
|
| 974 |
int selector[sizeof(a) == 1 ? 1 : 2]{};
|
| 975 |
-
f(x, selector); // OK:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 976 |
};
|
| 977 |
}
|
| 978 |
```
|
| 979 |
|
|
|
|
|
|
|
|
|
|
| 980 |
— *end example*]
|
| 981 |
|
| 982 |
-
|
| 983 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
|
| 985 |
-
|
| 986 |
-
*lambda-expression* can cause its implicit capture by the containing
|
| 987 |
-
*lambda-expression* (see below). Implicit odr-uses of `this` can result
|
| 988 |
-
in implicit capture. — *end note*]
|
| 989 |
|
| 990 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 991 |
-
entity captured by a *lambda-expression* is odr-used
|
| 992 |
-
|
| 993 |
-
`*this` is captured by a local lambda expression, its nearest enclosing
|
| 994 |
-
function shall be a non-static member function. If a *lambda-expression*
|
| 995 |
-
or an instantiation of the function call operator template of a generic
|
| 996 |
-
lambda odr-uses ([[basic.def.odr]]) `this` or a variable with automatic
|
| 997 |
-
storage duration from its reaching scope, that entity shall be captured
|
| 998 |
-
by the *lambda-expression*. If a *lambda-expression* captures an entity
|
| 999 |
-
and that entity is not defined or captured in the immediately enclosing
|
| 1000 |
-
lambda expression or function, the program is ill-formed.
|
| 1001 |
|
| 1002 |
-
[*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1003 |
|
| 1004 |
``` cpp
|
| 1005 |
void f1(int i) {
|
| 1006 |
int const N = 20;
|
| 1007 |
auto m1 = [=]{
|
|
@@ -1015,14 +1800,15 @@ void f1(int i) {
|
|
| 1015 |
int f;
|
| 1016 |
void work(int n) {
|
| 1017 |
int m = n*n;
|
| 1018 |
int j = 40;
|
| 1019 |
auto m3 = [this,m] {
|
| 1020 |
-
auto m4 = [&,j] { // error: j not
|
| 1021 |
-
int x = n; // error: n
|
| 1022 |
x += m; // OK: m implicitly captured by m4 and explicitly captured by m3
|
| 1023 |
-
x += i; // error: i is
|
|
|
|
| 1024 |
x += f; // OK: this captured implicitly by m4 and explicitly by m3
|
| 1025 |
};
|
| 1026 |
};
|
| 1027 |
}
|
| 1028 |
};
|
|
@@ -1032,11 +1818,11 @@ struct s2 {
|
|
| 1032 |
double ohseven = .007;
|
| 1033 |
auto f() {
|
| 1034 |
return [this] {
|
| 1035 |
return [*this] {
|
| 1036 |
return ohseven; // OK
|
| 1037 |
-
}
|
| 1038 |
}();
|
| 1039 |
}
|
| 1040 |
auto g() {
|
| 1041 |
return [] {
|
| 1042 |
return [*this] { }; // error: *this not captured by outer lambda-expression
|
|
@@ -1045,23 +1831,30 @@ struct s2 {
|
|
| 1045 |
};
|
| 1046 |
```
|
| 1047 |
|
| 1048 |
— *end example*]
|
| 1049 |
|
| 1050 |
-
|
| 1051 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1052 |
|
| 1053 |
-
[*Example
|
| 1054 |
|
| 1055 |
``` cpp
|
| 1056 |
void f2() {
|
| 1057 |
int i = 1;
|
| 1058 |
-
void g1(int = ([i]{ return i; })()); //
|
| 1059 |
-
void g2(int = ([i]{ return 0; })()); //
|
| 1060 |
-
void g3(int = ([=]{ return i; })()); //
|
| 1061 |
void g4(int = ([=]{ return 0; })()); // OK
|
| 1062 |
void g5(int = ([]{ return sizeof i; })()); // OK
|
|
|
|
|
|
|
| 1063 |
}
|
| 1064 |
```
|
| 1065 |
|
| 1066 |
— *end example*]
|
| 1067 |
|
|
@@ -1079,36 +1872,24 @@ the entity is a reference to an object, an lvalue reference to the
|
|
| 1079 |
referenced function type if the entity is a reference to a function, or
|
| 1080 |
the type of the corresponding captured entity otherwise. A member of an
|
| 1081 |
anonymous union shall not be captured by copy.
|
| 1082 |
|
| 1083 |
Every *id-expression* within the *compound-statement* of a
|
| 1084 |
-
*lambda-expression* that is an odr-use
|
| 1085 |
captured by copy is transformed into an access to the corresponding
|
| 1086 |
unnamed data member of the closure type.
|
| 1087 |
|
| 1088 |
-
[*Note
|
| 1089 |
-
original entity, never to a member of the closure type.
|
| 1090 |
-
|
| 1091 |
entity. — *end note*]
|
| 1092 |
|
| 1093 |
-
If `*this` is captured by copy, each odr-
|
| 1094 |
-
|
| 1095 |
-
|
| 1096 |
|
| 1097 |
-
[*
|
| 1098 |
-
prvalue. — *end note*]
|
| 1099 |
-
|
| 1100 |
-
An *id-expression* within the *compound-statement* of a
|
| 1101 |
-
*lambda-expression* that is an odr-use of a reference captured by
|
| 1102 |
-
reference refers to the entity to which the captured reference is bound
|
| 1103 |
-
and not to the captured reference.
|
| 1104 |
-
|
| 1105 |
-
[*Note 7*: The validity of such captures is determined by the lifetime
|
| 1106 |
-
of the object to which the reference refers, not by the lifetime of the
|
| 1107 |
-
reference itself. — *end note*]
|
| 1108 |
-
|
| 1109 |
-
[*Example 7*:
|
| 1110 |
|
| 1111 |
``` cpp
|
| 1112 |
void f(const int*);
|
| 1113 |
void g() {
|
| 1114 |
const int N = 10;
|
|
@@ -1116,27 +1897,21 @@ void g() {
|
|
| 1116 |
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 1117 |
f(&N); // OK: causes N to be captured; &N points to
|
| 1118 |
// the corresponding member of the closure type
|
| 1119 |
};
|
| 1120 |
}
|
| 1121 |
-
auto h(int &r) {
|
| 1122 |
-
return [&] {
|
| 1123 |
-
++r; // Valid after h returns if the lifetime of the
|
| 1124 |
-
// object to which r is bound has not ended
|
| 1125 |
-
};
|
| 1126 |
-
}
|
| 1127 |
```
|
| 1128 |
|
| 1129 |
— *end example*]
|
| 1130 |
|
| 1131 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 1132 |
captured but not captured by copy. It is unspecified whether additional
|
| 1133 |
unnamed non-static data members are declared in the closure type for
|
| 1134 |
entities captured by reference. If declared, such non-static data
|
| 1135 |
members shall be of literal type.
|
| 1136 |
|
| 1137 |
-
[*Example
|
| 1138 |
|
| 1139 |
``` cpp
|
| 1140 |
// The inner closure type must be a literal type regardless of how reference captures are represented.
|
| 1141 |
static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
|
| 1142 |
```
|
|
@@ -1144,22 +1919,44 @@ static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
|
|
| 1144 |
— *end example*]
|
| 1145 |
|
| 1146 |
A bit-field or a member of an anonymous union shall not be captured by
|
| 1147 |
reference.
|
| 1148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1149 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 1150 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 1151 |
`m2`’s capture is transformed as follows:
|
| 1152 |
|
| 1153 |
- if `m1` captures the entity by copy, `m2` captures the corresponding
|
| 1154 |
non-static data member of `m1`’s closure type;
|
| 1155 |
- if `m1` captures the entity by reference, `m2` captures the same
|
| 1156 |
entity captured by `m1`.
|
| 1157 |
|
| 1158 |
-
[*Example
|
| 1159 |
|
| 1160 |
-
The nested lambda
|
| 1161 |
`123234`.
|
| 1162 |
|
| 1163 |
``` cpp
|
| 1164 |
int a = 1, b = 1, c = 1;
|
| 1165 |
auto m1 = [a, &b, &c]() mutable {
|
|
@@ -1175,70 +1972,52 @@ m1();
|
|
| 1175 |
std::cout << a << b << c;
|
| 1176 |
```
|
| 1177 |
|
| 1178 |
— *end example*]
|
| 1179 |
|
| 1180 |
-
Every occurrence of `decltype((x))` where `x` is a possibly
|
| 1181 |
-
parenthesized *id-expression* that names an entity of automatic storage
|
| 1182 |
-
duration is treated as if `x` were transformed into an access to a
|
| 1183 |
-
corresponding data member of the closure type that would have been
|
| 1184 |
-
declared if `x` were an odr-use of the denoted entity.
|
| 1185 |
-
|
| 1186 |
-
[*Example 10*:
|
| 1187 |
-
|
| 1188 |
-
``` cpp
|
| 1189 |
-
void f3() {
|
| 1190 |
-
float x, &r = x;
|
| 1191 |
-
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
|
| 1192 |
-
decltype(x) y1; // y1 has type float
|
| 1193 |
-
decltype((x)) y2 = y1; // y2 has type float const& because this lambda is not mutable and x is an lvalue
|
| 1194 |
-
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
|
| 1195 |
-
decltype((r)) r2 = y2; // r2 has type float const&
|
| 1196 |
-
};
|
| 1197 |
-
}
|
| 1198 |
-
```
|
| 1199 |
-
|
| 1200 |
-
— *end example*]
|
| 1201 |
-
|
| 1202 |
When the *lambda-expression* is evaluated, the entities that are
|
| 1203 |
captured by copy are used to direct-initialize each corresponding
|
| 1204 |
non-static data member of the resulting closure object, and the
|
| 1205 |
non-static data members corresponding to the *init-capture*s are
|
| 1206 |
initialized as indicated by the corresponding *initializer* (which may
|
| 1207 |
be copy- or direct-initialization). (For array members, the array
|
| 1208 |
elements are direct-initialized in increasing subscript order.) These
|
| 1209 |
initializations are performed in the (unspecified) order in which the
|
| 1210 |
non-static data members are declared.
|
| 1211 |
|
| 1212 |
-
[*Note
|
| 1213 |
order of the constructions. — *end note*]
|
| 1214 |
|
| 1215 |
-
[*Note
|
| 1216 |
captured by reference, invoking the function call operator of the
|
| 1217 |
corresponding *lambda-expression* after the lifetime of the entity has
|
| 1218 |
ended is likely to result in undefined behavior. — *end note*]
|
| 1219 |
|
| 1220 |
-
A *simple-capture*
|
| 1221 |
-
[[temp.variadic]]
|
| 1222 |
-
|
|
|
|
| 1223 |
|
| 1224 |
-
[*Example
|
| 1225 |
|
| 1226 |
``` cpp
|
| 1227 |
template<class... Args>
|
| 1228 |
void f(Args... args) {
|
| 1229 |
auto lm = [&, args...] { return g(args...); };
|
| 1230 |
lm();
|
|
|
|
|
|
|
|
|
|
| 1231 |
}
|
| 1232 |
```
|
| 1233 |
|
| 1234 |
— *end example*]
|
| 1235 |
|
| 1236 |
### Fold expressions <a id="expr.prim.fold">[[expr.prim.fold]]</a>
|
| 1237 |
|
| 1238 |
-
A fold expression performs a fold of a
|
| 1239 |
-
|
| 1240 |
|
| 1241 |
``` bnf
|
| 1242 |
fold-expression:
|
| 1243 |
'(' cast-expression fold-operator '...' ')'
|
| 1244 |
'(' '...' fold-operator cast-expression ')'
|
|
@@ -1257,20 +2036,19 @@ fold-operator: one of
|
|
| 1257 |
An expression of the form `(...` *op* `e)` where *op* is a
|
| 1258 |
*fold-operator* is called a *unary left fold*. An expression of the form
|
| 1259 |
`(e` *op* `...)` where *op* is a *fold-operator* is called a *unary
|
| 1260 |
right fold*. Unary left folds and unary right folds are collectively
|
| 1261 |
called *unary folds*. In a unary fold, the *cast-expression* shall
|
| 1262 |
-
contain an unexpanded
|
| 1263 |
|
| 1264 |
An expression of the form `(e1` *op1* `...` *op2* `e2)` where *op1* and
|
| 1265 |
*op2* are *fold-operator*s is called a *binary fold*. In a binary fold,
|
| 1266 |
*op1* and *op2* shall be the same *fold-operator*, and either `e1` shall
|
| 1267 |
-
contain an unexpanded
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
fold*.
|
| 1272 |
|
| 1273 |
[*Example 1*:
|
| 1274 |
|
| 1275 |
``` cpp
|
| 1276 |
template<typename ...Args>
|
|
@@ -1278,17 +2056,323 @@ bool f(Args ...args) {
|
|
| 1278 |
return (true && ... && args); // OK
|
| 1279 |
}
|
| 1280 |
|
| 1281 |
template<typename ...Args>
|
| 1282 |
bool f(Args ...args) {
|
| 1283 |
-
return (args + ... + args); // error: both operands contain unexpanded
|
| 1284 |
}
|
| 1285 |
```
|
| 1286 |
|
| 1287 |
— *end example*]
|
| 1288 |
|
| 1289 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1290 |
|
| 1291 |
Postfix expressions group left-to-right.
|
| 1292 |
|
| 1293 |
``` bnf
|
| 1294 |
postfix-expression:
|
|
@@ -1297,156 +2381,172 @@ postfix-expression:
|
|
| 1297 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 1298 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 1299 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 1300 |
simple-type-specifier braced-init-list
|
| 1301 |
typename-specifier braced-init-list
|
| 1302 |
-
postfix-expression '. template'ₒₚₜ id-expression
|
| 1303 |
-
postfix-expression '-> template'ₒₚₜ id-expression
|
| 1304 |
-
postfix-expression '.' pseudo-destructor-name
|
| 1305 |
-
postfix-expression '->' pseudo-destructor-name
|
| 1306 |
postfix-expression '++'
|
| 1307 |
postfix-expression '-{-}'
|
| 1308 |
-
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
|
| 1312 |
-
|
| 1313 |
-
|
| 1314 |
```
|
| 1315 |
|
| 1316 |
``` bnf
|
| 1317 |
expression-list:
|
| 1318 |
initializer-list
|
| 1319 |
```
|
| 1320 |
|
| 1321 |
-
``` bnf
|
| 1322 |
-
pseudo-destructor-name:
|
| 1323 |
-
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 1324 |
-
nested-name-specifier 'template' simple-template-id ':: ~' type-name
|
| 1325 |
-
'~' type-name
|
| 1326 |
-
'~' decltype-specifier
|
| 1327 |
-
```
|
| 1328 |
-
|
| 1329 |
[*Note 1*: The `>` token following the *type-id* in a `dynamic_cast`,
|
| 1330 |
`static_cast`, `reinterpret_cast`, or `const_cast` may be the product of
|
| 1331 |
-
replacing a `>{>}` token by two consecutive `>` tokens
|
| 1332 |
-
[[temp.names]]
|
| 1333 |
|
| 1334 |
-
### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 1335 |
|
| 1336 |
A postfix expression followed by an expression in square brackets is a
|
| 1337 |
postfix expression. One of the expressions shall be a glvalue of type
|
| 1338 |
“array of `T`” or a prvalue of type “pointer to `T`” and the other shall
|
| 1339 |
be a prvalue of unscoped enumeration or integral type. The result is of
|
| 1340 |
type “`T`”. The type “`T`” shall be a completely-defined object
|
| 1341 |
-
type.[^
|
| 1342 |
-
`*((E1)+(E2))`
|
|
|
|
|
|
|
| 1343 |
|
| 1344 |
-
[*Note 1*:
|
| 1345 |
-
|
|
|
|
| 1346 |
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
|
|
|
| 1350 |
|
| 1351 |
A *braced-init-list* shall not be used with the built-in subscript
|
| 1352 |
operator.
|
| 1353 |
|
| 1354 |
-
### Function call <a id="expr.call">[[expr.call]]</a>
|
| 1355 |
|
| 1356 |
A function call is a postfix expression followed by parentheses
|
| 1357 |
containing a possibly empty, comma-separated list of
|
| 1358 |
*initializer-clause*s which constitute the arguments to the function.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1359 |
The postfix expression shall have function type or function pointer
|
| 1360 |
type. For a call to a non-member function or to a static member
|
| 1361 |
-
function, the postfix expression shall
|
| 1362 |
-
to a function (in which case the function-to-pointer standard
|
| 1363 |
-
|
| 1364 |
-
|
| 1365 |
-
expression whose function type is different from the function type of
|
| 1366 |
-
the called function’s definition results in undefined behavior (
|
| 1367 |
-
[[dcl.link]]). For a call to a non-static member function, the postfix
|
| 1368 |
-
expression shall be an implicit ([[class.mfct.non-static]],
|
| 1369 |
-
[[class.static]]) or explicit class member access ([[expr.ref]]) whose
|
| 1370 |
-
*id-expression* is a function member name, or a pointer-to-member
|
| 1371 |
-
expression ([[expr.mptr.oper]]) selecting a function member; the call
|
| 1372 |
-
is as a member of the class object referred to by the object expression.
|
| 1373 |
-
In the case of an implicit class member access, the implied object is
|
| 1374 |
-
the one pointed to by `this`.
|
| 1375 |
|
| 1376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1377 |
`(*this).f()` (see [[class.mfct.non-static]]). — *end note*]
|
| 1378 |
|
| 1379 |
-
If
|
| 1380 |
-
|
| 1381 |
-
|
| 1382 |
-
|
| 1383 |
-
member access expression is a *qualified-id*, that function is called.
|
| 1384 |
-
Otherwise, its final overrider ([[class.virtual]]) in the dynamic type
|
| 1385 |
-
of the object expression is called; such a call is referred to as a
|
| 1386 |
*virtual function call*.
|
| 1387 |
|
| 1388 |
-
[*Note
|
| 1389 |
the current value of the object expression. [[class.cdtor]] describes
|
| 1390 |
the behavior of virtual function calls when the object expression refers
|
| 1391 |
to an object under construction or destruction. — *end note*]
|
| 1392 |
|
| 1393 |
-
[*Note
|
| 1394 |
-
lookup
|
| 1395 |
program is ill-formed. No function is implicitly declared by such a
|
| 1396 |
call. — *end note*]
|
| 1397 |
|
| 1398 |
-
If the *postfix-expression*
|
| 1399 |
-
the type of the function call expression is
|
| 1400 |
-
of the function call expression is the
|
| 1401 |
-
chosen function (i.e., ignoring the
|
| 1402 |
-
|
| 1403 |
-
an object type, a reference type or
|
| 1404 |
-
|
| 1405 |
-
|
| 1406 |
-
|
| 1407 |
-
|
| 1408 |
-
|
| 1409 |
-
|
| 1410 |
-
an
|
| 1411 |
-
|
| 1412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1413 |
the access checking and disambiguation are done as part of the (possibly
|
| 1414 |
-
implicit) class member access operator. See [[class.member.lookup]],
|
| 1415 |
[[class.access.base]], and [[expr.ref]]. — *end note*]
|
| 1416 |
|
| 1417 |
-
When a function is called, the
|
| 1418 |
-
|
| 1419 |
|
| 1420 |
-
[*Note
|
| 1421 |
-
to
|
| 1422 |
-
|
| 1423 |
|
| 1424 |
It is *implementation-defined* whether the lifetime of a parameter ends
|
| 1425 |
when the function in which it is defined returns or at the end of the
|
| 1426 |
enclosing full-expression. The initialization and destruction of each
|
| 1427 |
parameter occurs within the context of the calling function.
|
| 1428 |
|
| 1429 |
-
[*Example
|
| 1430 |
destructor is checked at the point of call in the calling function. If a
|
| 1431 |
constructor or destructor for a function parameter throws an exception,
|
| 1432 |
the search for a handler starts in the scope of the calling function; in
|
| 1433 |
-
particular, if the function called has a *function-try-block*
|
| 1434 |
-
[[except]]
|
| 1435 |
-
is not considered. — *end example*]
|
| 1436 |
|
| 1437 |
The *postfix-expression* is sequenced before each *expression* in the
|
| 1438 |
*expression-list* and any default argument. The initialization of a
|
| 1439 |
parameter, including every associated value computation and side effect,
|
| 1440 |
is indeterminately sequenced with respect to that of any other
|
| 1441 |
parameter.
|
| 1442 |
|
| 1443 |
-
[*Note
|
| 1444 |
before the function is entered (see
|
| 1445 |
[[intro.execution]]). — *end note*]
|
| 1446 |
|
| 1447 |
-
[*Example
|
| 1448 |
|
| 1449 |
``` cpp
|
| 1450 |
void f() {
|
| 1451 |
std::string s = "but I have heard it works even if you don't believe in it";
|
| 1452 |
s.replace(0, 4, "").replace(s.find("even"), 4, "only").replace(s.find(" don't"), 6, "");
|
|
@@ -1454,15 +2554,15 @@ void f() {
|
|
| 1454 |
}
|
| 1455 |
```
|
| 1456 |
|
| 1457 |
— *end example*]
|
| 1458 |
|
| 1459 |
-
[*Note
|
| 1460 |
argument evaluation is sequenced as specified for the built-in operator;
|
| 1461 |
see [[over.match.oper]]. — *end note*]
|
| 1462 |
|
| 1463 |
-
[*Example
|
| 1464 |
|
| 1465 |
``` cpp
|
| 1466 |
struct S {
|
| 1467 |
S(int);
|
| 1468 |
};
|
|
@@ -1476,213 +2576,202 @@ After performing the initializations, the value of `i` is 2 (see
|
|
| 1476 |
[[expr.shift]]), but it is unspecified whether the value of `j` is 1 or
|
| 1477 |
2.
|
| 1478 |
|
| 1479 |
— *end example*]
|
| 1480 |
|
| 1481 |
-
The result of a function call is the result of the
|
| 1482 |
-
|
| 1483 |
-
(if any), except in a virtual
|
| 1484 |
-
|
| 1485 |
-
|
| 1486 |
-
converted to the return type of the
|
|
|
|
| 1487 |
|
| 1488 |
-
[*Note
|
| 1489 |
parameters, but these changes cannot affect the values of the arguments
|
| 1490 |
-
except where a parameter is of a reference type
|
| 1491 |
reference is to a const-qualified type, `const_cast` is required to be
|
| 1492 |
used to cast away the constness in order to modify the argument’s value.
|
| 1493 |
Where a parameter is of `const` reference type a temporary object is
|
| 1494 |
-
introduced if needed ([[dcl.type]],
|
| 1495 |
-
[[dcl.array]],
|
| 1496 |
modify the values of non-constant objects through pointer
|
| 1497 |
parameters. — *end note*]
|
| 1498 |
|
| 1499 |
A function can be declared to accept fewer arguments (by declaring
|
| 1500 |
-
default arguments
|
| 1501 |
-
|
| 1502 |
-
|
| 1503 |
|
| 1504 |
-
[*Note
|
| 1505 |
function parameter pack is used, a parameter is available for each
|
| 1506 |
argument. — *end note*]
|
| 1507 |
|
| 1508 |
When there is no parameter for a given argument, the argument is passed
|
| 1509 |
in such a way that the receiving function can obtain the value of the
|
| 1510 |
-
argument by invoking `va_arg`
|
| 1511 |
|
| 1512 |
-
[*Note
|
| 1513 |
function parameter pack. Function parameter packs are expanded during
|
| 1514 |
-
template instantiation
|
| 1515 |
-
|
| 1516 |
actually called. — *end note*]
|
| 1517 |
|
| 1518 |
-
The lvalue-to-rvalue
|
| 1519 |
-
|
| 1520 |
-
|
| 1521 |
-
|
| 1522 |
-
|
| 1523 |
-
|
| 1524 |
-
|
| 1525 |
-
type
|
| 1526 |
-
non-trivial move constructor, or a non-trivial destructor
|
| 1527 |
-
corresponding parameter, is conditionally-supported
|
| 1528 |
-
*implementation-defined* semantics. If the argument has integral or
|
| 1529 |
-
enumeration type that is subject to the integral promotions
|
| 1530 |
-
[[conv.prom]]
|
| 1531 |
-
floating-point promotion
|
| 1532 |
-
|
| 1533 |
referred to as the *default argument promotions*.
|
| 1534 |
|
| 1535 |
-
Recursive calls are permitted, except to the `main` function
|
| 1536 |
-
[[basic.start.main]]
|
| 1537 |
|
| 1538 |
A function call is an lvalue if the result type is an lvalue reference
|
| 1539 |
type or an rvalue reference to function type, an xvalue if the result
|
| 1540 |
type is an rvalue reference to object type, and a prvalue otherwise.
|
| 1541 |
|
| 1542 |
-
### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 1543 |
|
| 1544 |
-
A *simple-type-specifier*
|
| 1545 |
-
|
| 1546 |
-
|
| 1547 |
-
|
| 1548 |
-
|
| 1549 |
-
|
| 1550 |
-
|
| 1551 |
-
this section.
|
| 1552 |
|
| 1553 |
If the initializer is a parenthesized single expression, the type
|
| 1554 |
-
conversion expression is equivalent
|
| 1555 |
-
|
| 1556 |
-
|
| 1557 |
prvalue of the specified type that performs no initialization.
|
| 1558 |
Otherwise, the expression is a prvalue of the specified type whose
|
| 1559 |
-
result object is direct-initialized
|
| 1560 |
-
|
| 1561 |
-
array type.
|
| 1562 |
|
| 1563 |
-
###
|
| 1564 |
-
|
| 1565 |
-
The use of a *pseudo-destructor-name* after a dot `.` or arrow `->`
|
| 1566 |
-
operator represents the destructor for the non-class type denoted by
|
| 1567 |
-
*type-name* or *decltype-specifier*. The result shall only be used as
|
| 1568 |
-
the operand for the function call operator `()`, and the result of such
|
| 1569 |
-
a call has type `void`. The only effect is the evaluation of the
|
| 1570 |
-
*postfix-expression* before the dot or arrow.
|
| 1571 |
-
|
| 1572 |
-
The left-hand side of the dot operator shall be of scalar type. The
|
| 1573 |
-
left-hand side of the arrow operator shall be of pointer to scalar type.
|
| 1574 |
-
This scalar type is the object type. The *cv*-unqualified versions of
|
| 1575 |
-
the object type and of the type designated by the
|
| 1576 |
-
*pseudo-destructor-name* shall be the same type. Furthermore, the two
|
| 1577 |
-
*type-name*s in a *pseudo-destructor-name* of the form
|
| 1578 |
-
|
| 1579 |
-
``` bnf
|
| 1580 |
-
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 1581 |
-
```
|
| 1582 |
-
|
| 1583 |
-
shall designate the same scalar type (ignoring cv-qualification).
|
| 1584 |
-
|
| 1585 |
-
### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 1586 |
|
| 1587 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 1588 |
-
followed by the keyword `template`
|
| 1589 |
-
|
| 1590 |
-
before the dot or arrow is evaluated;[^
|
| 1591 |
-
together with the *id-expression*, determines the result of
|
| 1592 |
-
postfix expression.
|
| 1593 |
-
|
| 1594 |
-
For the first option (dot) the first expression shall be a glvalue
|
| 1595 |
-
|
| 1596 |
-
|
| 1597 |
-
|
| 1598 |
-
|
| 1599 |
-
|
| 1600 |
-
|
| 1601 |
-
|
| 1602 |
-
|
| 1603 |
-
|
| 1604 |
-
|
| 1605 |
-
|
| 1606 |
-
[*Note
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1607 |
after the `.` and `->` operators. — *end note*]
|
| 1608 |
|
| 1609 |
-
|
| 1610 |
-
|
| 1611 |
-
|
| 1612 |
-
|
| 1613 |
-
|
| 1614 |
-
|
| 1615 |
-
cv-qualifiers, as defined in [[basic.type.qualifier]].
|
| 1616 |
|
| 1617 |
If `E2` is declared to have type “reference to `T`”, then `E1.E2` is an
|
| 1618 |
lvalue; the type of `E1.E2` is `T`. Otherwise, one of the following
|
| 1619 |
rules applies.
|
| 1620 |
|
| 1621 |
- If `E2` is a static data member and the type of `E2` is `T`, then
|
| 1622 |
`E1.E2` is an lvalue; the expression designates the named member of
|
| 1623 |
the class. The type of `E1.E2` is `T`.
|
| 1624 |
- If `E2` is a non-static data member and the type of `E1` is “*cq1 vq1*
|
| 1625 |
`X`”, and the type of `E2` is “*cq2 vq2* `T`”, the expression
|
| 1626 |
-
designates the
|
| 1627 |
-
expression. If `E1` is an lvalue, then `E1.E2` is an
|
| 1628 |
-
`E1.E2` is an xvalue. Let the notation *vq12* stand
|
| 1629 |
-
*vq1* and *vq2*; that is, if *vq1* or *vq2* is
|
| 1630 |
-
is `volatile`. Similarly, let the notation
|
| 1631 |
-
“union” of *cq1* and *cq2*; that is, if *cq1* or
|
| 1632 |
-
then *cq12* is `const`. If `E2` is declared to be a
|
| 1633 |
-
then the type of `E1.E2` is “*vq12* `T`”. If `E2` is
|
| 1634 |
-
be a `mutable` member, then the type of `E1.E2` is
|
| 1635 |
-
`T`”.
|
| 1636 |
- If `E2` is a (possibly overloaded) member function, function overload
|
| 1637 |
-
resolution
|
| 1638 |
-
refers
|
| 1639 |
-
|
| 1640 |
-
|
| 1641 |
-
|
| 1642 |
-
|
| 1643 |
-
of
|
| 1644 |
-
|
| 1645 |
-
|
| 1646 |
-
*ref-qualifier*ₒₚₜ returning `T`”, then `E1.E2` is a prvalue. The
|
| 1647 |
-
expression designates a non-static member function. The expression
|
| 1648 |
-
can be used only as the left-hand operand of a member function
|
| 1649 |
-
call ([[class.mfct]]). \[*Note 3*: Any redundant set of parentheses
|
| 1650 |
-
surrounding the expression is ignored (
|
| 1651 |
-
[[expr.prim]]). — *end note*] The type of `E1.E2` is “function of
|
| 1652 |
-
parameter-type-list *cv* returning `T`”.
|
| 1653 |
- If `E2` is a nested type, the expression `E1.E2` is ill-formed.
|
| 1654 |
- If `E2` is a member enumerator and the type of `E2` is `T`, the
|
| 1655 |
expression `E1.E2` is a prvalue. The type of `E1.E2` is `T`.
|
| 1656 |
|
| 1657 |
If `E2` is a non-static data member or a non-static member function, the
|
| 1658 |
program is ill-formed if the class of which `E2` is directly a member is
|
| 1659 |
-
an ambiguous base
|
| 1660 |
-
[[class.access.base]]
|
| 1661 |
|
| 1662 |
-
[*Note
|
| 1663 |
ambiguous base of the class type of the object expression; see
|
| 1664 |
[[class.access.base]]. — *end note*]
|
| 1665 |
|
| 1666 |
-
### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 1667 |
|
| 1668 |
The value of a postfix `++` expression is the value of its operand.
|
| 1669 |
|
| 1670 |
[*Note 1*: The value obtained is a copy of the original
|
| 1671 |
-
value — *end note*]
|
| 1672 |
|
| 1673 |
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 1674 |
be an arithmetic type other than cv `bool`, or a pointer to a complete
|
| 1675 |
-
object type.
|
| 1676 |
-
|
| 1677 |
-
|
| 1678 |
-
|
| 1679 |
-
|
|
|
|
| 1680 |
|
| 1681 |
-
[*Note 2*: Therefore, a function call
|
| 1682 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 1683 |
-
single postfix ++ operator. — *end note*]
|
| 1684 |
|
| 1685 |
The result is a prvalue. The type of the result is the cv-unqualified
|
| 1686 |
version of the type of the operand. If the operand is a bit-field that
|
| 1687 |
cannot represent the incremented value, the resulting value of the
|
| 1688 |
bit-field is *implementation-defined*. See also [[expr.add]] and
|
|
@@ -1692,40 +2781,37 @@ The operand of postfix `\dcr` is decremented analogously to the postfix
|
|
| 1692 |
`++` operator.
|
| 1693 |
|
| 1694 |
[*Note 3*: For prefix increment and decrement, see
|
| 1695 |
[[expr.pre.incr]]. — *end note*]
|
| 1696 |
|
| 1697 |
-
### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 1698 |
|
| 1699 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 1700 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 1701 |
-
reference to a complete class type, or “pointer to
|
| 1702 |
-
`dynamic_cast` operator shall not cast away constness
|
| 1703 |
-
[[expr.const.cast]]
|
| 1704 |
|
| 1705 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 1706 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 1707 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 1708 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 1709 |
is an rvalue reference type, `v` shall be a glvalue having a complete
|
| 1710 |
class type, and the result is an xvalue of the type referred to by `T`.
|
| 1711 |
|
| 1712 |
-
If the type of `v` is the same as `T`
|
| 1713 |
-
|
| 1714 |
-
object type in `v`, the result is `v` (converted if necessary).
|
| 1715 |
-
|
| 1716 |
-
If the value of `v` is a null pointer value in the pointer case, the
|
| 1717 |
-
result is the null pointer value of type `T`.
|
| 1718 |
|
| 1719 |
If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
| 1720 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 1721 |
-
unique `B` subobject of the `D` object pointed to by `v`
|
| 1722 |
-
|
| 1723 |
-
|
| 1724 |
-
`D`
|
| 1725 |
-
|
| 1726 |
-
|
|
|
|
| 1727 |
|
| 1728 |
[*Example 1*:
|
| 1729 |
|
| 1730 |
``` cpp
|
| 1731 |
struct B { };
|
|
@@ -1735,37 +2821,37 @@ void foo(D* dp) {
|
|
| 1735 |
}
|
| 1736 |
```
|
| 1737 |
|
| 1738 |
— *end example*]
|
| 1739 |
|
| 1740 |
-
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
|
| 1741 |
-
|
| 1742 |
|
| 1743 |
-
If `
|
|
|
|
|
|
|
| 1744 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 1745 |
applied to see if the object pointed or referred to by `v` can be
|
| 1746 |
converted to the type pointed or referred to by `T`.
|
| 1747 |
|
| 1748 |
If `C` is the class type to which `T` points or refers, the runtime
|
| 1749 |
check logically executes as follows:
|
| 1750 |
|
| 1751 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 1752 |
-
points (refers) to a
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
| 1756 |
-
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
result points (refers) to the `C` subobject of the most derived
|
| 1760 |
-
object.
|
| 1761 |
- Otherwise, the runtime check *fails*.
|
| 1762 |
|
| 1763 |
The value of a failed cast to pointer type is the null pointer value of
|
| 1764 |
the required result type. A failed cast to reference type throws an
|
| 1765 |
-
exception
|
| 1766 |
-
[[except.handle]]
|
| 1767 |
|
| 1768 |
[*Example 2*:
|
| 1769 |
|
| 1770 |
``` cpp
|
| 1771 |
class A { virtual void f(); };
|
|
@@ -1786,58 +2872,61 @@ class E : public D, public B { };
|
|
| 1786 |
class F : public E, public D { };
|
| 1787 |
void h() {
|
| 1788 |
F f;
|
| 1789 |
A* ap = &f; // succeeds: finds unique A
|
| 1790 |
D* dp = dynamic_cast<D*>(ap); // fails: yields null; f has two D subobjects
|
| 1791 |
-
E* ep = (E*)ap; //
|
| 1792 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 1793 |
}
|
| 1794 |
```
|
| 1795 |
|
| 1796 |
— *end example*]
|
| 1797 |
|
| 1798 |
-
[*Note 1*:
|
| 1799 |
-
applied to an object under construction or
|
|
|
|
| 1800 |
|
| 1801 |
-
### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 1802 |
|
| 1803 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 1804 |
-
`std::type_info`
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
|
| 1808 |
-
|
| 1809 |
-
|
| 1810 |
-
|
| 1811 |
|
| 1812 |
-
When `typeid` is applied to a glvalue
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
`std::bad_typeid` exception ([[bad.typeid]]).
|
| 1821 |
|
| 1822 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 1823 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 1824 |
-
representing the static type of the expression. Lvalue-to-rvalue
|
| 1825 |
-
[[conv.lval]]
|
| 1826 |
-
|
| 1827 |
-
expression
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
|
| 1831 |
When `typeid` is applied to a *type-id*, the result refers to a
|
| 1832 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 1833 |
type of the *type-id* is a reference to a possibly cv-qualified type,
|
| 1834 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 1835 |
object representing the cv-unqualified referenced type. If the type of
|
| 1836 |
the *type-id* is a class type or a reference to a class type, the class
|
| 1837 |
shall be completely-defined.
|
| 1838 |
|
|
|
|
|
|
|
|
|
|
| 1839 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
| 1840 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 1841 |
representing the cv-unqualified type.
|
| 1842 |
|
| 1843 |
[*Example 1*:
|
|
@@ -1853,32 +2942,32 @@ typeid(D) == typeid(d2); // yields true
|
|
| 1853 |
typeid(D) == typeid(const D&); // yields true
|
| 1854 |
```
|
| 1855 |
|
| 1856 |
— *end example*]
|
| 1857 |
|
| 1858 |
-
If the header `<typeinfo>`
|
| 1859 |
-
|
| 1860 |
|
| 1861 |
-
[*Note
|
| 1862 |
-
to an object under construction or destruction. — *end note*]
|
| 1863 |
|
| 1864 |
-
### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
| 1865 |
|
| 1866 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 1867 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 1868 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 1869 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 1870 |
otherwise, the result is a prvalue. The `static_cast` operator shall not
|
| 1871 |
-
cast away constness
|
| 1872 |
|
| 1873 |
An lvalue of type “*cv1* `B`”, where `B` is a class type, can be cast to
|
| 1874 |
-
type “reference to *cv2* `D`”, where `D` is a class derived
|
| 1875 |
-
[[class.derived]]
|
| 1876 |
-
|
| 1877 |
-
|
| 1878 |
-
standard conversion from “pointer to `D`” to “pointer to `B`” exists
|
| 1879 |
-
[[conv.ptr]]
|
| 1880 |
can be cast to type “rvalue reference to *cv2* `D`” with the same
|
| 1881 |
constraints as for an lvalue of type “*cv1* `B`”. If the object of type
|
| 1882 |
“*cv1* `B`” is actually a base class subobject of an object of type `D`,
|
| 1883 |
the result refers to the enclosing object of type `D`. Otherwise, the
|
| 1884 |
behavior is undefined.
|
|
@@ -1889,65 +2978,70 @@ behavior is undefined.
|
|
| 1889 |
struct B { };
|
| 1890 |
struct D : public B { };
|
| 1891 |
D d;
|
| 1892 |
B &br = d;
|
| 1893 |
|
| 1894 |
-
static_cast<D&>(br); // produces lvalue
|
| 1895 |
```
|
| 1896 |
|
| 1897 |
— *end example*]
|
| 1898 |
|
| 1899 |
An lvalue of type “*cv1* `T1`” can be cast to type “rvalue reference to
|
| 1900 |
-
*cv2* `T2`” if “*cv2* `T2`” is reference-compatible with “*cv1* `T1`”
|
| 1901 |
-
[[dcl.init.ref]]
|
| 1902 |
the object or the specified base class subobject thereof; otherwise, the
|
| 1903 |
-
lvalue-to-rvalue conversion
|
| 1904 |
and the resulting prvalue is used as the *expression* of the
|
| 1905 |
-
`static_cast` for the remainder of this
|
| 1906 |
-
inaccessible
|
| 1907 |
-
|
| 1908 |
-
such a cast is ill-formed.
|
| 1909 |
|
| 1910 |
-
An expression
|
| 1911 |
-
|
| 1912 |
-
|
| 1913 |
-
object or reference of type `T` from
|
| 1914 |
-
function
|
| 1915 |
-
|
|
|
|
|
|
|
|
|
|
| 1916 |
|
| 1917 |
``` cpp
|
| 1918 |
-
T t(
|
| 1919 |
```
|
| 1920 |
|
| 1921 |
-
for some invented temporary variable `t`
|
| 1922 |
-
|
| 1923 |
-
result object is direct-initialized from
|
| 1924 |
|
| 1925 |
[*Note 1*: The conversion is ill-formed when attempting to convert an
|
| 1926 |
expression of class type to an inaccessible or ambiguous base
|
| 1927 |
class. — *end note*]
|
| 1928 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1929 |
Otherwise, the `static_cast` shall perform one of the conversions listed
|
| 1930 |
below. No other conversion shall be performed explicitly using a
|
| 1931 |
`static_cast`.
|
| 1932 |
|
| 1933 |
Any expression can be explicitly converted to type cv `void`, in which
|
| 1934 |
-
case it becomes a discarded-value expression
|
| 1935 |
|
| 1936 |
-
[*Note
|
| 1937 |
-
[[class.temporary]]
|
| 1938 |
until the usual time, and the value of the object is preserved for the
|
| 1939 |
purpose of executing the destructor. — *end note*]
|
| 1940 |
|
| 1941 |
-
The inverse of any standard conversion sequence
|
| 1942 |
-
|
| 1943 |
-
[[conv.
|
| 1944 |
-
[[conv.
|
| 1945 |
-
[[conv.
|
| 1946 |
-
|
| 1947 |
-
|
| 1948 |
-
conversion sequence.
|
| 1949 |
|
| 1950 |
[*Example 2*:
|
| 1951 |
|
| 1952 |
``` cpp
|
| 1953 |
struct B { };
|
|
@@ -1958,63 +3052,68 @@ void f() {
|
|
| 1958 |
}
|
| 1959 |
```
|
| 1960 |
|
| 1961 |
— *end example*]
|
| 1962 |
|
| 1963 |
-
The lvalue-to-rvalue
|
| 1964 |
-
|
| 1965 |
-
|
| 1966 |
-
|
| 1967 |
-
|
| 1968 |
-
cases:
|
| 1969 |
|
| 1970 |
-
A value of a scoped enumeration type
|
| 1971 |
-
converted to an integral type
|
| 1972 |
-
|
| 1973 |
-
|
| 1974 |
-
|
| 1975 |
-
|
| 1976 |
-
enumeration type can also be explicitly converted to a floating-point
|
| 1977 |
-
type; the result is the same as that of converting from the original
|
| 1978 |
-
value to the floating-point type.
|
| 1979 |
|
| 1980 |
A value of integral or enumeration type can be explicitly converted to a
|
| 1981 |
-
complete enumeration type.
|
| 1982 |
-
|
| 1983 |
-
|
| 1984 |
-
|
| 1985 |
-
|
| 1986 |
-
|
| 1987 |
-
type
|
|
|
|
|
|
|
|
|
|
| 1988 |
|
| 1989 |
A prvalue of type “pointer to *cv1* `B`”, where `B` is a class type, can
|
| 1990 |
be converted to a prvalue of type “pointer to *cv2* `D`”, where `D` is a
|
| 1991 |
-
class derived
|
| 1992 |
cv-qualification as, or greater cv-qualification than, *cv1*. If `B` is
|
| 1993 |
a virtual base class of `D` or a base class of a virtual base class of
|
| 1994 |
`D`, or if no valid standard conversion from “pointer to `D`” to
|
| 1995 |
-
“pointer to `B`” exists
|
| 1996 |
-
null pointer value
|
| 1997 |
value of the destination type. If the prvalue of type “pointer to *cv1*
|
| 1998 |
`B`” points to a `B` that is actually a subobject of an object of type
|
| 1999 |
`D`, the resulting pointer points to the enclosing object of type `D`.
|
| 2000 |
Otherwise, the behavior is undefined.
|
| 2001 |
|
| 2002 |
A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
|
| 2003 |
converted to a prvalue of type “pointer to member of `B` of type *cv2*
|
| 2004 |
-
`T`”, where `
|
| 2005 |
-
*cv2* is the same cv-qualification as, or
|
| 2006 |
-
*cv1*.
|
| 2007 |
-
`B` of type `T`” to “pointer to member of `D` of type `T`” exists (
|
| 2008 |
-
[[conv.mem]]), the program is ill-formed. The null member pointer
|
| 2009 |
-
value ([[conv.mem]]) is converted to the null member pointer value of
|
| 2010 |
-
the destination type. If class `B` contains the original member, or is a
|
| 2011 |
-
base or derived class of the class containing the original member, the
|
| 2012 |
-
resulting pointer to member points to the original member. Otherwise,
|
| 2013 |
-
the behavior is undefined.
|
| 2014 |
|
| 2015 |
-
[*Note
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2016 |
dynamic type of the object with which indirection through the pointer to
|
| 2017 |
member is performed must contain the original member; see
|
| 2018 |
[[expr.mptr.oper]]. — *end note*]
|
| 2019 |
|
| 2020 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
|
@@ -2023,11 +3122,11 @@ prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
|
|
| 2023 |
*cv1*. If the original pointer value represents the address `A` of a
|
| 2024 |
byte in memory and `A` does not satisfy the alignment requirement of
|
| 2025 |
`T`, then the resulting pointer value is unspecified. Otherwise, if the
|
| 2026 |
original pointer value points to an object *a*, and there is an object
|
| 2027 |
*b* of type `T` (ignoring cv-qualification) that is
|
| 2028 |
-
pointer-interconvertible
|
| 2029 |
pointer to *b*. Otherwise, the pointer value is unchanged by the
|
| 2030 |
conversion.
|
| 2031 |
|
| 2032 |
[*Example 3*:
|
| 2033 |
|
|
@@ -2037,34 +3136,35 @@ const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
|
|
| 2037 |
bool b = p1 == p2; // b will have the value true.
|
| 2038 |
```
|
| 2039 |
|
| 2040 |
— *end example*]
|
| 2041 |
|
| 2042 |
-
### Reinterpret cast <a id="expr.reinterpret.cast">[[expr.reinterpret.cast]]</a>
|
| 2043 |
|
| 2044 |
The result of the expression `reinterpret_cast<T>(v)` is the result of
|
| 2045 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 2046 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 2047 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 2048 |
-
otherwise, the result is a prvalue and the lvalue-to-rvalue
|
| 2049 |
-
[[conv.lval]]
|
| 2050 |
-
|
| 2051 |
-
|
| 2052 |
-
|
| 2053 |
-
|
| 2054 |
|
| 2055 |
-
The `reinterpret_cast` operator shall not cast away constness
|
| 2056 |
-
[[expr.const.cast]]
|
| 2057 |
-
|
| 2058 |
-
|
| 2059 |
|
| 2060 |
[*Note 1*: The mapping performed by `reinterpret_cast` might, or might
|
| 2061 |
not, produce a representation different from the original
|
| 2062 |
value. — *end note*]
|
| 2063 |
|
| 2064 |
A pointer can be explicitly converted to any integral type large enough
|
| 2065 |
-
to hold
|
|
|
|
| 2066 |
|
| 2067 |
[*Note 2*: It is intended to be unsurprising to those who know the
|
| 2068 |
addressing structure of the underlying machine. — *end note*]
|
| 2069 |
|
| 2070 |
A value of type `std::nullptr_t` can be converted to an integral type;
|
|
@@ -2086,23 +3186,23 @@ value. — *end note*]
|
|
| 2086 |
|
| 2087 |
A function pointer can be explicitly converted to a function pointer of
|
| 2088 |
a different type.
|
| 2089 |
|
| 2090 |
[*Note 5*: The effect of calling a function through a pointer to a
|
| 2091 |
-
function type
|
| 2092 |
-
|
| 2093 |
|
| 2094 |
Except that converting a prvalue of type “pointer to `T1`” to the type
|
| 2095 |
“pointer to `T2`” (where `T1` and `T2` are function types) and back to
|
| 2096 |
its original type yields the original pointer value, the result of such
|
| 2097 |
a pointer conversion is unspecified.
|
| 2098 |
|
| 2099 |
[*Note 6*: See also [[conv.ptr]] for more details of pointer
|
| 2100 |
conversions. — *end note*]
|
| 2101 |
|
| 2102 |
An object pointer can be explicitly converted to an object pointer of a
|
| 2103 |
-
different type.[^
|
| 2104 |
converted to the object pointer type “pointer to cv `T`”, the result is
|
| 2105 |
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 2106 |
|
| 2107 |
[*Note 7*: Converting a prvalue of type “pointer to `T1`” to the type
|
| 2108 |
“pointer to `T2`” (where `T1` and `T2` are object types and where the
|
|
@@ -2115,76 +3215,72 @@ conditionally-supported. The meaning of such a conversion is
|
|
| 2115 |
*implementation-defined*, except that if an implementation supports
|
| 2116 |
conversions in both directions, converting a prvalue of one type to the
|
| 2117 |
other type and back, possibly with different cv-qualification, shall
|
| 2118 |
yield the original pointer value.
|
| 2119 |
|
| 2120 |
-
The null pointer value
|
| 2121 |
-
value of the destination type.
|
| 2122 |
|
| 2123 |
[*Note 8*: A null pointer constant of type `std::nullptr_t` cannot be
|
| 2124 |
converted to a pointer type, and a null pointer constant of integral
|
| 2125 |
type is not necessarily converted to a null pointer
|
| 2126 |
value. — *end note*]
|
| 2127 |
|
| 2128 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 2129 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 2130 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 2131 |
-
object types.[^
|
| 2132 |
converted to the null member pointer value of the destination type. The
|
| 2133 |
result of this conversion is unspecified, except in the following cases:
|
| 2134 |
|
| 2135 |
-
-
|
| 2136 |
-
different pointer
|
| 2137 |
-
type yields the original pointer
|
| 2138 |
-
-
|
| 2139 |
`T1`” to the type “pointer to data member of `Y` of type `T2`” (where
|
| 2140 |
the alignment requirements of `T2` are no stricter than those of `T1`)
|
| 2141 |
-
and back to its original type yields the original pointer
|
| 2142 |
value.
|
| 2143 |
|
| 2144 |
-
A glvalue
|
| 2145 |
-
`T2`” if an expression of type “pointer to `T1`” can
|
| 2146 |
-
converted to the type “pointer to `T2`” using a
|
| 2147 |
-
|
| 2148 |
-
|
|
|
|
|
|
|
| 2149 |
|
| 2150 |
-
|
| 2151 |
-
`reinterpret_cast<T&>(x)` has the same effect as the conversion
|
| 2152 |
-
`*reinterpret_cast<T*>(&x)` with the built-in `&` and `*` operators (and
|
| 2153 |
-
similarly for `reinterpret_cast<T&&>(x)`). — *end note*]
|
| 2154 |
-
|
| 2155 |
-
No temporary is created, no copy is made, and constructors (
|
| 2156 |
-
[[class.ctor]]) or conversion functions ([[class.conv]]) are not
|
| 2157 |
-
called.[^14]
|
| 2158 |
-
|
| 2159 |
-
### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 2160 |
|
| 2161 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 2162 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
| 2163 |
is an rvalue reference to object type, the result is an xvalue;
|
| 2164 |
-
otherwise, the result is a prvalue and the lvalue-to-rvalue
|
| 2165 |
-
[[conv.lval]]
|
| 2166 |
-
|
| 2167 |
-
|
| 2168 |
-
|
| 2169 |
-
|
| 2170 |
|
| 2171 |
-
[*Note 1*: Subject to the restrictions in this
|
| 2172 |
may be cast to its own type using a `const_cast`
|
| 2173 |
operator. — *end note*]
|
| 2174 |
|
| 2175 |
-
For two similar types `T1` and `T2`
|
| 2176 |
-
`T1` may be explicitly converted to the type `T2` using a `const_cast`
|
| 2177 |
-
|
|
|
|
|
|
|
| 2178 |
|
| 2179 |
[*Example 1*:
|
| 2180 |
|
| 2181 |
``` cpp
|
| 2182 |
typedef int *A[3]; // array of 3 pointer to int
|
| 2183 |
typedef const int *const CA[3]; // array of 3 const pointer to const int
|
| 2184 |
|
| 2185 |
-
CA &&r = A{};
|
|
|
|
| 2186 |
A &&r1 = const_cast<A>(CA{}); // error: temporary array decayed to pointer
|
| 2187 |
A &&r2 = const_cast<A&&>(CA{}); // OK
|
| 2188 |
```
|
| 2189 |
|
| 2190 |
— *end example*]
|
|
@@ -2200,26 +3296,25 @@ then the following conversions can also be made:
|
|
| 2200 |
- if `T1` is a class type, a prvalue of type `T1` can be explicitly
|
| 2201 |
converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
|
| 2202 |
|
| 2203 |
The result of a reference `const_cast` refers to the original object if
|
| 2204 |
the operand is a glvalue and to the result of applying the temporary
|
| 2205 |
-
materialization conversion
|
| 2206 |
|
| 2207 |
-
A null pointer value
|
| 2208 |
-
value of the destination type. The null member pointer value
|
| 2209 |
-
[[conv.mem]]
|
| 2210 |
destination type.
|
| 2211 |
|
| 2212 |
[*Note 2*: Depending on the type of the object, a write operation
|
| 2213 |
through the pointer, lvalue or pointer to data member resulting from a
|
| 2214 |
-
`const_cast` that casts away a const-qualifier[^
|
| 2215 |
-
undefined behavior
|
| 2216 |
|
| 2217 |
A conversion from a type `T1` to a type `T2` *casts away constness* if
|
| 2218 |
-
`T1` and `T2` are different, there is a cv-decomposition
|
| 2219 |
-
|
| 2220 |
-
cv-decomposition of the form
|
| 2221 |
|
| 2222 |
and there is no qualification conversion that converts `T1` to
|
| 2223 |
|
| 2224 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 2225 |
lvalue reference cast or casting from an expression of type `T1` to an
|
|
@@ -2234,65 +3329,62 @@ conversions lead to values whose use causes undefined behavior. For the
|
|
| 2234 |
same reasons, conversions between pointers to member functions, and in
|
| 2235 |
particular, the conversion from a pointer to a const member function to
|
| 2236 |
a pointer to a non-const member function, are not
|
| 2237 |
covered. — *end note*]
|
| 2238 |
|
| 2239 |
-
## Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 2240 |
|
| 2241 |
Expressions with unary operators group right-to-left.
|
| 2242 |
|
| 2243 |
``` bnf
|
| 2244 |
unary-expression:
|
| 2245 |
postfix-expression
|
|
|
|
| 2246 |
'++' cast-expression
|
| 2247 |
'-{-}' cast-expression
|
| 2248 |
-
|
| 2249 |
-
|
| 2250 |
-
|
| 2251 |
-
|
| 2252 |
-
|
| 2253 |
noexcept-expression
|
| 2254 |
new-expression
|
| 2255 |
delete-expression
|
| 2256 |
```
|
| 2257 |
|
| 2258 |
``` bnf
|
| 2259 |
unary-operator: one of
|
| 2260 |
'* & + - ! ~'
|
| 2261 |
```
|
| 2262 |
|
| 2263 |
-
### Unary operators <a id="expr.unary.op">[[expr.unary.op]]</a>
|
| 2264 |
|
| 2265 |
The unary `*` operator performs *indirection*: the expression to which
|
| 2266 |
it is applied shall be a pointer to an object type, or a pointer to a
|
| 2267 |
function type and the result is an lvalue referring to the object or
|
| 2268 |
function to which the expression points. If the type of the expression
|
| 2269 |
is “pointer to `T`”, the type of the result is “`T`”.
|
| 2270 |
|
| 2271 |
[*Note 1*: Indirection through a pointer to an incomplete type (other
|
| 2272 |
-
than
|
| 2273 |
limited ways (to initialize a reference, for example); this lvalue must
|
| 2274 |
not be converted to a prvalue, see [[conv.lval]]. — *end note*]
|
| 2275 |
|
| 2276 |
The result of each of the following unary operators is a prvalue.
|
| 2277 |
|
| 2278 |
-
The result of the unary `&` operator is a pointer to its operand.
|
| 2279 |
-
operand shall be an lvalue or a *qualified-id*. If the operand is a
|
| 2280 |
-
*qualified-id* naming a non-static or variant member `m` of some class
|
| 2281 |
-
`C` with type `T`, the result has type “pointer to member of class `C`
|
| 2282 |
-
of type `T`” and is a prvalue designating `C::m`. Otherwise, if the type
|
| 2283 |
-
of the expression is `T`, the result has type “pointer to `T`” and is a
|
| 2284 |
-
prvalue that is the address of the designated object ([[intro.memory]])
|
| 2285 |
-
or a pointer to the designated function.
|
| 2286 |
|
| 2287 |
-
|
| 2288 |
-
|
| 2289 |
-
|
| 2290 |
-
|
| 2291 |
-
|
| 2292 |
-
|
| 2293 |
-
|
|
|
|
|
|
|
|
|
|
| 2294 |
|
| 2295 |
[*Example 1*:
|
| 2296 |
|
| 2297 |
``` cpp
|
| 2298 |
struct A { int i; };
|
|
@@ -2305,38 +3397,37 @@ bool b = p2 > p1; // defined behavior, with value true
|
|
| 2305 |
```
|
| 2306 |
|
| 2307 |
— *end example*]
|
| 2308 |
|
| 2309 |
[*Note 3*: A pointer to member formed from a `mutable` non-static data
|
| 2310 |
-
member
|
| 2311 |
-
|
| 2312 |
|
| 2313 |
A pointer to member is only formed when an explicit `&` is used and its
|
| 2314 |
operand is a *qualified-id* not enclosed in parentheses.
|
| 2315 |
|
| 2316 |
[*Note 4*: That is, the expression `&(qualified-id)`, where the
|
| 2317 |
*qualified-id* is enclosed in parentheses, does not form an expression
|
| 2318 |
of type “pointer to member”. Neither does `qualified-id`, because there
|
| 2319 |
is no implicit conversion from a *qualified-id* for a non-static member
|
| 2320 |
function to the type “pointer to member function” as there is from an
|
| 2321 |
-
lvalue of function type to the type “pointer to function”
|
| 2322 |
-
|
| 2323 |
-
|
| 2324 |
|
| 2325 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 2326 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 2327 |
the built-in meaning or the operator function is called. The operand of
|
| 2328 |
`&` shall not be a bit-field.
|
| 2329 |
|
| 2330 |
-
The address of an overloaded function
|
| 2331 |
only in a context that uniquely determines which version of the
|
| 2332 |
-
overloaded function is referred to (see [[over.over]]).
|
| 2333 |
-
|
| 2334 |
-
|
| 2335 |
-
|
| 2336 |
-
|
| 2337 |
-
member function”. — *end note*]
|
| 2338 |
|
| 2339 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 2340 |
enumeration, or pointer type and the result is the value of the
|
| 2341 |
argument. Integral promotion is performed on integral or enumeration
|
| 2342 |
operands. The type of the result is the type of the promoted operand.
|
|
@@ -2347,90 +3438,207 @@ promotion is performed on integral or enumeration operands. The negative
|
|
| 2347 |
of an unsigned quantity is computed by subtracting its value from 2ⁿ,
|
| 2348 |
where n is the number of bits in the promoted operand. The type of the
|
| 2349 |
result is the type of the promoted operand.
|
| 2350 |
|
| 2351 |
The operand of the logical negation operator `!` is contextually
|
| 2352 |
-
converted to `bool`
|
| 2353 |
-
|
| 2354 |
-
|
| 2355 |
|
| 2356 |
The operand of `~` shall have integral or unscoped enumeration type; the
|
| 2357 |
result is the ones’ complement of its operand. Integral promotions are
|
| 2358 |
performed. The type of the result is the type of the promoted operand.
|
| 2359 |
There is an ambiguity in the grammar when `~` is followed by a
|
| 2360 |
-
*
|
| 2361 |
treating `~` as the unary complement operator rather than as the start
|
| 2362 |
of an *unqualified-id* naming a destructor.
|
| 2363 |
|
| 2364 |
[*Note 6*: Because the grammar does not permit an operator to follow
|
| 2365 |
-
the `.`, `->`, or `::` tokens, a `~` followed by a *
|
| 2366 |
*decltype-specifier* in a member access expression or *qualified-id* is
|
| 2367 |
unambiguously parsed as a destructor name. — *end note*]
|
| 2368 |
|
| 2369 |
-
### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
| 2370 |
|
| 2371 |
-
The operand of prefix `++` is modified by adding `1`.
|
| 2372 |
-
be a modifiable lvalue. The type of the operand shall
|
| 2373 |
-
type other than cv `bool`, or a pointer to a
|
| 2374 |
-
type.
|
| 2375 |
-
|
| 2376 |
-
|
|
|
|
| 2377 |
|
| 2378 |
-
[*Note 1*: See the discussions of addition
|
| 2379 |
-
|
| 2380 |
-
conversions. — *end note*]
|
| 2381 |
|
| 2382 |
-
The operand of prefix `\dcr` is modified by subtracting
|
| 2383 |
-
requirements on the operand of prefix `\dcr` and the properties
|
| 2384 |
-
result are otherwise the same as those of prefix `++`.
|
| 2385 |
|
| 2386 |
[*Note 2*: For postfix increment and decrement, see
|
| 2387 |
[[expr.post.incr]]. — *end note*]
|
| 2388 |
|
| 2389 |
-
###
|
| 2390 |
-
|
| 2391 |
-
The `
|
| 2392 |
-
|
| 2393 |
-
|
| 2394 |
-
|
| 2395 |
-
|
| 2396 |
-
|
| 2397 |
-
|
| 2398 |
-
`
|
| 2399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2400 |
|
| 2401 |
[*Note 1*: In particular, `sizeof(bool)`, `sizeof(char16_t)`,
|
| 2402 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 2403 |
-
implementation-defined.[^
|
| 2404 |
|
| 2405 |
-
[*Note 2*: See [[intro.memory]] for the definition of
|
| 2406 |
-
[[basic.types]] for the definition of
|
| 2407 |
-
representation
|
| 2408 |
|
| 2409 |
-
When applied to a reference
|
| 2410 |
-
|
| 2411 |
-
|
| 2412 |
-
|
| 2413 |
-
|
| 2414 |
-
|
| 2415 |
-
|
| 2416 |
-
|
| 2417 |
-
*n* elements is *n* times the size of an element.
|
| 2418 |
|
| 2419 |
-
The
|
| 2420 |
-
|
|
|
|
|
|
|
| 2421 |
|
| 2422 |
-
The
|
| 2423 |
-
|
| 2424 |
-
|
| 2425 |
-
|
| 2426 |
-
is applied.
|
| 2427 |
-
|
| 2428 |
-
The identifier in a `sizeof...` expression shall name a parameter pack.
|
| 2429 |
-
The `sizeof...` operator yields the number of arguments provided for the
|
| 2430 |
-
parameter pack *identifier*. A `sizeof...` expression is a pack
|
| 2431 |
-
expansion ([[temp.variadic]]).
|
| 2432 |
|
| 2433 |
[*Example 1*:
|
| 2434 |
|
| 2435 |
``` cpp
|
| 2436 |
template<class... Types>
|
|
@@ -2439,35 +3647,71 @@ struct count {
|
|
| 2439 |
};
|
| 2440 |
```
|
| 2441 |
|
| 2442 |
— *end example*]
|
| 2443 |
|
| 2444 |
-
The result of `sizeof` and `sizeof...` is a
|
| 2445 |
`std::size_t`.
|
| 2446 |
|
| 2447 |
-
[*Note 3*:
|
|
|
|
| 2448 |
`<cstddef>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 2449 |
|
| 2450 |
-
###
|
| 2451 |
|
| 2452 |
-
|
| 2453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2454 |
object is the *allocated type*. This type shall be a complete object
|
| 2455 |
type, but not an abstract class type or array thereof (
|
| 2456 |
-
[[intro.object]],
|
| 2457 |
|
| 2458 |
[*Note 1*: Because references are not objects, references cannot be
|
| 2459 |
created by *new-expression*s. — *end note*]
|
| 2460 |
|
| 2461 |
[*Note 2*: The *type-id* may be a cv-qualified type, in which case the
|
| 2462 |
object created by the *new-expression* has a cv-qualified
|
| 2463 |
type. — *end note*]
|
| 2464 |
|
| 2465 |
``` bnf
|
| 2466 |
new-expression:
|
| 2467 |
-
'::'ₒₚₜ
|
| 2468 |
-
'::'ₒₚₜ
|
| 2469 |
```
|
| 2470 |
|
| 2471 |
``` bnf
|
| 2472 |
new-placement:
|
| 2473 |
'(' expression-list ')'
|
|
@@ -2484,37 +3728,27 @@ new-declarator:
|
|
| 2484 |
noptr-new-declarator
|
| 2485 |
```
|
| 2486 |
|
| 2487 |
``` bnf
|
| 2488 |
noptr-new-declarator:
|
| 2489 |
-
'[' expression ']' attribute-specifier-seqₒₚₜ
|
| 2490 |
noptr-new-declarator '[' constant-expression ']' attribute-specifier-seqₒₚₜ
|
| 2491 |
```
|
| 2492 |
|
| 2493 |
``` bnf
|
| 2494 |
new-initializer:
|
| 2495 |
'(' expression-listₒₚₜ ')'
|
| 2496 |
braced-init-list
|
| 2497 |
```
|
| 2498 |
|
| 2499 |
-
|
| 2500 |
-
[[basic.stc.dynamic]]).
|
| 2501 |
-
|
| 2502 |
-
[*Note 3*: The lifetime of such an entity is not necessarily
|
| 2503 |
-
restricted to the scope in which it is created. — *end note*]
|
| 2504 |
-
|
| 2505 |
-
If the entity is a non-array object, the *new-expression* returns a
|
| 2506 |
-
pointer to the object created. If it is an array, the *new-expression*
|
| 2507 |
-
returns a pointer to the initial element of the array.
|
| 2508 |
-
|
| 2509 |
-
If a placeholder type ([[dcl.spec.auto]]) appears in the
|
| 2510 |
*type-specifier-seq* of a *new-type-id* or *type-id* of a
|
| 2511 |
*new-expression*, the allocated type is deduced as follows: Let *init*
|
| 2512 |
be the *new-initializer*, if any, and `T` be the *new-type-id* or
|
| 2513 |
*type-id* of the *new-expression*, then the allocated type is the type
|
| 2514 |
-
deduced for the variable `x` in the invented declaration
|
| 2515 |
-
[[dcl.spec.auto]]
|
| 2516 |
|
| 2517 |
``` cpp
|
| 2518 |
T x init ;
|
| 2519 |
```
|
| 2520 |
|
|
@@ -2531,11 +3765,11 @@ auto y = new A{1, 2}; // allocated type is A<int>
|
|
| 2531 |
— *end example*]
|
| 2532 |
|
| 2533 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 2534 |
of *new-declarator*s.
|
| 2535 |
|
| 2536 |
-
[*Note
|
| 2537 |
`&`, `&&`, `*`, and `[]` and their expression
|
| 2538 |
counterparts. — *end note*]
|
| 2539 |
|
| 2540 |
[*Example 2*:
|
| 2541 |
|
|
@@ -2545,11 +3779,11 @@ new int * i; // syntax error: parsed as (new int*) i, not as
|
|
| 2545 |
|
| 2546 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 2547 |
|
| 2548 |
— *end example*]
|
| 2549 |
|
| 2550 |
-
[*Note
|
| 2551 |
|
| 2552 |
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 2553 |
effects.
|
| 2554 |
|
| 2555 |
[*Example 3*:
|
|
@@ -2563,11 +3797,11 @@ is ill-formed because the binding is
|
|
| 2563 |
``` cpp
|
| 2564 |
(new int) (*[10])(); // error
|
| 2565 |
```
|
| 2566 |
|
| 2567 |
Instead, the explicitly parenthesized version of the `new` operator can
|
| 2568 |
-
be used to create objects of compound types
|
| 2569 |
|
| 2570 |
``` cpp
|
| 2571 |
new (int (*[10])());
|
| 2572 |
```
|
| 2573 |
|
|
@@ -2576,10 +3810,19 @@ returning `int`).
|
|
| 2576 |
|
| 2577 |
— *end example*]
|
| 2578 |
|
| 2579 |
— *end note*]
|
| 2580 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2581 |
When the allocated object is an array (that is, the
|
| 2582 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 2583 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 2584 |
initial element (if any) of the array.
|
| 2585 |
|
|
@@ -2588,65 +3831,71 @@ type of `new int[i][10]` is `int (*)[10]` — *end note*]
|
|
| 2588 |
|
| 2589 |
The *attribute-specifier-seq* in a *noptr-new-declarator* appertains to
|
| 2590 |
the associated array type.
|
| 2591 |
|
| 2592 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 2593 |
-
converted constant expression
|
| 2594 |
-
|
| 2595 |
-
*noptr-new-declarator* is implicitly converted to `std::size_t`.
|
| 2596 |
|
| 2597 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 2598 |
well-formed (because `n` is the *expression* of a
|
| 2599 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 2600 |
`n` is not a constant expression). — *end example*]
|
| 2601 |
|
| 2602 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2603 |
|
| 2604 |
- the expression is of non-class type and its value before converting to
|
| 2605 |
`std::size_t` is less than zero;
|
| 2606 |
- the expression is of class type and its value before application of
|
| 2607 |
-
the second standard conversion
|
| 2608 |
zero;
|
| 2609 |
- its value is such that the size of the allocated object would exceed
|
| 2610 |
-
the *implementation-defined* limit
|
| 2611 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 2612 |
elements for which initializers are provided (including the
|
| 2613 |
-
terminating `'\0'` in a string
|
| 2614 |
number of elements to initialize.
|
| 2615 |
|
| 2616 |
If the *expression* is erroneous after converting to `std::size_t`:
|
| 2617 |
|
| 2618 |
- if the *expression* is a core constant expression, the program is
|
| 2619 |
ill-formed;
|
| 2620 |
- otherwise, an allocation function is not called; instead
|
| 2621 |
- if the allocation function that would have been called has a
|
| 2622 |
-
non-throwing exception specification
|
| 2623 |
-
|
| 2624 |
result type;
|
| 2625 |
- otherwise, the *new-expression* terminates by throwing an exception
|
| 2626 |
-
of a type that would match a handler
|
| 2627 |
-
`std::bad_array_new_length`
|
| 2628 |
|
| 2629 |
When the value of the *expression* is zero, the allocation function is
|
| 2630 |
called to allocate an array with no elements.
|
| 2631 |
|
| 2632 |
A *new-expression* may obtain storage for the object by calling an
|
| 2633 |
-
allocation function
|
| 2634 |
*new-expression* terminates by throwing an exception, it may release
|
| 2635 |
-
storage by calling a deallocation function
|
| 2636 |
-
[[basic.stc.dynamic.deallocation]]
|
| 2637 |
-
|
| 2638 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 2639 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 2640 |
the deallocation function’s name is `operator delete[]`.
|
| 2641 |
|
| 2642 |
-
[*Note 7*: An implementation
|
| 2643 |
-
global allocation functions ([[basic.stc.dynamic]],
|
| 2644 |
-
[[new.delete.single]],
|
| 2645 |
-
alternative definitions of these functions
|
| 2646 |
-
and/or class-specific versions
|
| 2647 |
-
|
| 2648 |
include functions that do not perform allocation or deallocation; for
|
| 2649 |
example, see [[new.delete.placement]]. — *end note*]
|
| 2650 |
|
| 2651 |
If the *new-expression* begins with a unary `::` operator, the
|
| 2652 |
allocation function’s name is looked up in the global scope. Otherwise,
|
|
@@ -2656,13 +3905,21 @@ lookup fails to find the name, or if the allocated type is not a class
|
|
| 2656 |
type, the allocation function’s name is looked up in the global scope.
|
| 2657 |
|
| 2658 |
An implementation is allowed to omit a call to a replaceable global
|
| 2659 |
allocation function ([[new.delete.single]], [[new.delete.array]]). When
|
| 2660 |
it does so, the storage is instead provided by the implementation or
|
| 2661 |
-
provided by extending the allocation of another *new-expression*.
|
| 2662 |
-
|
| 2663 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2664 |
true were the allocation not extended:
|
| 2665 |
|
| 2666 |
- the evaluation of `e1` is sequenced before the evaluation of `e2`, and
|
| 2667 |
- `e2` is evaluated whenever `e1` obtains storage, and
|
| 2668 |
- both `e1` and `e2` invoke the same replaceable global allocation
|
|
@@ -2677,20 +3934,20 @@ true were the allocation not extended:
|
|
| 2677 |
`e1`.
|
| 2678 |
|
| 2679 |
[*Example 5*:
|
| 2680 |
|
| 2681 |
``` cpp
|
| 2682 |
-
|
| 2683 |
// These allocations are safe for merging:
|
| 2684 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 2685 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
| 2686 |
std::unique_ptr<char[]> c{new (std::nothrow) char[x]};
|
| 2687 |
|
| 2688 |
g(a.get(), b.get(), c.get());
|
| 2689 |
}
|
| 2690 |
|
| 2691 |
-
|
| 2692 |
std::unique_ptr<char[]> a{new char[8]};
|
| 2693 |
try {
|
| 2694 |
// Merging this allocation would change its catch handler.
|
| 2695 |
std::unique_ptr<char[]> b{new char[x]};
|
| 2696 |
} catch (const std::bad_alloc& e) {
|
|
@@ -2705,18 +3962,19 @@ true were the allocation not extended:
|
|
| 2705 |
When a *new-expression* calls an allocation function and that allocation
|
| 2706 |
has not been extended, the *new-expression* passes the amount of space
|
| 2707 |
requested to the allocation function as the first argument of type
|
| 2708 |
`std::size_t`. That argument shall be no less than the size of the
|
| 2709 |
object being created; it may be greater than the size of the object
|
| 2710 |
-
being created only if the object is an array
|
| 2711 |
-
|
| 2712 |
-
|
| 2713 |
-
|
| 2714 |
-
|
| 2715 |
-
|
|
|
|
| 2716 |
|
| 2717 |
-
[*Note
|
| 2718 |
to storage that is appropriately aligned for objects of any type with
|
| 2719 |
fundamental alignment, this constraint on array allocation overhead
|
| 2720 |
permits the common idiom of allocating character arrays into which
|
| 2721 |
objects of other types will later be placed. — *end note*]
|
| 2722 |
|
|
@@ -2735,14 +3993,19 @@ Overload resolution is performed on a function call created by
|
|
| 2735 |
assembling an argument list. The first argument is the amount of space
|
| 2736 |
requested, and has type `std::size_t`. If the type of the allocated
|
| 2737 |
object has new-extended alignment, the next argument is the type’s
|
| 2738 |
alignment, and has type `std::align_val_t`. If the *new-placement*
|
| 2739 |
syntax is used, the *initializer-clause*s in its *expression-list* are
|
| 2740 |
-
the succeeding arguments. If no matching function is found
|
| 2741 |
-
|
| 2742 |
-
|
| 2743 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2744 |
|
| 2745 |
[*Example 6*:
|
| 2746 |
|
| 2747 |
- `new T` results in one of the following calls:
|
| 2748 |
``` cpp
|
|
@@ -2767,69 +4030,69 @@ again.
|
|
| 2767 |
|
| 2768 |
Here, each instance of `x` is a non-negative unspecified value
|
| 2769 |
representing array allocation overhead; the result of the
|
| 2770 |
*new-expression* will be offset by this amount from the value returned
|
| 2771 |
by `operator new[]`. This overhead may be applied in all array
|
| 2772 |
-
*new-expression*s, including those referencing
|
| 2773 |
-
|
| 2774 |
-
|
| 2775 |
-
to another.
|
| 2776 |
|
| 2777 |
— *end example*]
|
| 2778 |
|
| 2779 |
-
[*Note
|
| 2780 |
-
specification
|
| 2781 |
-
|
| 2782 |
-
[[basic.stc.dynamic.allocation]],
|
| 2783 |
-
|
| 2784 |
-
|
| 2785 |
failure to allocate storage and a non-null pointer
|
| 2786 |
otherwise. — *end note*]
|
| 2787 |
|
| 2788 |
-
If the allocation function is a non-allocating form
|
| 2789 |
-
[[new.delete.placement]]
|
| 2790 |
Otherwise, if the allocation function returns null, initialization shall
|
| 2791 |
not be done, the deallocation function shall not be called, and the
|
| 2792 |
value of the *new-expression* shall be null.
|
| 2793 |
|
| 2794 |
-
[*Note
|
| 2795 |
null, it must be a pointer to a block of storage in which space for the
|
| 2796 |
object has been reserved. The block of storage is assumed to be
|
| 2797 |
appropriately aligned and of the requested size. The address of the
|
| 2798 |
created object will not necessarily be the same as that of the block if
|
| 2799 |
the object is an array. — *end note*]
|
| 2800 |
|
| 2801 |
A *new-expression* that creates an object of type `T` initializes that
|
| 2802 |
object as follows:
|
| 2803 |
|
| 2804 |
-
- If the *new-initializer* is omitted, the object is
|
| 2805 |
-
|
| 2806 |
-
|
| 2807 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 2808 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 2809 |
|
| 2810 |
The invocation of the allocation function is sequenced before the
|
| 2811 |
evaluations of expressions in the *new-initializer*. Initialization of
|
| 2812 |
the allocated object is sequenced before the value computation of the
|
| 2813 |
*new-expression*.
|
| 2814 |
|
| 2815 |
If the *new-expression* creates an object or an array of objects of
|
| 2816 |
class type, access and ambiguity control are done for the allocation
|
| 2817 |
-
function, the deallocation function
|
| 2818 |
-
|
| 2819 |
-
of objects of class type, the
|
| 2820 |
-
[[class.dtor]]
|
| 2821 |
|
| 2822 |
-
If any part of the object initialization described above[^
|
| 2823 |
by throwing an exception and a suitable deallocation function can be
|
| 2824 |
found, the deallocation function is called to free the memory in which
|
| 2825 |
the object was being constructed, after which the exception continues to
|
| 2826 |
propagate in the context of the *new-expression*. If no unambiguous
|
| 2827 |
matching deallocation function can be found, propagating the exception
|
| 2828 |
does not cause the object’s memory to be freed.
|
| 2829 |
|
| 2830 |
-
[*Note
|
| 2831 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 2832 |
leak. — *end note*]
|
| 2833 |
|
| 2834 |
If the *new-expression* begins with a unary `::` operator, the
|
| 2835 |
deallocation function’s name is looked up in the global scope.
|
|
@@ -2839,20 +4102,19 @@ thereof, the deallocation function’s name is looked up in the scope of
|
|
| 2839 |
not a class type or array thereof, the deallocation function’s name is
|
| 2840 |
looked up in the global scope.
|
| 2841 |
|
| 2842 |
A declaration of a placement deallocation function matches the
|
| 2843 |
declaration of a placement allocation function if it has the same number
|
| 2844 |
-
of parameters and, after parameter transformations
|
| 2845 |
parameter types except the first are identical. If the lookup finds a
|
| 2846 |
single matching deallocation function, that function will be called;
|
| 2847 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 2848 |
-
a usual deallocation function
|
| 2849 |
-
[[basic.stc.dynamic.deallocation]]) and that function, considered as a
|
| 2850 |
placement deallocation function, would have been selected as a match for
|
| 2851 |
the allocation function, the program is ill-formed. For a non-placement
|
| 2852 |
allocation function, the normal deallocation function lookup is used to
|
| 2853 |
-
find the matching deallocation function
|
| 2854 |
|
| 2855 |
[*Example 7*:
|
| 2856 |
|
| 2857 |
``` cpp
|
| 2858 |
struct S {
|
|
@@ -2861,74 +4123,72 @@ struct S {
|
|
| 2861 |
|
| 2862 |
// Usual (non-placement) deallocation function:
|
| 2863 |
static void operator delete(void*, std::size_t);
|
| 2864 |
};
|
| 2865 |
|
| 2866 |
-
S* p = new (0) S; //
|
| 2867 |
// placement allocation function
|
| 2868 |
```
|
| 2869 |
|
| 2870 |
— *end example*]
|
| 2871 |
|
| 2872 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 2873 |
returned from the allocation function call as the first argument of type
|
| 2874 |
`void*`. If a placement deallocation function is called, it is passed
|
| 2875 |
the same additional arguments as were passed to the placement allocation
|
| 2876 |
function, that is, the same arguments as those specified with the
|
| 2877 |
-
*new-placement* syntax. If the implementation is allowed to
|
| 2878 |
-
of any argument as part of the call to
|
| 2879 |
-
|
| 2880 |
-
|
| 2881 |
-
call to the allocation function. If the copy is elided in one place, it
|
| 2882 |
-
need not be elided in the other.
|
| 2883 |
|
| 2884 |
-
### Delete <a id="expr.delete">[[expr.delete]]</a>
|
| 2885 |
|
| 2886 |
-
The *delete-expression* operator destroys a most derived object
|
| 2887 |
-
[[intro.object]]
|
| 2888 |
|
| 2889 |
``` bnf
|
| 2890 |
delete-expression:
|
| 2891 |
-
'::'ₒₚₜ
|
| 2892 |
-
'::'ₒₚₜ
|
| 2893 |
```
|
| 2894 |
|
| 2895 |
-
The first alternative is
|
| 2896 |
-
|
| 2897 |
-
square brackets, it shall be interpreted
|
| 2898 |
-
The operand shall be of pointer to
|
| 2899 |
-
class type
|
| 2900 |
-
[[conv]]
|
| 2901 |
-
result has type `void`.
|
| 2902 |
|
| 2903 |
If the operand has a class type, the operand is converted to a pointer
|
| 2904 |
type by calling the above-mentioned conversion function, and the
|
| 2905 |
converted operand is used in place of the original operand for the
|
| 2906 |
-
remainder of this
|
| 2907 |
-
|
| 2908 |
-
|
| 2909 |
-
|
| 2910 |
-
|
| 2911 |
-
|
| 2912 |
-
|
| 2913 |
-
|
| 2914 |
-
behavior is undefined.
|
| 2915 |
|
| 2916 |
[*Note 1*: This means that the syntax of the *delete-expression* must
|
| 2917 |
match the type of the object allocated by `new`, not the syntax of the
|
| 2918 |
*new-expression*. — *end note*]
|
| 2919 |
|
| 2920 |
[*Note 2*: A pointer to a `const` type can be the operand of a
|
| 2921 |
-
*delete-expression*; it is not necessary to cast away the constness
|
| 2922 |
-
[[expr.const.cast]]
|
| 2923 |
operand of the *delete-expression*. — *end note*]
|
| 2924 |
|
| 2925 |
-
In
|
| 2926 |
-
|
| 2927 |
-
|
| 2928 |
-
|
| 2929 |
-
|
|
|
|
| 2930 |
type of the object to be deleted differs from its static type, the
|
| 2931 |
behavior is undefined.
|
| 2932 |
|
| 2933 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 2934 |
exactly once.
|
|
@@ -2936,25 +4196,26 @@ exactly once.
|
|
| 2936 |
If the object being deleted has incomplete class type at the point of
|
| 2937 |
deletion and the complete class has a non-trivial destructor or a
|
| 2938 |
deallocation function, the behavior is undefined.
|
| 2939 |
|
| 2940 |
If the value of the operand of the *delete-expression* is not a null
|
| 2941 |
-
pointer value
|
| 2942 |
-
|
| 2943 |
-
|
| 2944 |
-
|
| 2945 |
-
|
|
|
|
| 2946 |
|
| 2947 |
If the value of the operand of the *delete-expression* is not a null
|
| 2948 |
pointer value, then:
|
| 2949 |
|
| 2950 |
- If the allocation call for the *new-expression* for the object to be
|
| 2951 |
-
deleted was not omitted and the allocation was not extended
|
| 2952 |
-
[[expr.new]]
|
| 2953 |
-
function
|
| 2954 |
-
|
| 2955 |
-
|
| 2956 |
- Otherwise, if the allocation was extended or was provided by extending
|
| 2957 |
the allocation of another *new-expression*, and the
|
| 2958 |
*delete-expression* for every other pointer value produced by a
|
| 2959 |
*new-expression* that had storage provided by the extended
|
| 2960 |
*new-expression* has been evaluated, the *delete-expression* shall
|
|
@@ -2971,122 +4232,113 @@ exception. — *end note*]
|
|
| 2971 |
If the value of the operand of the *delete-expression* is a null pointer
|
| 2972 |
value, it is unspecified whether a deallocation function will be called
|
| 2973 |
as described above.
|
| 2974 |
|
| 2975 |
[*Note 4*: An implementation provides default definitions of the global
|
| 2976 |
-
deallocation functions `operator delete` for non-arrays
|
| 2977 |
-
[[new.delete.single]]
|
| 2978 |
-
[[new.delete.array]]
|
| 2979 |
-
of these functions
|
| 2980 |
-
versions
|
| 2981 |
|
| 2982 |
When the keyword `delete` in a *delete-expression* is preceded by the
|
| 2983 |
unary `::` operator, the deallocation function’s name is looked up in
|
| 2984 |
global scope. Otherwise, the lookup considers class-specific
|
| 2985 |
-
deallocation functions
|
| 2986 |
-
|
| 2987 |
-
|
| 2988 |
|
| 2989 |
If deallocation function lookup finds more than one usual deallocation
|
| 2990 |
function, the function to be called is selected as follows:
|
| 2991 |
|
|
|
|
|
|
|
|
|
|
| 2992 |
- If the type has new-extended alignment, a function with a parameter of
|
| 2993 |
type `std::align_val_t` is preferred; otherwise a function without
|
| 2994 |
-
such a parameter is preferred. If
|
| 2995 |
-
|
| 2996 |
-
|
| 2997 |
-
|
| 2998 |
- If the deallocation functions have class scope, the one without a
|
| 2999 |
parameter of type `std::size_t` is selected.
|
| 3000 |
-
- If the type is complete and if, for
|
| 3001 |
-
|
| 3002 |
-
|
| 3003 |
-
|
| 3004 |
-
selected.
|
| 3005 |
- Otherwise, it is unspecified whether a deallocation function with a
|
| 3006 |
parameter of type `std::size_t` is selected.
|
| 3007 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3008 |
When a *delete-expression* is executed, the selected deallocation
|
| 3009 |
-
function shall be called with the address of the
|
| 3010 |
-
|
| 3011 |
-
for the array allocation overhead
|
| 3012 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3013 |
of type `std::align_val_t` is used, the alignment of the type of the
|
| 3014 |
-
|
| 3015 |
deallocation function with a parameter of type `std::size_t` is used,
|
| 3016 |
-
the size of the
|
| 3017 |
-
|
|
|
|
| 3018 |
|
| 3019 |
-
[*Note
|
| 3020 |
-
and either the first argument was not the result of a prior
|
| 3021 |
-
|
| 3022 |
-
corresponding argument in said call, the behavior
|
| 3023 |
-
[[new.delete.single]],
|
|
|
|
| 3024 |
|
| 3025 |
Access and ambiguity control are done for both the deallocation function
|
| 3026 |
-
and the destructor ([[class.dtor]],
|
| 3027 |
|
| 3028 |
-
###
|
| 3029 |
-
|
| 3030 |
-
An `alignof` expression yields the alignment requirement of its operand
|
| 3031 |
-
type. The operand shall be a *type-id* representing a complete object
|
| 3032 |
-
type, or an array thereof, or a reference to one of those types.
|
| 3033 |
-
|
| 3034 |
-
The result is an integral constant of type `std::size_t`.
|
| 3035 |
-
|
| 3036 |
-
When `alignof` is applied to a reference type, the result is the
|
| 3037 |
-
alignment of the referenced type. When `alignof` is applied to an array
|
| 3038 |
-
type, the result is the alignment of the element type.
|
| 3039 |
-
|
| 3040 |
-
### `noexcept` operator <a id="expr.unary.noexcept">[[expr.unary.noexcept]]</a>
|
| 3041 |
-
|
| 3042 |
-
The `noexcept` operator determines whether the evaluation of its
|
| 3043 |
-
operand, which is an unevaluated operand (Clause [[expr]]), can throw
|
| 3044 |
-
an exception ([[except.throw]]).
|
| 3045 |
-
|
| 3046 |
-
``` bnf
|
| 3047 |
-
noexcept-expression:
|
| 3048 |
-
'noexcept' '(' expression ')'
|
| 3049 |
-
```
|
| 3050 |
-
|
| 3051 |
-
The result of the `noexcept` operator is a constant of type `bool` and
|
| 3052 |
-
is a prvalue.
|
| 3053 |
-
|
| 3054 |
-
The result of the `noexcept` operator is `true` unless the *expression*
|
| 3055 |
-
is potentially-throwing ([[except.spec]]).
|
| 3056 |
-
|
| 3057 |
-
## Explicit type conversion (cast notation) <a id="expr.cast">[[expr.cast]]</a>
|
| 3058 |
|
| 3059 |
The result of the expression `(T)` *cast-expression* is of type `T`. The
|
| 3060 |
result is an lvalue if `T` is an lvalue reference type or an rvalue
|
| 3061 |
reference to function type and an xvalue if `T` is an rvalue reference
|
| 3062 |
to object type; otherwise the result is a prvalue.
|
| 3063 |
|
| 3064 |
[*Note 1*: If `T` is a non-class type that is cv-qualified, the
|
| 3065 |
*cv-qualifier*s are discarded when determining the type of the resulting
|
| 3066 |
-
prvalue; see
|
| 3067 |
|
| 3068 |
-
An explicit type conversion can be expressed using functional notation
|
| 3069 |
-
[[expr.type.conv]]
|
| 3070 |
`static_cast`, `reinterpret_cast`, `const_cast`), or the *cast*
|
| 3071 |
notation.
|
| 3072 |
|
| 3073 |
``` bnf
|
| 3074 |
cast-expression:
|
| 3075 |
unary-expression
|
| 3076 |
'(' type-id ')' cast-expression
|
| 3077 |
```
|
| 3078 |
|
| 3079 |
Any type conversion not mentioned below and not explicitly defined by
|
| 3080 |
-
the user
|
| 3081 |
|
| 3082 |
The conversions performed by
|
| 3083 |
|
| 3084 |
-
- a `const_cast`
|
| 3085 |
-
- a `static_cast`
|
| 3086 |
- a `static_cast` followed by a `const_cast`,
|
| 3087 |
-
- a `reinterpret_cast`
|
| 3088 |
- a `reinterpret_cast` followed by a `const_cast`,
|
| 3089 |
|
| 3090 |
can be performed using the cast notation of explicit type conversion.
|
| 3091 |
The same semantic restrictions and behaviors apply, with the exception
|
| 3092 |
that in performing a `static_cast` in the following situations the
|
|
@@ -3134,11 +4386,11 @@ inheritance relationship between the two classes.
|
|
| 3134 |
[*Note 2*: For example, if the classes were defined later in the
|
| 3135 |
translation unit, a multi-pass compiler would be permitted to interpret
|
| 3136 |
a cast between pointers to the classes as if the class types were
|
| 3137 |
complete at the point of the cast. — *end note*]
|
| 3138 |
|
| 3139 |
-
## Pointer-to-member operators <a id="expr.mptr.oper">[[expr.mptr.oper]]</a>
|
| 3140 |
|
| 3141 |
The pointer-to-member operators `->*` and `.*` group left-to-right.
|
| 3142 |
|
| 3143 |
``` bnf
|
| 3144 |
pm-expression:
|
|
@@ -3162,30 +4414,30 @@ converted into the equivalent form `(*(E1)).*E2`.
|
|
| 3162 |
Abbreviating *pm-expression*`.*`*cast-expression* as `E1.*E2`, `E1` is
|
| 3163 |
called the *object expression*. If the dynamic type of `E1` does not
|
| 3164 |
contain the member to which `E2` refers, the behavior is undefined.
|
| 3165 |
Otherwise, the expression `E1` is sequenced before the expression `E2`.
|
| 3166 |
|
| 3167 |
-
The restrictions on
|
| 3168 |
-
|
| 3169 |
-
|
| 3170 |
-
|
| 3171 |
|
| 3172 |
[*Note 1*:
|
| 3173 |
|
| 3174 |
It is not possible to use a pointer to member that refers to a `mutable`
|
| 3175 |
-
member to modify a
|
| 3176 |
|
| 3177 |
``` cpp
|
| 3178 |
struct S {
|
| 3179 |
S() : i(0) { }
|
| 3180 |
mutable int i;
|
| 3181 |
};
|
| 3182 |
void f()
|
| 3183 |
{
|
| 3184 |
const S cs;
|
| 3185 |
int S::* pm = &S::i; // pm refers to mutable member S::i
|
| 3186 |
-
cs.*pm = 88; //
|
| 3187 |
}
|
| 3188 |
```
|
| 3189 |
|
| 3190 |
— *end note*]
|
| 3191 |
|
|
@@ -3202,21 +4454,22 @@ calls the member function denoted by `ptr_to_mfct` for the object
|
|
| 3202 |
pointed to by `ptr_to_obj`.
|
| 3203 |
|
| 3204 |
— *end example*]
|
| 3205 |
|
| 3206 |
In a `.*` expression whose object expression is an rvalue, the program
|
| 3207 |
-
is ill-formed if the second operand is a pointer to member function
|
| 3208 |
-
*ref-qualifier* `&`
|
| 3209 |
-
|
| 3210 |
-
|
| 3211 |
-
|
| 3212 |
-
|
| 3213 |
-
|
| 3214 |
-
|
| 3215 |
-
|
|
|
|
| 3216 |
|
| 3217 |
-
## Multiplicative operators <a id="expr.mul">[[expr.mul]]</a>
|
| 3218 |
|
| 3219 |
The multiplicative operators `*`, `/`, and `%` group left-to-right.
|
| 3220 |
|
| 3221 |
``` bnf
|
| 3222 |
multiplicative-expression:
|
|
@@ -3226,28 +4479,28 @@ multiplicative-expression:
|
|
| 3226 |
multiplicative-expression '%' pm-expression
|
| 3227 |
```
|
| 3228 |
|
| 3229 |
The operands of `*` and `/` shall have arithmetic or unscoped
|
| 3230 |
enumeration type; the operands of `%` shall have integral or unscoped
|
| 3231 |
-
enumeration type. The usual arithmetic conversions
|
| 3232 |
-
operands and determine the type of the result.
|
| 3233 |
|
| 3234 |
The binary `*` operator indicates multiplication.
|
| 3235 |
|
| 3236 |
The binary `/` operator yields the quotient, and the binary `%` operator
|
| 3237 |
yields the remainder from the division of the first expression by the
|
| 3238 |
second. If the second operand of `/` or `%` is zero the behavior is
|
| 3239 |
undefined. For integral operands the `/` operator yields the algebraic
|
| 3240 |
-
quotient with any fractional part discarded;[^
|
| 3241 |
is representable in the type of the result, `(a/b)*b + a%b` is equal to
|
| 3242 |
`a`; otherwise, the behavior of both `a/b` and `a%b` is undefined.
|
| 3243 |
|
| 3244 |
-
## Additive operators <a id="expr.add">[[expr.add]]</a>
|
| 3245 |
|
| 3246 |
The additive operators `+` and `-` group left-to-right. The usual
|
| 3247 |
-
arithmetic conversions are performed for operands of
|
| 3248 |
-
enumeration type.
|
| 3249 |
|
| 3250 |
``` bnf
|
| 3251 |
additive-expression:
|
| 3252 |
multiplicative-expression
|
| 3253 |
additive-expression '+' multiplicative-expression
|
|
@@ -3269,45 +4522,45 @@ For subtraction, one of the following shall hold:
|
|
| 3269 |
|
| 3270 |
The result of the binary `+` operator is the sum of the operands. The
|
| 3271 |
result of the binary `-` operator is the difference resulting from the
|
| 3272 |
subtraction of the second operand from the first.
|
| 3273 |
|
| 3274 |
-
When an expression that has integral type is added to or subtracted
|
| 3275 |
-
|
| 3276 |
-
expression `P` points to element x[i] of an array object `x` with n
|
| 3277 |
-
elements, [^25] the expressions `P + J` and `J + P` (where `J` has the
|
| 3278 |
-
value j) point to the (possibly-hypothetical) element x[i + j] if
|
| 3279 |
-
0 ≤ i + j ≤ n; otherwise, the behavior is undefined. Likewise, the
|
| 3280 |
-
expression `P - J` points to the (possibly-hypothetical) element
|
| 3281 |
-
x[i - j] if 0 ≤ i - j ≤ n; otherwise, the behavior is undefined.
|
| 3282 |
|
| 3283 |
-
|
| 3284 |
-
|
| 3285 |
-
|
| 3286 |
-
|
| 3287 |
-
|
| 3288 |
-
|
| 3289 |
-
|
|
|
|
|
|
|
| 3290 |
|
| 3291 |
-
|
| 3292 |
-
|
| 3293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3294 |
|
| 3295 |
For addition or subtraction, if the expressions `P` or `Q` have type
|
| 3296 |
“pointer to cv `T`”, where `T` and the array element type are not
|
| 3297 |
-
similar
|
| 3298 |
|
| 3299 |
[*Note 2*: In particular, a pointer to a base class cannot be used for
|
| 3300 |
pointer arithmetic when the array contains objects of a derived class
|
| 3301 |
type. — *end note*]
|
| 3302 |
|
| 3303 |
-
|
| 3304 |
-
result is a null pointer value. If two null pointer values are
|
| 3305 |
-
subtracted, the result compares equal to the value 0 converted to the
|
| 3306 |
-
type `std::ptrdiff_t`.
|
| 3307 |
-
|
| 3308 |
-
## Shift operators <a id="expr.shift">[[expr.shift]]</a>
|
| 3309 |
|
| 3310 |
The shift operators `<<` and `>>` group left-to-right.
|
| 3311 |
|
| 3312 |
``` bnf
|
| 3313 |
shift-expression:
|
|
@@ -3317,118 +4570,196 @@ shift-expression:
|
|
| 3317 |
```
|
| 3318 |
|
| 3319 |
The operands shall be of integral or unscoped enumeration type and
|
| 3320 |
integral promotions are performed. The type of the result is that of the
|
| 3321 |
promoted left operand. The behavior is undefined if the right operand is
|
| 3322 |
-
negative, or greater than or equal to the
|
| 3323 |
-
|
| 3324 |
|
| 3325 |
-
The value of `E1 << E2` is `E1`
|
| 3326 |
-
|
| 3327 |
-
result is $\mathrm{E1}\times2^\mathrm{E2}$, reduced modulo one more than
|
| 3328 |
-
the maximum value representable in the result type. Otherwise, if `E1`
|
| 3329 |
-
has a signed type and non-negative value, and
|
| 3330 |
-
$\mathrm{E1}\times2^\mathrm{E2}$ is representable in the corresponding
|
| 3331 |
-
unsigned type of the result type, then that value, converted to the
|
| 3332 |
-
result type, is the resulting value; otherwise, the behavior is
|
| 3333 |
-
undefined.
|
| 3334 |
|
| 3335 |
-
|
| 3336 |
-
|
| 3337 |
-
|
| 3338 |
-
|
| 3339 |
-
|
|
|
|
|
|
|
|
|
|
| 3340 |
|
| 3341 |
The expression `E1` is sequenced before the expression `E2`.
|
| 3342 |
|
| 3343 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3344 |
|
| 3345 |
The relational operators group left-to-right.
|
| 3346 |
|
| 3347 |
[*Example 1*: `a<b<c` means `(a<b)<c` and *not*
|
| 3348 |
`(a<b)&&(b<c)`. — *end example*]
|
| 3349 |
|
| 3350 |
``` bnf
|
| 3351 |
relational-expression:
|
| 3352 |
-
|
| 3353 |
-
relational-expression '<'
|
| 3354 |
-
relational-expression '>'
|
| 3355 |
-
relational-expression '<='
|
| 3356 |
-
relational-expression '>='
|
| 3357 |
```
|
| 3358 |
|
| 3359 |
-
The
|
| 3360 |
-
|
| 3361 |
-
|
| 3362 |
-
|
| 3363 |
|
| 3364 |
-
The
|
| 3365 |
-
|
| 3366 |
-
|
| 3367 |
-
|
| 3368 |
-
type (Clause [[expr]]). After conversions, the operands shall have the
|
| 3369 |
-
same type.
|
| 3370 |
|
| 3371 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3372 |
|
| 3373 |
- If two pointers point to different elements of the same array, or to
|
| 3374 |
subobjects thereof, the pointer to the element with the higher
|
| 3375 |
-
subscript
|
| 3376 |
- If two pointers point to different non-static data members of the same
|
| 3377 |
object, or to subobjects of such members, recursively, the pointer to
|
| 3378 |
-
the later declared member
|
| 3379 |
-
have the same access control
|
| 3380 |
-
their class is not a union.
|
| 3381 |
-
- Otherwise, neither pointer
|
|
|
|
| 3382 |
|
| 3383 |
-
If two operands `p` and `q` compare equal
|
| 3384 |
-
|
| 3385 |
-
|
| 3386 |
-
`
|
| 3387 |
-
|
| 3388 |
-
|
| 3389 |
|
| 3390 |
If both operands (after conversions) are of arithmetic or enumeration
|
| 3391 |
type, each of the operators shall yield `true` if the specified
|
| 3392 |
relationship is true and `false` if it is false.
|
| 3393 |
|
| 3394 |
-
## Equality operators <a id="expr.eq">[[expr.eq]]</a>
|
| 3395 |
|
| 3396 |
``` bnf
|
| 3397 |
equality-expression:
|
| 3398 |
relational-expression
|
| 3399 |
equality-expression '==' relational-expression
|
| 3400 |
equality-expression '!=' relational-expression
|
| 3401 |
```
|
| 3402 |
|
| 3403 |
The `==` (equal to) and the `!=` (not equal to) operators group
|
| 3404 |
-
left-to-right. The
|
| 3405 |
-
|
| 3406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3407 |
each case below, the operands shall have the same type after the
|
| 3408 |
specified conversions have been applied.
|
| 3409 |
|
| 3410 |
-
If at least one of the operands is a pointer, pointer conversions
|
| 3411 |
-
[[conv.ptr]]
|
| 3412 |
-
qualification conversions
|
| 3413 |
-
|
| 3414 |
-
|
| 3415 |
|
| 3416 |
- If one pointer represents the address of a complete object, and
|
| 3417 |
another pointer represents the address one past the last element of a
|
| 3418 |
-
different complete object,[^
|
| 3419 |
unspecified.
|
| 3420 |
- Otherwise, if the pointers are both null, both point to the same
|
| 3421 |
-
function, or both represent the same address
|
| 3422 |
-
|
| 3423 |
- Otherwise, the pointers compare unequal.
|
| 3424 |
|
| 3425 |
-
If at least one of the operands is a pointer to member,
|
| 3426 |
-
member conversions
|
| 3427 |
-
[[conv.
|
| 3428 |
-
|
| 3429 |
-
is defined as follows:
|
| 3430 |
|
| 3431 |
- If two pointers to members are both the null member pointer value,
|
| 3432 |
they compare equal.
|
| 3433 |
- If only one of two pointers to members is the null member pointer
|
| 3434 |
value, they compare unequal.
|
|
@@ -3448,15 +4779,15 @@ is defined as follows:
|
|
| 3448 |
|
| 3449 |
bool b1 = (bx == cx); // unspecified
|
| 3450 |
```
|
| 3451 |
|
| 3452 |
— *end example*]
|
| 3453 |
-
- If both refer to (possibly different) members of the same union
|
| 3454 |
-
[[class.union]]
|
| 3455 |
- Otherwise, two pointers to members compare equal if they would refer
|
| 3456 |
-
to the same member of the same most derived object
|
| 3457 |
-
|
| 3458 |
associated class type were performed, otherwise they compare unequal.
|
| 3459 |
\[*Example 2*:
|
| 3460 |
``` cpp
|
| 3461 |
struct B {
|
| 3462 |
int f();
|
|
@@ -3484,126 +4815,142 @@ operator and `false` for the `!=` operator. If two operands compare
|
|
| 3484 |
unequal, the result is `false` for the `==` operator and `true` for the
|
| 3485 |
`!=` operator. Otherwise, the result of each of the operators is
|
| 3486 |
unspecified.
|
| 3487 |
|
| 3488 |
If both operands are of arithmetic or enumeration type, the usual
|
| 3489 |
-
arithmetic conversions are performed on both
|
| 3490 |
-
operators shall yield `true` if the specified
|
| 3491 |
-
`false` if it is false.
|
| 3492 |
|
| 3493 |
-
## Bitwise AND operator <a id="expr.bit.and">[[expr.bit.and]]</a>
|
| 3494 |
|
| 3495 |
``` bnf
|
| 3496 |
and-expression:
|
| 3497 |
equality-expression
|
| 3498 |
and-expression '&' equality-expression
|
| 3499 |
```
|
| 3500 |
|
| 3501 |
-
The
|
| 3502 |
-
|
| 3503 |
-
|
|
|
|
|
|
|
|
|
|
| 3504 |
|
| 3505 |
-
|
|
|
|
|
|
|
|
|
|
| 3506 |
|
| 3507 |
``` bnf
|
| 3508 |
exclusive-or-expression:
|
| 3509 |
and-expression
|
| 3510 |
exclusive-or-expression '^' and-expression
|
| 3511 |
```
|
| 3512 |
|
| 3513 |
-
The
|
| 3514 |
-
|
| 3515 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3516 |
|
| 3517 |
-
|
|
|
|
|
|
|
|
|
|
| 3518 |
|
| 3519 |
``` bnf
|
| 3520 |
inclusive-or-expression:
|
| 3521 |
exclusive-or-expression
|
| 3522 |
inclusive-or-expression '|' exclusive-or-expression
|
| 3523 |
```
|
| 3524 |
|
| 3525 |
-
The
|
| 3526 |
-
|
| 3527 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3528 |
|
| 3529 |
-
|
|
|
|
|
|
|
|
|
|
| 3530 |
|
| 3531 |
``` bnf
|
| 3532 |
logical-and-expression:
|
| 3533 |
inclusive-or-expression
|
| 3534 |
logical-and-expression '&&' inclusive-or-expression
|
| 3535 |
```
|
| 3536 |
|
| 3537 |
The `&&` operator groups left-to-right. The operands are both
|
| 3538 |
-
contextually converted to `bool`
|
| 3539 |
-
|
| 3540 |
-
|
| 3541 |
-
|
| 3542 |
|
| 3543 |
-
The result is a `bool`. If the second expression is evaluated,
|
| 3544 |
-
|
| 3545 |
-
|
| 3546 |
-
with the second expression.
|
| 3547 |
|
| 3548 |
-
## Logical OR operator <a id="expr.log.or">[[expr.log.or]]</a>
|
| 3549 |
|
| 3550 |
``` bnf
|
| 3551 |
logical-or-expression:
|
| 3552 |
logical-and-expression
|
| 3553 |
logical-or-expression '||' logical-and-expression
|
| 3554 |
```
|
| 3555 |
|
| 3556 |
The `||` operator groups left-to-right. The operands are both
|
| 3557 |
-
contextually converted to `bool`
|
| 3558 |
-
|
| 3559 |
`||` guarantees left-to-right evaluation; moreover, the second operand
|
| 3560 |
is not evaluated if the first operand evaluates to `true`.
|
| 3561 |
|
| 3562 |
-
The result is a `bool`. If the second expression is evaluated,
|
| 3563 |
-
|
| 3564 |
-
|
| 3565 |
-
with the second expression.
|
| 3566 |
|
| 3567 |
-
## Conditional operator <a id="expr.cond">[[expr.cond]]</a>
|
| 3568 |
|
| 3569 |
``` bnf
|
| 3570 |
conditional-expression:
|
| 3571 |
logical-or-expression
|
| 3572 |
logical-or-expression '?' expression ':' assignment-expression
|
| 3573 |
```
|
| 3574 |
|
| 3575 |
Conditional expressions group right-to-left. The first expression is
|
| 3576 |
-
contextually converted to `bool`
|
| 3577 |
-
|
| 3578 |
-
|
| 3579 |
-
|
| 3580 |
-
|
| 3581 |
-
sequenced before every value computation and side effect associated with
|
| 3582 |
-
the second or third expression.
|
| 3583 |
|
| 3584 |
If either the second or the third operand has type `void`, one of the
|
| 3585 |
following shall hold:
|
| 3586 |
|
| 3587 |
- The second or the third operand (but not both) is a (possibly
|
| 3588 |
-
parenthesized) *throw-expression*
|
| 3589 |
-
|
| 3590 |
-
|
| 3591 |
- Both the second and the third operands have type `void`; the result is
|
| 3592 |
of type `void` and is a prvalue. \[*Note 1*: This includes the case
|
| 3593 |
where both operands are *throw-expression*s. — *end note*]
|
| 3594 |
|
| 3595 |
Otherwise, if the second and third operand are glvalue bit-fields of the
|
| 3596 |
same value category and of types *cv1* `T` and *cv2* `T`, respectively,
|
| 3597 |
-
the operands are considered to be of type
|
| 3598 |
-
this
|
| 3599 |
|
| 3600 |
Otherwise, if the second and third operand have different types and
|
| 3601 |
either has (possibly cv-qualified) class type, or if both are glvalues
|
| 3602 |
of the same value category and the same type except for
|
| 3603 |
cv-qualification, an attempt is made to form an implicit conversion
|
| 3604 |
-
sequence
|
| 3605 |
the other.
|
| 3606 |
|
| 3607 |
[*Note 2*: Properties such as access, whether an operand is a
|
| 3608 |
bit-field, or whether a conversion function is deleted are ignored for
|
| 3609 |
that determination. — *end note*]
|
|
@@ -3612,22 +4959,24 @@ Attempts are made to form an implicit conversion sequence from an
|
|
| 3612 |
operand expression `E1` of type `T1` to a target type related to the
|
| 3613 |
type `T2` of the operand expression `E2` as follows:
|
| 3614 |
|
| 3615 |
- If `E2` is an lvalue, the target type is “lvalue reference to `T2`”,
|
| 3616 |
subject to the constraint that in the conversion the reference must
|
| 3617 |
-
bind directly
|
| 3618 |
- If `E2` is an xvalue, the target type is “rvalue reference to `T2`”,
|
| 3619 |
subject to the constraint that the reference must bind directly.
|
| 3620 |
- If `E2` is a prvalue or if neither of the conversion sequences above
|
| 3621 |
can be formed and at least one of the operands has (possibly
|
| 3622 |
cv-qualified) class type:
|
| 3623 |
-
- if `T1` and `T2` are the same class type (ignoring
|
| 3624 |
-
|
| 3625 |
-
|
|
|
|
|
|
|
| 3626 |
- otherwise, the target type is the type that `E2` would have after
|
| 3627 |
-
applying the lvalue-to-rvalue
|
| 3628 |
-
[[conv.array]]
|
| 3629 |
conversions.
|
| 3630 |
|
| 3631 |
Using this process, it is determined whether an implicit conversion
|
| 3632 |
sequence can be formed from the second operand to the target type
|
| 3633 |
determined for the third operand, and vice versa. If both sequences can
|
|
@@ -3635,11 +4984,11 @@ be formed, or one can be formed but it is the ambiguous conversion
|
|
| 3635 |
sequence, the program is ill-formed. If no conversion sequence can be
|
| 3636 |
formed, the operands are left unchanged and further checking is
|
| 3637 |
performed as described below. Otherwise, if exactly one conversion
|
| 3638 |
sequence can be formed, that conversion is applied to the chosen operand
|
| 3639 |
and the converted operand is used in place of the original operand for
|
| 3640 |
-
the remainder of this
|
| 3641 |
|
| 3642 |
[*Note 3*: The conversion might be ill-formed even if an implicit
|
| 3643 |
conversion sequence could be formed. — *end note*]
|
| 3644 |
|
| 3645 |
If the second and third operands are glvalues of the same value category
|
|
@@ -3648,58 +4997,110 @@ and it is a bit-field if the second or the third operand is a bit-field,
|
|
| 3648 |
or if both are bit-fields.
|
| 3649 |
|
| 3650 |
Otherwise, the result is a prvalue. If the second and third operands do
|
| 3651 |
not have the same type, and either has (possibly cv-qualified) class
|
| 3652 |
type, overload resolution is used to determine the conversions (if any)
|
| 3653 |
-
to be applied to the operands ([[over.match.oper]],
|
| 3654 |
-
|
| 3655 |
-
|
| 3656 |
-
|
| 3657 |
-
|
| 3658 |
|
| 3659 |
-
Lvalue-to-rvalue
|
| 3660 |
-
|
| 3661 |
-
|
| 3662 |
-
|
| 3663 |
|
| 3664 |
- The second and third operands have the same type; the result is of
|
| 3665 |
that type and the result object is initialized using the selected
|
| 3666 |
operand.
|
| 3667 |
- The second and third operands have arithmetic or enumeration type; the
|
| 3668 |
-
usual arithmetic conversions are performed to
|
| 3669 |
-
type, and the result is of that type.
|
| 3670 |
- One or both of the second and third operands have pointer type;
|
| 3671 |
-
pointer conversions
|
| 3672 |
-
[[conv.fctptr]]
|
| 3673 |
-
performed to bring them to their composite pointer type
|
| 3674 |
-
|
| 3675 |
-
- One or both of the second and third operands have pointer
|
| 3676 |
-
type; pointer to member conversions
|
| 3677 |
-
conversions
|
| 3678 |
-
|
| 3679 |
-
composite pointer type.
|
| 3680 |
- Both the second and third operands have type `std::nullptr_t` or one
|
| 3681 |
has that type and the other is a null pointer constant. The result is
|
| 3682 |
of type `std::nullptr_t`.
|
| 3683 |
|
| 3684 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3685 |
|
| 3686 |
``` bnf
|
| 3687 |
throw-expression:
|
| 3688 |
-
|
| 3689 |
```
|
| 3690 |
|
| 3691 |
A *throw-expression* is of type `void`.
|
| 3692 |
|
| 3693 |
-
Evaluating a *throw-expression* with an operand throws an exception
|
| 3694 |
-
[[except.throw]]
|
| 3695 |
removing any top-level *cv-qualifier*s from the static type of the
|
| 3696 |
operand and adjusting the type from “array of `T`” or function type `T`
|
| 3697 |
to “pointer to `T`”.
|
| 3698 |
|
| 3699 |
A *throw-expression* with no operand rethrows the currently handled
|
| 3700 |
-
exception
|
| 3701 |
existing exception object; no new exception object is created. The
|
| 3702 |
exception is no longer considered to be caught.
|
| 3703 |
|
| 3704 |
[*Example 1*:
|
| 3705 |
|
|
@@ -3716,72 +5117,71 @@ try {
|
|
| 3716 |
```
|
| 3717 |
|
| 3718 |
— *end example*]
|
| 3719 |
|
| 3720 |
If no exception is presently being handled, evaluating a
|
| 3721 |
-
*throw-expression* with no operand calls `std::{}terminate()`
|
| 3722 |
-
[[except.terminate]]
|
| 3723 |
|
| 3724 |
-
## Assignment and compound assignment operators <a id="expr.ass">[[expr.ass]]</a>
|
| 3725 |
|
| 3726 |
The assignment operator (`=`) and the compound assignment operators all
|
| 3727 |
group right-to-left. All require a modifiable lvalue as their left
|
| 3728 |
-
operand
|
| 3729 |
-
in all cases is a bit-field if the left operand is a bit-field.
|
| 3730 |
-
cases, the assignment is sequenced after the value computation of
|
| 3731 |
-
right and left operands, and before the value computation of the
|
| 3732 |
assignment expression. The right operand is sequenced before the left
|
| 3733 |
operand. With respect to an indeterminately-sequenced function call, the
|
| 3734 |
operation of a compound assignment is a single evaluation.
|
| 3735 |
|
| 3736 |
-
[*Note 1*: Therefore, a function call
|
| 3737 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 3738 |
single compound assignment operator. — *end note*]
|
| 3739 |
|
| 3740 |
``` bnf
|
| 3741 |
assignment-expression:
|
| 3742 |
conditional-expression
|
| 3743 |
-
|
| 3744 |
throw-expression
|
|
|
|
| 3745 |
```
|
| 3746 |
|
| 3747 |
``` bnf
|
| 3748 |
assignment-operator: one of
|
| 3749 |
'= *= /= %= += -= >>= <<= &= ^= |='
|
| 3750 |
```
|
| 3751 |
|
| 3752 |
-
In simple assignment (`=`), the
|
| 3753 |
-
|
|
|
|
| 3754 |
|
| 3755 |
-
If the
|
| 3756 |
-
|
| 3757 |
-
operand.
|
| 3758 |
-
|
| 3759 |
-
If the left operand is of class type, the class shall be complete.
|
| 3760 |
-
Assignment to objects of a class is defined by the copy/move assignment
|
| 3761 |
-
operator ([[class.copy]], [[over.ass]]).
|
| 3762 |
-
|
| 3763 |
-
[*Note 2*: For class objects, assignment is not in general the same as
|
| 3764 |
-
initialization ([[dcl.init]], [[class.ctor]], [[class.init]],
|
| 3765 |
-
[[class.copy]]). — *end note*]
|
| 3766 |
|
| 3767 |
When the left operand of an assignment operator is a bit-field that
|
| 3768 |
cannot represent the value of the expression, the resulting value of the
|
| 3769 |
bit-field is *implementation-defined*.
|
| 3770 |
|
| 3771 |
-
|
| 3772 |
-
|
| 3773 |
-
|
| 3774 |
-
|
| 3775 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3776 |
|
| 3777 |
If the value being stored in an object is read via another object that
|
| 3778 |
overlaps in any way the storage of the first object, then the overlap
|
| 3779 |
shall be exact and the two objects shall have the same type, otherwise
|
| 3780 |
the behavior is undefined.
|
| 3781 |
|
| 3782 |
-
[*Note
|
| 3783 |
left and right sides of the assignment operation; it is not a statement
|
| 3784 |
about how the target of the assignment may be aliased in general. See
|
| 3785 |
[[basic.lval]]. — *end note*]
|
| 3786 |
|
| 3787 |
A *braced-init-list* may appear on the right-hand side of
|
|
@@ -3806,36 +5206,34 @@ a = b = { 1 }; // meaning a=b=1;
|
|
| 3806 |
a = { 1 } = b; // syntax error
|
| 3807 |
```
|
| 3808 |
|
| 3809 |
— *end example*]
|
| 3810 |
|
| 3811 |
-
## Comma operator <a id="expr.comma">[[expr.comma]]</a>
|
| 3812 |
|
| 3813 |
The comma operator groups left-to-right.
|
| 3814 |
|
| 3815 |
``` bnf
|
| 3816 |
expression:
|
| 3817 |
assignment-expression
|
| 3818 |
expression ',' assignment-expression
|
| 3819 |
```
|
| 3820 |
|
| 3821 |
A pair of expressions separated by a comma is evaluated left-to-right;
|
| 3822 |
-
the left expression is a discarded-value expression
|
| 3823 |
-
|
| 3824 |
-
|
| 3825 |
-
|
| 3826 |
-
|
| 3827 |
-
|
| 3828 |
-
operand is a bit-field. If the right operand is a temporary expression (
|
| 3829 |
-
[[class.temporary]]), the result is a temporary expression.
|
| 3830 |
|
| 3831 |
In contexts where comma is given a special meaning,
|
| 3832 |
|
| 3833 |
-
[*Example 1*: in lists of arguments to functions
|
| 3834 |
-
lists of initializers
|
| 3835 |
|
| 3836 |
-
the comma operator as described in
|
| 3837 |
parentheses.
|
| 3838 |
|
| 3839 |
[*Example 2*:
|
| 3840 |
|
| 3841 |
``` cpp
|
|
@@ -3844,133 +5242,173 @@ f(a, (t=3, t+2), c);
|
|
| 3844 |
|
| 3845 |
has three arguments, the second of which has the value `5`.
|
| 3846 |
|
| 3847 |
— *end example*]
|
| 3848 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3849 |
## Constant expressions <a id="expr.const">[[expr.const]]</a>
|
| 3850 |
|
| 3851 |
Certain contexts require expressions that satisfy additional
|
| 3852 |
requirements as detailed in this subclause; other contexts have
|
| 3853 |
different semantics depending on whether or not an expression satisfies
|
| 3854 |
these requirements. Expressions that satisfy these requirements,
|
| 3855 |
-
assuming that copy elision is performed, are
|
| 3856 |
-
expressions*.
|
| 3857 |
|
| 3858 |
[*Note 1*: Constant expressions can be evaluated during
|
| 3859 |
translation. — *end note*]
|
| 3860 |
|
| 3861 |
``` bnf
|
| 3862 |
constant-expression:
|
| 3863 |
conditional-expression
|
| 3864 |
```
|
| 3865 |
|
| 3866 |
-
|
| 3867 |
-
|
| 3868 |
-
|
| 3869 |
-
|
| 3870 |
-
-
|
| 3871 |
-
|
| 3872 |
-
|
| 3873 |
-
|
| 3874 |
-
|
| 3875 |
-
|
| 3876 |
-
|
| 3877 |
-
|
| 3878 |
-
|
| 3879 |
-
|
| 3880 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3881 |
- an expression that would exceed the implementation-defined limits (see
|
| 3882 |
-
|
| 3883 |
- an operation that would have undefined behavior as specified in
|
| 3884 |
-
|
| 3885 |
-
|
| 3886 |
-
[[expr]]
|
| 3887 |
-
|
| 3888 |
-
|
| 3889 |
-
-
|
| 3890 |
-
|
| 3891 |
-
- a non-volatile glvalue of integral or enumeration type that refers
|
| 3892 |
-
to a complete non-volatile const object with a preceding
|
| 3893 |
-
initialization, initialized with a constant expression, or
|
| 3894 |
-
- a non-volatile glvalue that refers to a subobject of a string
|
| 3895 |
-
literal ([[lex.string]]), or
|
| 3896 |
-
- a non-volatile glvalue that refers to a non-volatile object defined
|
| 3897 |
-
with `constexpr`, or that refers to a non-mutable subobject of such
|
| 3898 |
-
an object, or
|
| 3899 |
- a non-volatile glvalue of literal type that refers to a non-volatile
|
| 3900 |
-
object whose lifetime began within the evaluation of
|
| 3901 |
-
- an lvalue-to-rvalue conversion
|
| 3902 |
glvalue that refers to a non-active member of a union or a subobject
|
| 3903 |
thereof;
|
|
|
|
|
|
|
| 3904 |
- an invocation of an implicitly-defined copy/move constructor or
|
| 3905 |
copy/move assignment operator for a union whose active member (if any)
|
| 3906 |
is mutable, unless the lifetime of the union object began within the
|
| 3907 |
-
evaluation of
|
| 3908 |
-
- an assignment expression ([[expr.ass]]) or invocation of an
|
| 3909 |
-
assignment operator ([[class.copy]]) that would change the active
|
| 3910 |
-
member of a union;
|
| 3911 |
- an *id-expression* that refers to a variable or data member of
|
| 3912 |
reference type unless the reference has a preceding initialization and
|
| 3913 |
either
|
| 3914 |
-
- it is
|
| 3915 |
-
- its lifetime began within the evaluation of
|
| 3916 |
- in a *lambda-expression*, a reference to `this` or to a variable with
|
| 3917 |
automatic storage duration defined outside that *lambda-expression*,
|
| 3918 |
where the reference would be an odr-use ([[basic.def.odr]],
|
| 3919 |
[[expr.prim.lambda]]);
|
| 3920 |
\[*Example 1*:
|
| 3921 |
``` cpp
|
| 3922 |
void g() {
|
| 3923 |
const int n = 0;
|
| 3924 |
[=] {
|
| 3925 |
-
constexpr int i = n;
|
| 3926 |
-
constexpr int j = *&n;
|
| 3927 |
};
|
| 3928 |
}
|
| 3929 |
```
|
| 3930 |
|
| 3931 |
— *end example*]
|
| 3932 |
-
\[*Note
|
| 3933 |
If the odr-use occurs in an invocation of a function call operator of
|
| 3934 |
a closure type, it no longer refers to `this` or to an enclosing
|
| 3935 |
-
automatic variable due to the transformation
|
| 3936 |
-
[[expr.prim.lambda.capture]]
|
| 3937 |
the corresponding data member.
|
| 3938 |
\[*Example 2*:
|
| 3939 |
``` cpp
|
| 3940 |
auto monad = [](auto v) { return [=] { return v; }; };
|
| 3941 |
auto bind = [](auto m) {
|
| 3942 |
return [=](auto fvm) { return fvm(m()); };
|
| 3943 |
};
|
| 3944 |
|
| 3945 |
-
// OK to
|
| 3946 |
static_assert(bind(monad(2))(monad)() == monad(2)());
|
| 3947 |
```
|
| 3948 |
|
| 3949 |
— *end example*]
|
| 3950 |
— *end note*]
|
| 3951 |
- a conversion from type cv `void*` to a pointer-to-object type;
|
| 3952 |
-
- a
|
| 3953 |
-
- a
|
| 3954 |
-
- a pseudo-destructor call ([[expr.pseudo]]);
|
| 3955 |
-
- modification of an object ([[expr.ass]], [[expr.post.incr]],
|
| 3956 |
[[expr.pre.incr]]) unless it is applied to a non-volatile lvalue of
|
| 3957 |
literal type that refers to a non-volatile object whose lifetime began
|
| 3958 |
-
within the evaluation of
|
| 3959 |
-
- a
|
| 3960 |
-
|
| 3961 |
-
|
| 3962 |
-
|
| 3963 |
-
- a
|
| 3964 |
-
the
|
| 3965 |
-
- a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3966 |
|
| 3967 |
-
If
|
| 3968 |
-
evaluation of
|
| 3969 |
-
|
| 3970 |
-
|
| 3971 |
-
constant expression.
|
| 3972 |
|
| 3973 |
[*Example 3*:
|
| 3974 |
|
| 3975 |
``` cpp
|
| 3976 |
int x; // not constant
|
|
@@ -4010,47 +5448,101 @@ constexpr int y = h(1); // OK: initializes y with the value 2
|
|
| 4010 |
// the lifetime of k begins inside h(1)
|
| 4011 |
```
|
| 4012 |
|
| 4013 |
— *end example*]
|
| 4014 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4015 |
An *integral constant expression* is an expression of integral or
|
| 4016 |
unscoped enumeration type, implicitly converted to a prvalue, where the
|
| 4017 |
converted expression is a core constant expression.
|
| 4018 |
|
| 4019 |
-
[*Note
|
| 4020 |
-
[[class.bit]]
|
| 4021 |
-
fixed
|
| 4022 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4023 |
|
| 4024 |
A *converted constant expression* of type `T` is an expression,
|
| 4025 |
implicitly converted to type `T`, where the converted expression is a
|
| 4026 |
constant expression and the implicit conversion sequence contains only
|
| 4027 |
|
| 4028 |
- user-defined conversions,
|
| 4029 |
-
- lvalue-to-rvalue conversions
|
| 4030 |
-
- array-to-pointer conversions
|
| 4031 |
-
- function-to-pointer conversions
|
| 4032 |
-
- qualification conversions
|
| 4033 |
-
- integral promotions
|
| 4034 |
-
- integral conversions
|
| 4035 |
-
conversions
|
| 4036 |
-
- null pointer conversions
|
| 4037 |
-
- null member pointer conversions
|
| 4038 |
and
|
| 4039 |
-
- function pointer conversions
|
| 4040 |
|
| 4041 |
and where the reference binding (if any) binds directly.
|
| 4042 |
|
| 4043 |
-
[*Note
|
| 4044 |
-
[[expr.new]]
|
| 4045 |
-
initializers if the underlying type is fixed
|
| 4046 |
-
bounds
|
| 4047 |
-
[[temp.arg]]
|
| 4048 |
|
| 4049 |
A *contextually converted constant expression of type `bool`* is an
|
| 4050 |
-
expression, contextually converted to `bool`
|
| 4051 |
-
|
| 4052 |
sequence contains only the conversions above.
|
| 4053 |
|
| 4054 |
A *constant expression* is either a glvalue core constant expression
|
| 4055 |
that refers to an entity that is a permitted result of a constant
|
| 4056 |
expression (as defined below), or a prvalue core constant expression
|
|
@@ -4059,29 +5551,44 @@ whose value satisfies the following constraints:
|
|
| 4059 |
- if the value is an object of class type, each non-static data member
|
| 4060 |
of reference type refers to an entity that is a permitted result of a
|
| 4061 |
constant expression,
|
| 4062 |
- if the value is of pointer type, it contains the address of an object
|
| 4063 |
with static storage duration, the address past the end of such an
|
| 4064 |
-
object
|
| 4065 |
-
value,
|
|
|
|
|
|
|
| 4066 |
- if the value is an object of class or array type, each subobject
|
| 4067 |
satisfies these constraints for the value.
|
| 4068 |
|
| 4069 |
An entity is a *permitted result of a constant expression* if it is an
|
| 4070 |
-
object with static storage duration that
|
| 4071 |
object or is a temporary object whose value satisfies the above
|
| 4072 |
-
constraints, or it is a function.
|
| 4073 |
|
| 4074 |
-
[*
|
| 4075 |
|
| 4076 |
-
|
| 4077 |
-
|
| 4078 |
-
|
| 4079 |
-
|
| 4080 |
-
|
|
|
|
|
|
|
|
|
|
| 4081 |
|
| 4082 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4083 |
|
| 4084 |
``` cpp
|
| 4085 |
bool f() {
|
| 4086 |
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
|
| 4087 |
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
|
|
@@ -4093,48 +5600,88 @@ It is unspecified whether the value of `f()` will be `true` or `false`.
|
|
| 4093 |
|
| 4094 |
— *end example*]
|
| 4095 |
|
| 4096 |
— *end note*]
|
| 4097 |
|
| 4098 |
-
|
| 4099 |
-
|
| 4100 |
-
|
| 4101 |
-
|
| 4102 |
-
|
|
|
|
|
|
|
| 4103 |
|
| 4104 |
-
|
| 4105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4106 |
``` cpp
|
| 4107 |
-
struct
|
| 4108 |
-
|
| 4109 |
-
|
| 4110 |
-
|
| 4111 |
-
|
| 4112 |
-
|
| 4113 |
-
|
| 4114 |
-
|
| 4115 |
-
|
| 4116 |
-
|
| 4117 |
-
int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4118 |
```
|
| 4119 |
|
| 4120 |
— *end example*]
|
| 4121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4122 |
<!-- Link reference definitions -->
|
| 4123 |
-
[
|
| 4124 |
-
[bad.
|
| 4125 |
-
[bad.
|
|
|
|
| 4126 |
[basic.align]: basic.md#basic.align
|
| 4127 |
[basic.compound]: basic.md#basic.compound
|
| 4128 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 4129 |
[basic.fundamental]: basic.md#basic.fundamental
|
|
|
|
| 4130 |
[basic.life]: basic.md#basic.life
|
| 4131 |
[basic.lookup]: basic.md#basic.lookup
|
| 4132 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 4133 |
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 4134 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 4135 |
-
[basic.lval]:
|
| 4136 |
[basic.namespace]: dcl.md#basic.namespace
|
| 4137 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 4138 |
[basic.scope.class]: basic.md#basic.scope.class
|
| 4139 |
[basic.start.main]: basic.md#basic.start.main
|
| 4140 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
|
@@ -4145,87 +5692,111 @@ int ary[a]; // error: ambiguous conversion
|
|
| 4145 |
[basic.types]: basic.md#basic.types
|
| 4146 |
[class]: class.md#class
|
| 4147 |
[class.abstract]: class.md#class.abstract
|
| 4148 |
[class.access]: class.md#class.access
|
| 4149 |
[class.access.base]: class.md#class.access.base
|
| 4150 |
-
[class.base.init]:
|
| 4151 |
[class.bit]: class.md#class.bit
|
| 4152 |
-
[class.cdtor]:
|
| 4153 |
-
[class.conv]:
|
| 4154 |
-
[class.conv.fct]:
|
| 4155 |
-
[class.copy]:
|
| 4156 |
-
[class.ctor]:
|
|
|
|
|
|
|
| 4157 |
[class.derived]: class.md#class.derived
|
| 4158 |
-
[class.dtor]:
|
| 4159 |
-
[class.free]:
|
| 4160 |
[class.friend]: class.md#class.friend
|
| 4161 |
-
[class.init]: special.md#class.init
|
| 4162 |
[class.mem]: class.md#class.mem
|
| 4163 |
[class.member.lookup]: class.md#class.member.lookup
|
| 4164 |
[class.mfct]: class.md#class.mfct
|
| 4165 |
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 4166 |
-
[class.
|
|
|
|
| 4167 |
[class.qual]: basic.md#class.qual
|
|
|
|
| 4168 |
[class.static]: class.md#class.static
|
| 4169 |
-
[class.temporary]:
|
| 4170 |
[class.this]: class.md#class.this
|
| 4171 |
[class.union]: class.md#class.union
|
| 4172 |
[class.virtual]: class.md#class.virtual
|
| 4173 |
-
[
|
| 4174 |
-
[conv
|
| 4175 |
-
[conv.
|
| 4176 |
-
[conv.
|
| 4177 |
-
[conv.
|
| 4178 |
-
[conv.
|
| 4179 |
-
[conv.
|
| 4180 |
-
[conv.
|
| 4181 |
-
[conv.
|
| 4182 |
-
[conv.
|
| 4183 |
-
[conv.
|
| 4184 |
-
[conv.
|
| 4185 |
-
[conv.
|
| 4186 |
-
[conv.
|
|
|
|
|
|
|
|
|
|
| 4187 |
[cpp]: cpp.md#cpp
|
| 4188 |
-
[
|
|
|
|
| 4189 |
[dcl.align]: dcl.md#dcl.align
|
| 4190 |
[dcl.array]: dcl.md#dcl.array
|
|
|
|
| 4191 |
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 4192 |
[dcl.dcl]: dcl.md#dcl.dcl
|
|
|
|
| 4193 |
[dcl.enum]: dcl.md#dcl.enum
|
| 4194 |
[dcl.fct]: dcl.md#dcl.fct
|
| 4195 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
|
|
|
| 4196 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 4197 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 4198 |
[dcl.init]: dcl.md#dcl.init
|
| 4199 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 4200 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 4201 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
|
|
|
| 4202 |
[dcl.link]: dcl.md#dcl.link
|
|
|
|
| 4203 |
[dcl.name]: dcl.md#dcl.name
|
|
|
|
| 4204 |
[dcl.ref]: dcl.md#dcl.ref
|
| 4205 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 4206 |
[dcl.stc]: dcl.md#dcl.stc
|
| 4207 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 4208 |
[dcl.type]: dcl.md#dcl.type
|
| 4209 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 4210 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4211 |
[except]: except.md#except
|
| 4212 |
[except.handle]: except.md#except.handle
|
|
|
|
| 4213 |
[except.spec]: except.md#except.spec
|
| 4214 |
[except.terminate]: except.md#except.terminate
|
| 4215 |
[except.throw]: except.md#except.throw
|
| 4216 |
[expr]: #expr
|
| 4217 |
[expr.add]: #expr.add
|
| 4218 |
[expr.alignof]: #expr.alignof
|
|
|
|
| 4219 |
[expr.ass]: #expr.ass
|
|
|
|
| 4220 |
[expr.bit.and]: #expr.bit.and
|
| 4221 |
[expr.call]: #expr.call
|
| 4222 |
[expr.cast]: #expr.cast
|
| 4223 |
[expr.comma]: #expr.comma
|
|
|
|
| 4224 |
[expr.cond]: #expr.cond
|
| 4225 |
[expr.const]: #expr.const
|
| 4226 |
[expr.const.cast]: #expr.const.cast
|
|
|
|
| 4227 |
[expr.delete]: #expr.delete
|
| 4228 |
[expr.dynamic.cast]: #expr.dynamic.cast
|
| 4229 |
[expr.eq]: #expr.eq
|
| 4230 |
[expr.log.and]: #expr.log.and
|
| 4231 |
[expr.log.or]: #expr.log.or
|
|
@@ -4233,51 +5804,64 @@ int ary[a]; // error: ambiguous conversion
|
|
| 4233 |
[expr.mul]: #expr.mul
|
| 4234 |
[expr.new]: #expr.new
|
| 4235 |
[expr.or]: #expr.or
|
| 4236 |
[expr.post]: #expr.post
|
| 4237 |
[expr.post.incr]: #expr.post.incr
|
|
|
|
| 4238 |
[expr.pre.incr]: #expr.pre.incr
|
| 4239 |
[expr.prim]: #expr.prim
|
| 4240 |
[expr.prim.fold]: #expr.prim.fold
|
| 4241 |
[expr.prim.id]: #expr.prim.id
|
|
|
|
| 4242 |
[expr.prim.id.qual]: #expr.prim.id.qual
|
| 4243 |
[expr.prim.id.unqual]: #expr.prim.id.unqual
|
| 4244 |
[expr.prim.lambda]: #expr.prim.lambda
|
| 4245 |
[expr.prim.lambda.capture]: #expr.prim.lambda.capture
|
| 4246 |
[expr.prim.lambda.closure]: #expr.prim.lambda.closure
|
| 4247 |
[expr.prim.literal]: #expr.prim.literal
|
| 4248 |
[expr.prim.paren]: #expr.prim.paren
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4249 |
[expr.prim.this]: #expr.prim.this
|
| 4250 |
-
[expr.
|
| 4251 |
[expr.ref]: #expr.ref
|
| 4252 |
[expr.reinterpret.cast]: #expr.reinterpret.cast
|
| 4253 |
[expr.rel]: #expr.rel
|
| 4254 |
[expr.shift]: #expr.shift
|
| 4255 |
[expr.sizeof]: #expr.sizeof
|
|
|
|
| 4256 |
[expr.static.cast]: #expr.static.cast
|
| 4257 |
[expr.sub]: #expr.sub
|
| 4258 |
[expr.throw]: #expr.throw
|
|
|
|
| 4259 |
[expr.type.conv]: #expr.type.conv
|
| 4260 |
[expr.typeid]: #expr.typeid
|
| 4261 |
[expr.unary]: #expr.unary
|
| 4262 |
[expr.unary.noexcept]: #expr.unary.noexcept
|
| 4263 |
[expr.unary.op]: #expr.unary.op
|
| 4264 |
[expr.xor]: #expr.xor
|
|
|
|
| 4265 |
[function.objects]: utilities.md#function.objects
|
| 4266 |
[implimits]: limits.md#implimits
|
| 4267 |
[intro]: intro.md#intro
|
| 4268 |
-
[intro.execution]:
|
| 4269 |
-
[intro.memory]:
|
| 4270 |
-
[intro.object]:
|
|
|
|
|
|
|
| 4271 |
[lex.literal]: lex.md#lex.literal
|
| 4272 |
[lex.string]: lex.md#lex.string
|
| 4273 |
[library]: library.md#library
|
|
|
|
| 4274 |
[namespace.qual]: basic.md#namespace.qual
|
| 4275 |
-
[new.badlength]:
|
| 4276 |
-
[new.delete.array]:
|
| 4277 |
-
[new.delete.placement]:
|
| 4278 |
-
[new.delete.single]:
|
| 4279 |
[over]: over.md#over
|
| 4280 |
[over.ass]: over.md#over.ass
|
| 4281 |
[over.best.ics]: over.md#over.best.ics
|
| 4282 |
[over.built]: over.md#over.built
|
| 4283 |
[over.call]: over.md#over.call
|
|
@@ -4288,121 +5872,162 @@ int ary[a]; // error: ambiguous conversion
|
|
| 4288 |
[over.match.oper]: over.md#over.match.oper
|
| 4289 |
[over.match.viable]: over.md#over.match.viable
|
| 4290 |
[over.oper]: over.md#over.oper
|
| 4291 |
[over.over]: over.md#over.over
|
| 4292 |
[replacement.functions]: library.md#replacement.functions
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4293 |
[stmt.return]: stmt.md#stmt.return
|
| 4294 |
[stmt.switch]: stmt.md#stmt.switch
|
| 4295 |
-
[support.runtime]:
|
| 4296 |
-
[support.types]:
|
| 4297 |
-
[support.types.layout]: language.md#support.types.layout
|
| 4298 |
[temp.arg]: temp.md#temp.arg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4299 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 4300 |
[temp.explicit]: temp.md#temp.explicit
|
| 4301 |
-
[temp.mem]: temp.md#temp.mem
|
| 4302 |
[temp.names]: temp.md#temp.names
|
|
|
|
|
|
|
| 4303 |
[temp.res]: temp.md#temp.res
|
| 4304 |
[temp.variadic]: temp.md#temp.variadic
|
| 4305 |
[thread]: thread.md#thread
|
| 4306 |
-
[type.info]:
|
| 4307 |
|
| 4308 |
[^1]: The precedence of operators is not directly specified, but it can
|
| 4309 |
be derived from the syntax.
|
| 4310 |
|
| 4311 |
-
[^2]:
|
| 4312 |
-
|
| 4313 |
-
type.
|
| 4314 |
|
| 4315 |
[^3]: The cast and assignment operators must still perform their
|
| 4316 |
-
specific conversions as described in [[expr.
|
| 4317 |
-
[[expr.static.cast]] and [[expr.ass]].
|
| 4318 |
|
| 4319 |
-
[^4]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4320 |
`(*this)` ([[class.mfct.non-static]]).
|
| 4321 |
|
| 4322 |
-
[^
|
| 4323 |
following common idiom: `&x[0]`.
|
| 4324 |
|
| 4325 |
-
[^
|
| 4326 |
subexpression evaluation happens even if the result is unnecessary
|
| 4327 |
to determine the value of the entire postfix expression, for example
|
| 4328 |
if the *id-expression* denotes a static member.
|
| 4329 |
|
| 4330 |
-
[^
|
| 4331 |
|
| 4332 |
-
[^
|
| 4333 |
by `v` can contain other `B` objects as base classes, but these are
|
| 4334 |
ignored.
|
| 4335 |
|
| 4336 |
-
[^
|
| 4337 |
|
| 4338 |
-
[^
|
| 4339 |
`*(p)`, `((*p))`, `*((p))`, and so on all meet this requirement.
|
| 4340 |
|
| 4341 |
-
[^
|
| 4342 |
-
function types) are never cv-qualified; see [[dcl.fct]].
|
| 4343 |
-
|
| 4344 |
-
[^12]: The types may have different cv-qualifiers, subject to the
|
| 4345 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 4346 |
constness.
|
| 4347 |
|
| 4348 |
-
[^
|
| 4349 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 4350 |
constness.
|
| 4351 |
|
| 4352 |
-
[^
|
|
|
|
| 4353 |
|
| 4354 |
-
[^
|
| 4355 |
|
| 4356 |
is not limited to conversions that cast away a const-qualifier.
|
| 4357 |
|
| 4358 |
-
[^
|
| 4359 |
|
| 4360 |
-
[^
|
| 4361 |
-
result of applying `sizeof` to the subobject, due to
|
| 4362 |
-
classes and less strict padding requirements on
|
| 4363 |
-
subobjects.
|
| 4364 |
|
| 4365 |
-
[^
|
| 4366 |
second standard conversion converts to the unsigned type
|
| 4367 |
`std::size_t` and thus thwarts any attempt to detect a negative
|
| 4368 |
value afterwards.
|
| 4369 |
|
| 4370 |
-
[^
|
| 4371 |
constructor.
|
| 4372 |
|
| 4373 |
-
[^
|
| 4374 |
-
empty square brackets can follow the `delete` keyword if the
|
| 4375 |
-
expression is enclosed in parentheses.
|
| 4376 |
|
| 4377 |
-
[^
|
| 4378 |
type `void*` because `void` is not an object type.
|
| 4379 |
|
| 4380 |
-
[^
|
| 4381 |
first element of the array created by that *new-expression*.
|
| 4382 |
Zero-length arrays do not have a first element.
|
| 4383 |
|
| 4384 |
-
[^
|
| 4385 |
-
different from the dynamic type, and the destructor is not virtual,
|
| 4386 |
-
the size might be incorrect, but that case is already undefined, as
|
| 4387 |
-
stated above.
|
| 4388 |
|
| 4389 |
-
[^
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4390 |
|
| 4391 |
-
[^
|
| 4392 |
-
a single-element array for
|
| 4393 |
-
pointer past the last element of an array
|
| 4394 |
-
considered to be equivalent to a pointer to a
|
| 4395 |
-
|
| 4396 |
|
| 4397 |
-
[^
|
| 4398 |
-
a single-element array for
|
| 4399 |
-
|
| 4400 |
-
considered to be equivalent to a pointer to a hypothetical element
|
| 4401 |
-
x[n] for this purpose; see [[basic.compound]].
|
| 4402 |
|
| 4403 |
-
[^
|
| 4404 |
-
|
| 4405 |
-
|
| 4406 |
-
[^28]: Nonetheless, implementations are encouraged to provide consistent
|
| 4407 |
-
results, irrespective of whether the evaluation was performed during
|
| 4408 |
translation and/or during program execution.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Expressions <a id="expr">[[expr]]</a>
|
| 2 |
|
| 3 |
+
## Preamble <a id="expr.pre">[[expr.pre]]</a>
|
| 4 |
+
|
| 5 |
+
[*Note 1*: [[expr]] defines the syntax, order of evaluation, and
|
| 6 |
+
meaning of expressions.[^1] An expression is a sequence of operators and
|
| 7 |
+
operands that specifies a computation. An expression can result in a
|
| 8 |
value and can cause side effects. — *end note*]
|
| 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
|
| 14 |
+
rules for syntax and evaluation order specified in [[expr.compound]],
|
| 15 |
but the requirements of operand type and value category are replaced by
|
| 16 |
the rules for function call. Relations between operators, such as `++a`
|
| 17 |
+
meaning `a+=1`, are not guaranteed for overloaded operators
|
| 18 |
+
[[over.oper]]. — *end note*]
|
| 19 |
|
| 20 |
+
Subclause [[expr.compound]] defines the effects of operators when
|
| 21 |
+
applied to types for which they have not been overloaded. Operator
|
| 22 |
+
overloading shall not modify the rules for the *built-in operators*,
|
| 23 |
+
that is, for operators applied to types for which they are defined by
|
| 24 |
+
this Standard. However, these built-in operators participate in overload
|
| 25 |
+
resolution, and as part of that process user-defined conversions will be
|
| 26 |
+
considered where necessary to convert the operands to types appropriate
|
| 27 |
+
for the built-in operator. If a built-in operator is selected, such
|
| 28 |
+
conversions will be applied to the operands before the operation is
|
| 29 |
+
considered further according to the rules in subclause
|
| 30 |
+
[[expr.compound]]; see [[over.match.oper]], [[over.built]].
|
| 31 |
|
| 32 |
If during the evaluation of an expression, the result is not
|
| 33 |
mathematically defined or not in the range of representable values for
|
| 34 |
its type, the behavior is undefined.
|
| 35 |
|
| 36 |
[*Note 3*: Treatment of division by zero, forming a remainder using a
|
| 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 may regroup operators according to the usual
|
| 43 |
+
mathematical rules only where the operators really are associative or
|
| 44 |
+
commutative.[^2] For example, in the following fragment
|
| 45 |
|
| 46 |
+
``` cpp
|
| 47 |
+
int a, b;
|
| 48 |
+
...
|
| 49 |
+
a = a + 32760 + b + 5;
|
| 50 |
+
```
|
| 51 |
|
| 52 |
+
the expression statement behaves exactly the same as
|
| 53 |
+
|
| 54 |
+
``` cpp
|
| 55 |
+
a = (((a + 32760) + b) + 5);
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
due to the associativity and precedence of these operators. Thus, the
|
| 59 |
+
result of the sum `(a + 32760)` is next added to `b`, and that result is
|
| 60 |
+
then added to 5 which results in the value assigned to `a`. On a machine
|
| 61 |
+
in which overflows produce an exception and in which the range of values
|
| 62 |
+
representable by an `int` is \[`-32768`, `+32767`\], the implementation
|
| 63 |
+
cannot rewrite this expression as
|
| 64 |
+
|
| 65 |
+
``` cpp
|
| 66 |
+
a = ((a + b) + 32765);
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
since if the values for `a` and `b` were, respectively, -32754 and -15,
|
| 70 |
+
the sum `a + b` would produce an exception while the original expression
|
| 71 |
+
would not; nor can the expression be rewritten as either
|
| 72 |
+
|
| 73 |
+
``` cpp
|
| 74 |
+
a = ((a + 32765) + b);
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
or
|
| 78 |
+
|
| 79 |
+
``` cpp
|
| 80 |
+
a = (a + (b + 32765));
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
since the values for `a` and `b` might have been, respectively, 4 and -8
|
| 84 |
+
or -17 and 12. However on a machine in which overflows do not produce an
|
| 85 |
+
exception and in which the results of overflows are reversible, the
|
| 86 |
+
above expression statement can be rewritten by the implementation in any
|
| 87 |
+
of the above ways because the same result will occur.
|
| 88 |
+
|
| 89 |
+
— *end note*]
|
| 90 |
+
|
| 91 |
+
The values of the floating-point operands and the results of
|
| 92 |
+
floating-point expressions may be represented in greater precision and
|
| 93 |
+
range than that required by the type; the types are not
|
| 94 |
+
changed thereby.[^3]
|
| 95 |
+
|
| 96 |
+
## Properties of expressions <a id="expr.prop">[[expr.prop]]</a>
|
| 97 |
+
|
| 98 |
+
### Value category <a id="basic.lval">[[basic.lval]]</a>
|
| 99 |
+
|
| 100 |
+
Expressions are categorized according to the taxonomy in Figure
|
| 101 |
+
[[fig:basic.lval]].
|
| 102 |
+
|
| 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, bit-field, or function.
|
| 109 |
+
- A *prvalue* is an expression whose evaluation initializes an object or
|
| 110 |
+
a bit-field, or computes the value of an operand of an operator, as
|
| 111 |
+
specified by the context in which it appears, or an expression that
|
| 112 |
+
has type cv `void`.
|
| 113 |
+
- An *xvalue* is a glvalue that denotes an object or bit-field whose
|
| 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
|
| 121 |
+
property of an expression is called its *value category*.
|
| 122 |
+
|
| 123 |
+
[*Note 1*: The discussion of each built-in operator in
|
| 124 |
+
[[expr.compound]] indicates the category of the value it yields and the
|
| 125 |
+
value categories of the operands it expects. For example, the built-in
|
| 126 |
+
assignment operators expect that the left operand is an lvalue and that
|
| 127 |
+
the right operand is a prvalue and yield an lvalue as the result.
|
| 128 |
+
User-defined operators are functions, and the categories of values they
|
| 129 |
+
expect and yield are determined by their parameter and return
|
| 130 |
+
types. — *end note*]
|
| 131 |
+
|
| 132 |
+
[*Note 2*: Historically, lvalues and rvalues were so-called because
|
| 133 |
+
they could appear on the left- and right-hand side of an assignment
|
| 134 |
+
(although this is no longer generally true); glvalues are “generalized”
|
| 135 |
+
lvalues, prvalues are “pure” rvalues, and xvalues are “eXpiring”
|
| 136 |
+
lvalues. Despite their names, these terms classify expressions, not
|
| 137 |
+
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 ([[expr.type.conv]],
|
| 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
|
| 153 |
+
xvalue and the second operand is a pointer to data member
|
| 154 |
+
[[expr.mptr.oper]].
|
| 155 |
|
| 156 |
In general, the effect of this rule is that named rvalue references are
|
| 157 |
treated as lvalues and unnamed rvalue references to objects are treated
|
| 158 |
as xvalues; rvalue references to functions are treated as lvalues
|
| 159 |
whether named or not.
|
|
|
|
| 176 |
The expressions `f()`, `f().m`, `static_cast<A&&>(a)`, and `a + a` are
|
| 177 |
xvalues. The expression `ar` is an lvalue.
|
| 178 |
|
| 179 |
— *end example*]
|
| 180 |
|
| 181 |
+
The *result* of a glvalue is the entity denoted by the expression. The
|
| 182 |
+
*result* of a prvalue is the value that the expression stores into its
|
| 183 |
+
context; a prvalue that has type cv `void` has no result. A prvalue
|
| 184 |
+
whose result is the value *V* is sometimes said to have or name the
|
| 185 |
+
value *V*. The *result object* of a prvalue is the object initialized by
|
| 186 |
+
the prvalue; a non-discarded prvalue that is used to compute the value
|
| 187 |
+
of an operand of a built-in operator or a prvalue that has type
|
| 188 |
+
cv `void` has no result object.
|
| 189 |
|
| 190 |
+
[*Note 4*: Except when the prvalue is the operand of a
|
| 191 |
+
*decltype-specifier*, a prvalue of class or array type always has a
|
| 192 |
+
result object. For a discarded prvalue that has type other than
|
| 193 |
+
cv `void`, a temporary object is materialized; see
|
| 194 |
+
[[expr.context]]. — *end note*]
|
| 195 |
|
| 196 |
+
Whenever a glvalue appears as an operand of an operator that expects a
|
| 197 |
+
prvalue for that operand, the lvalue-to-rvalue [[conv.lval]],
|
| 198 |
+
array-to-pointer [[conv.array]], or function-to-pointer [[conv.func]]
|
| 199 |
+
standard conversions are applied to convert the expression to a prvalue.
|
|
|
|
| 200 |
|
| 201 |
+
[*Note 5*: An attempt to bind an rvalue reference to an lvalue is not
|
| 202 |
+
such a context; see [[dcl.init.ref]]. — *end note*]
|
| 203 |
+
|
| 204 |
+
[*Note 6*: Because cv-qualifiers are removed from the type of an
|
| 205 |
expression of non-class type when the expression is converted to a
|
| 206 |
+
prvalue, an lvalue of type `const int` can, for example, be used where a
|
| 207 |
+
prvalue of type `int` is required. — *end note*]
|
| 208 |
+
|
| 209 |
+
[*Note 7*: There are no prvalue bit-fields; if a bit-field is converted
|
| 210 |
+
to a prvalue [[conv.lval]], a prvalue of the type of the bit-field is
|
| 211 |
+
created, which might then be promoted [[conv.prom]]. — *end note*]
|
| 212 |
+
|
| 213 |
+
Whenever a prvalue appears as an operand of an operator that expects a
|
| 214 |
+
glvalue for that operand, the temporary materialization conversion
|
| 215 |
+
[[conv.rval]] is applied to convert the expression to an xvalue.
|
| 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.simple]], a prvalue shall always
|
| 222 |
+
have complete type or the `void` type; if it has a class type or
|
| 223 |
+
(possibly multi-dimensional) array of class type, that class shall not
|
| 224 |
+
be an abstract class [[class.abstract]]. A glvalue shall not have type
|
| 225 |
+
cv `void`.
|
| 226 |
+
|
| 227 |
+
[*Note 8*: A glvalue may have complete or incomplete non-`void` type.
|
| 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 ([[expr.ass]],
|
| 236 |
+
[[expr.post.incr]], [[expr.pre.incr]]). — *end note*]
|
| 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 |
+
|
| 242 |
+
- the dynamic type of the object,
|
| 243 |
+
- a type that is the signed or unsigned type corresponding to the
|
| 244 |
+
dynamic type of the object, or
|
| 245 |
+
- a `char`, `unsigned char`, or `std::byte` type.
|
| 246 |
+
|
| 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*: Unlike in C, C++ has no accesses of class
|
| 253 |
+
type. — *end note*]
|
| 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]]), the type is adjusted to `T` prior to any
|
| 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
|
| 264 |
+
has ended, the behavior is undefined (see
|
| 265 |
+
[[basic.life]]). — *end note*]
|
| 266 |
+
|
| 267 |
+
If a prvalue initially has the type “cv `T`”, where `T` is a
|
| 268 |
+
cv-unqualified non-class, non-array type, the type of the expression is
|
| 269 |
+
adjusted to `T` prior to any further analysis.
|
| 270 |
+
|
| 271 |
+
The *composite pointer type* of two operands `p1` and `p2` having types
|
| 272 |
+
`T1` and `T2`, respectively, where at least one is a pointer or
|
| 273 |
+
pointer-to-member type or `std::nullptr_t`, is:
|
| 274 |
+
|
| 275 |
+
- if both `p1` and `p2` are null pointer constants, `std::nullptr_t`;
|
| 276 |
+
- if either `p1` or `p2` is a null pointer constant, `T2` or `T1`,
|
| 277 |
+
respectively;
|
| 278 |
+
- if `T1` or `T2` is “pointer to *cv1* `void`” and the other type is
|
| 279 |
+
“pointer to *cv2* `T`”, where `T` is an object type or `void`,
|
| 280 |
+
“pointer to *cv12* `void`”, where *cv12* is the union of *cv1* and
|
| 281 |
+
*cv2*;
|
| 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 cv-combined type
|
| 288 |
+
[[conv.qual]] of `T1` and `T2` or the cv-combined type of `T2` and
|
| 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 cv-combined type of
|
| 300 |
+
`T2` and `T1` or the cv-combined type of `T1` and `T2`, respectively;
|
| 301 |
+
- if `T1` and `T2` are similar types [[conv.qual]], the cv-combined type
|
| 302 |
+
of `T1` and `T2`;
|
| 303 |
+
- otherwise, a program that necessitates the determination of a
|
| 304 |
+
composite pointer type is ill-formed.
|
| 305 |
+
|
| 306 |
+
[*Example 1*:
|
| 307 |
+
|
| 308 |
+
``` cpp
|
| 309 |
+
typedef void *p;
|
| 310 |
+
typedef const int *q;
|
| 311 |
+
typedef int **pi;
|
| 312 |
+
typedef const int **pci;
|
| 313 |
+
```
|
| 314 |
+
|
| 315 |
+
The composite pointer type of `p` and `q` is “pointer to `const void`”;
|
| 316 |
+
the composite pointer type of `pi` and `pci` is “pointer to `const`
|
| 317 |
+
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 ([[expr.prim.req]],
|
| 324 |
+
[[expr.typeid]], [[expr.sizeof]], [[expr.unary.noexcept]],
|
| 325 |
+
[[dcl.type.simple]], [[temp.pre]], [[temp.concept]]). An unevaluated
|
| 326 |
+
operand is not evaluated.
|
| 327 |
+
|
| 328 |
+
[*Note 1*: In an unevaluated operand, a non-static class member may be
|
| 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 |
+
|
| 334 |
+
In some contexts, an expression only appears for its side effects. Such
|
| 335 |
+
an expression is called a *discarded-value expression*. The
|
| 336 |
+
array-to-pointer [[conv.array]] and function-to-pointer [[conv.func]]
|
| 337 |
+
standard conversions are not applied. The lvalue-to-rvalue conversion
|
| 338 |
+
[[conv.lval]] is applied if and only if the expression is a glvalue of
|
| 339 |
+
volatile-qualified type and it is one of the following:
|
| 340 |
+
|
| 341 |
+
- `(` *expression* `)`, where *expression* is one of these expressions,
|
| 342 |
+
- *id-expression* [[expr.prim.id]],
|
| 343 |
+
- subscripting [[expr.sub]],
|
| 344 |
+
- class member access [[expr.ref]],
|
| 345 |
+
- indirection [[expr.unary.op]],
|
| 346 |
+
- pointer-to-member operation [[expr.mptr.oper]],
|
| 347 |
+
- conditional expression [[expr.cond]] where both the second and the
|
| 348 |
+
third operands are one of these expressions, or
|
| 349 |
+
- comma expression [[expr.comma]] where the right operand is one of
|
| 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 |
+
If the (possibly converted) expression is a prvalue, the temporary
|
| 356 |
+
materialization conversion [[conv.rval]] is applied.
|
| 357 |
+
|
| 358 |
+
[*Note 3*: If the expression is an lvalue of class type, it must have a
|
| 359 |
+
volatile copy constructor to initialize the temporary object that is the
|
| 360 |
+
result object of the lvalue-to-rvalue conversion. — *end note*]
|
| 361 |
+
|
| 362 |
+
The glvalue expression is evaluated and its value is discarded.
|
| 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 |
+
|
| 371 |
+
- Zero or one conversion from the following set: lvalue-to-rvalue
|
| 372 |
+
conversion, array-to-pointer conversion, and function-to-pointer
|
| 373 |
+
conversion.
|
| 374 |
+
- Zero or one conversion from the following set: integral promotions,
|
| 375 |
+
floating-point promotion, integral conversions, floating-point
|
| 376 |
+
conversions, floating-integral conversions, pointer conversions,
|
| 377 |
+
pointer-to-member conversions, and boolean conversions.
|
| 378 |
+
- Zero or one function pointer conversion.
|
| 379 |
+
- Zero or one qualification conversion.
|
| 380 |
+
|
| 381 |
+
[*Note 1*: A standard conversion sequence can be empty, i.e., it can
|
| 382 |
+
consist of no conversions. — *end note*]
|
| 383 |
+
|
| 384 |
+
A standard conversion sequence will be applied to an expression if
|
| 385 |
+
necessary to convert it to a required destination type.
|
| 386 |
+
|
| 387 |
+
[*Note 2*:
|
| 388 |
+
|
| 389 |
+
Expressions with a given type will be implicitly converted to other
|
| 390 |
+
types in several contexts:
|
| 391 |
+
|
| 392 |
+
- When used as operands of operators. The operator’s requirements for
|
| 393 |
+
its operands dictate the destination type [[expr.compound]].
|
| 394 |
+
- When used in the condition of an `if` statement [[stmt.if]] or
|
| 395 |
+
iteration statement [[stmt.iter]]. The destination type is `bool`.
|
| 396 |
+
- When used in the expression of a `switch` statement [[stmt.switch]].
|
| 397 |
+
The destination type is integral.
|
| 398 |
+
- When used as the source expression for an initialization (which
|
| 399 |
+
includes use as an argument in a function call and use as the
|
| 400 |
+
expression in a `return` statement). The type of the entity being
|
| 401 |
+
initialized is (generally) the destination type. See [[dcl.init]],
|
| 402 |
+
[[dcl.init.ref]].
|
| 403 |
+
|
| 404 |
+
— *end note*]
|
| 405 |
+
|
| 406 |
+
An expression E can be *implicitly converted* to a type `T` if and only
|
| 407 |
+
if the declaration `T t=E;` is well-formed, for some invented temporary
|
| 408 |
+
variable `t` [[dcl.init]].
|
| 409 |
+
|
| 410 |
+
Certain language constructs require that an expression be converted to a
|
| 411 |
+
Boolean value. An expression E appearing in such a context is said to be
|
| 412 |
+
*contextually converted to `bool`* and is well-formed if and only if the
|
| 413 |
+
declaration `bool t(E);` is well-formed, for some invented temporary
|
| 414 |
+
variable `t` [[dcl.init]].
|
| 415 |
+
|
| 416 |
+
Certain language constructs require conversion to a value having one of
|
| 417 |
+
a specified set of types appropriate to the construct. An expression E
|
| 418 |
+
of class type `C` appearing in such a context is said to be
|
| 419 |
+
*contextually implicitly converted* to a specified type `T` and is
|
| 420 |
+
well-formed if and only if E can be implicitly converted to a type `T`
|
| 421 |
+
that is determined as follows: `C` is searched for non-explicit
|
| 422 |
+
conversion functions whose return type is cv `T` or reference to cv `T`
|
| 423 |
+
such that `T` is allowed by the context. There shall be exactly one such
|
| 424 |
+
`T`.
|
| 425 |
+
|
| 426 |
+
The effect of any implicit conversion is the same as performing the
|
| 427 |
+
corresponding declaration and initialization and then using the
|
| 428 |
+
temporary variable as the result of the conversion. The result is an
|
| 429 |
+
lvalue if `T` is an lvalue reference type or an rvalue reference to
|
| 430 |
+
function type [[dcl.ref]], an xvalue if `T` is an rvalue reference to
|
| 431 |
+
object type, and a prvalue otherwise. The expression E is used as a
|
| 432 |
+
glvalue if and only if the initialization uses it as a glvalue.
|
| 433 |
+
|
| 434 |
+
[*Note 3*: For class types, user-defined conversions are considered as
|
| 435 |
+
well; see [[class.conv]]. In general, an implicit conversion sequence
|
| 436 |
+
[[over.best.ics]] consists of a standard conversion sequence followed by
|
| 437 |
+
a user-defined conversion followed by another standard conversion
|
| 438 |
+
sequence. — *end note*]
|
| 439 |
+
|
| 440 |
+
[*Note 4*: There are some contexts where certain conversions are
|
| 441 |
+
suppressed. For example, the lvalue-to-rvalue conversion is not done on
|
| 442 |
+
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] If `T` is an incomplete type, a program that
|
| 449 |
+
necessitates this conversion is ill-formed. If `T` is a non-class type,
|
| 450 |
+
the type of the prvalue is the cv-unqualified version of `T`. Otherwise,
|
| 451 |
+
the type of the prvalue is `T`. [^6]
|
| 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
|
| 457 |
+
- the evaluation of E results in the evaluation of a member E_`x` of the
|
| 458 |
+
set of potential results of E, and E_`x` names a variable `x` that is
|
| 459 |
+
not odr-used by E_`x` [[basic.def.odr]],
|
| 460 |
+
|
| 461 |
+
the value contained in the referenced object is not accessed.
|
| 462 |
+
|
| 463 |
+
[*Example 1*:
|
| 464 |
+
|
| 465 |
+
``` cpp
|
| 466 |
+
struct S { int n; };
|
| 467 |
+
auto f() {
|
| 468 |
+
S x { 1 };
|
| 469 |
+
constexpr S y { 2 };
|
| 470 |
+
return [&](bool b) { return (b ? y : x).n; };
|
| 471 |
+
}
|
| 472 |
+
auto g = f();
|
| 473 |
+
int m = g(false); // undefined behavior: access of x.n outside its lifetime
|
| 474 |
+
int n = g(true); // OK, does not access y.n
|
| 475 |
+
```
|
| 476 |
+
|
| 477 |
+
— *end example*]
|
| 478 |
+
|
| 479 |
+
The result of the conversion is determined according to the following
|
| 480 |
+
rules:
|
| 481 |
+
|
| 482 |
+
- If `T` is cv `std::nullptr_t`, the result is a null pointer constant
|
| 483 |
+
[[conv.ptr]]. \[*Note 1*: Since the conversion does not access the
|
| 484 |
+
object to which the glvalue refers, there is no side effect even if
|
| 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 ([[basic.stc.dynamic.deallocation]],
|
| 491 |
+
[[basic.stc.dynamic.safety]]), the behavior is
|
| 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*]
|
| 498 |
+
|
| 499 |
+
### Array-to-pointer conversion <a id="conv.array">[[conv.array]]</a>
|
| 500 |
+
|
| 501 |
+
An lvalue or rvalue of type “array of `N` `T`” or “array of unknown
|
| 502 |
+
bound of `T`” can be converted to a prvalue of type “pointer to `T`”.
|
| 503 |
+
The temporary materialization conversion [[conv.rval]] is applied. The
|
| 504 |
+
result is a pointer to the first element of the array.
|
| 505 |
+
|
| 506 |
+
### Function-to-pointer conversion <a id="conv.func">[[conv.func]]</a>
|
| 507 |
+
|
| 508 |
+
An lvalue of function type `T` can be converted to a prvalue of type
|
| 509 |
+
“pointer to `T`”. The result is a pointer to the function.[^7]
|
| 510 |
+
|
| 511 |
+
### Temporary materialization conversion <a id="conv.rval">[[conv.rval]]</a>
|
| 512 |
+
|
| 513 |
+
A prvalue of type `T` can be converted to an xvalue of type `T`. This
|
| 514 |
+
conversion initializes a temporary object [[class.temporary]] of type
|
| 515 |
+
`T` from the prvalue by evaluating the prvalue with the temporary object
|
| 516 |
+
as its result object, and produces an xvalue denoting the temporary
|
| 517 |
+
object. `T` shall be a complete type.
|
| 518 |
+
|
| 519 |
+
[*Note 1*: If `T` is a class type (or array thereof), it must have an
|
| 520 |
+
accessible and non-deleted destructor; see
|
| 521 |
+
[[class.dtor]]. — *end note*]
|
| 522 |
+
|
| 523 |
+
[*Example 1*:
|
| 524 |
+
|
| 525 |
+
``` cpp
|
| 526 |
+
struct X { int n; };
|
| 527 |
+
int k = X().n; // OK, X() prvalue is converted to xvalue
|
| 528 |
+
```
|
| 529 |
+
|
| 530 |
+
— *end example*]
|
| 531 |
+
|
| 532 |
+
### Qualification conversions <a id="conv.qual">[[conv.qual]]</a>
|
| 533 |
+
|
| 534 |
+
A *cv-decomposition* of a type `T` is a sequence of cvᵢ and Pᵢ such that
|
| 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 cv-decompositions, taking `U` as “`int`”, as “pointer to
|
| 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 |
+
cv-decomposition of `T`, that is, cv₁, cv₂, …, cvₙ, is called the
|
| 550 |
+
*cv-qualification signature* of `T`.
|
| 551 |
+
|
| 552 |
+
Two types `T1` and `T2` are *similar* if they have cv-decompositions
|
| 553 |
+
with the same n such that corresponding Pᵢ components are either the
|
| 554 |
+
same or one is “array of Nᵢ” and the other is “array of unknown bound
|
| 555 |
+
of”, and the types denoted by `U` are the same.
|
| 556 |
+
|
| 557 |
+
The *cv-combined type* of two types `T1` and `T2` is the type `T3`
|
| 558 |
+
similar to `T1` whose cv-decomposition is such that:
|
| 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 cv-decomposition of `T`j. A
|
| 568 |
+
prvalue of type `T1` can be converted to type `T2` if the cv-combined
|
| 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
|
| 575 |
+
inadvertently modify a const object (as it is done on line \#2). For
|
| 576 |
+
example,
|
| 577 |
+
|
| 578 |
+
``` cpp
|
| 579 |
+
int main() {
|
| 580 |
+
const char c = 'c';
|
| 581 |
+
char* pc;
|
| 582 |
+
const char** pcc = &pc; // #1: not allowed
|
| 583 |
+
*pcc = &c;
|
| 584 |
+
*pc = 'C'; // #2: modifies a const object
|
| 585 |
+
}
|
| 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 cv-combined type of `T1` and
|
| 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
|
| 598 |
+
member of `X` of type *cv2* `T`” if “*cv2* `T`” is more cv-qualified
|
| 599 |
+
than “*cv1* `T`”. — *end note*]
|
| 600 |
+
|
| 601 |
+
[*Note 4*: Function types (including those used in
|
| 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`, `char16_t`, `char32_t`,
|
| 608 |
+
or `wchar_t` whose integer conversion rank [[conv.rank]] is less than
|
| 609 |
+
the rank of `int` can be converted to a prvalue of type `int` if `int`
|
| 610 |
+
can represent all the values of the source type; otherwise, the source
|
| 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 to a
|
| 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`,
|
| 626 |
+
or `unsigned long long int`. If none of the types in that list can
|
| 627 |
+
represent all the values of the enumeration, a prvalue of an unscoped
|
| 628 |
+
enumeration type can be converted to a prvalue of the extended integer
|
| 629 |
+
type with lowest integer conversion rank [[conv.rank]] greater than the
|
| 630 |
+
rank of `long long` in which all the values of the enumeration can be
|
| 631 |
+
represented. If there are two such extended types, the signed one is
|
| 632 |
+
chosen.
|
| 633 |
+
|
| 634 |
+
A prvalue of an unscoped enumeration type whose underlying type is fixed
|
| 635 |
+
[[dcl.enum]] can be converted to a prvalue of its underlying type.
|
| 636 |
+
Moreover, if integral promotion can be applied to its underlying type, a
|
| 637 |
+
prvalue of an unscoped enumeration type whose underlying type is fixed
|
| 638 |
+
can also be converted to a prvalue of the promoted underlying type.
|
| 639 |
+
|
| 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 an enumerated type, it is treated as any other value of
|
| 646 |
+
that type for promotion purposes.
|
| 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*.
|
| 652 |
+
|
| 653 |
+
### Floating-point promotion <a id="conv.fpprom">[[conv.fpprom]]</a>
|
| 654 |
+
|
| 655 |
+
A prvalue of type `float` can be converted to a prvalue of type
|
| 656 |
+
`double`. The value is unchanged.
|
| 657 |
+
|
| 658 |
+
This conversion is called *floating-point promotion*.
|
| 659 |
+
|
| 660 |
+
### Integral conversions <a id="conv.integral">[[conv.integral]]</a>
|
| 661 |
+
|
| 662 |
+
A prvalue of an integer type can be converted to a prvalue of another
|
| 663 |
+
integer type. A prvalue of an unscoped enumeration type can be converted
|
| 664 |
+
to a prvalue of an integer type.
|
| 665 |
+
|
| 666 |
+
If the destination type is `bool`, see [[conv.bool]]. If the source
|
| 667 |
+
type is `bool`, the value `false` is converted to zero and the value
|
| 668 |
+
`true` is converted to one.
|
| 669 |
+
|
| 670 |
+
Otherwise, the result is the unique value of the destination type that
|
| 671 |
+
is congruent to the source integer modulo 2ᴺ, where N is the width of
|
| 672 |
+
the destination type.
|
| 673 |
+
|
| 674 |
+
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. If the source value can be exactly
|
| 681 |
+
represented in the destination type, the result of the conversion is
|
| 682 |
+
that exact representation. If the source value is between two adjacent
|
| 683 |
+
destination values, the result of the conversion is an
|
| 684 |
+
*implementation-defined* choice of either of those values. Otherwise,
|
| 685 |
+
the behavior is undefined.
|
| 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>
|
| 691 |
+
|
| 692 |
+
A prvalue of a floating-point type can be converted to a prvalue of an
|
| 693 |
+
integer type. The conversion truncates; that is, the fractional part is
|
| 694 |
+
discarded. The behavior is undefined if the truncated value cannot be
|
| 695 |
+
represented in the destination type.
|
| 696 |
+
|
| 697 |
+
[*Note 1*: If the destination type is `bool`, see
|
| 698 |
+
[[conv.bool]]. — *end note*]
|
| 699 |
+
|
| 700 |
+
A prvalue of an integer type or of an unscoped enumeration type can be
|
| 701 |
+
converted to a prvalue of a floating-point type. The result is exact if
|
| 702 |
+
possible. If the value being converted is in the range of values that
|
| 703 |
+
can be represented but the value cannot be represented exactly, it is an
|
| 704 |
+
*implementation-defined* choice of either the next lower or higher
|
| 705 |
+
representable value.
|
| 706 |
+
|
| 707 |
+
[*Note 2*: Loss of precision occurs if the integral value cannot be
|
| 708 |
+
represented exactly as a value of the floating-point
|
| 709 |
+
type. — *end note*]
|
| 710 |
+
|
| 711 |
+
If the value being converted is outside the range of values that can be
|
| 712 |
+
represented, the behavior is undefined. If the source type is `bool`,
|
| 713 |
+
the value `false` is converted to zero and the value `true` is converted
|
| 714 |
+
to one.
|
| 715 |
+
|
| 716 |
+
### Pointer conversions <a id="conv.ptr">[[conv.ptr]]</a>
|
| 717 |
+
|
| 718 |
+
A *null pointer constant* is an integer literal [[lex.icon]] with value
|
| 719 |
+
zero or a prvalue of type `std::nullptr_t`. A null pointer constant can
|
| 720 |
+
be converted to a pointer type; the result is the null pointer value of
|
| 721 |
+
that type [[basic.compound]] and is distinguishable from every other
|
| 722 |
+
value of object pointer or function pointer type. Such a conversion is
|
| 723 |
+
called a *null pointer conversion*. Two null pointer values of the same
|
| 724 |
+
type shall compare equal. The conversion of a null pointer constant to a
|
| 725 |
+
pointer to cv-qualified type is a single conversion, and not the
|
| 726 |
+
sequence of a pointer conversion followed by a qualification conversion
|
| 727 |
+
[[conv.qual]]. A null pointer constant of integral type can be converted
|
| 728 |
+
to a prvalue of type `std::nullptr_t`.
|
| 729 |
+
|
| 730 |
+
[*Note 1*: The resulting prvalue is not a null pointer
|
| 731 |
+
value. — *end note*]
|
| 732 |
+
|
| 733 |
+
A prvalue of type “pointer to cv `T`”, where `T` is an object type, can
|
| 734 |
+
be converted to a prvalue of type “pointer to cv `void`”. The pointer
|
| 735 |
+
value [[basic.compound]] is unchanged by this conversion.
|
| 736 |
+
|
| 737 |
+
A prvalue of type “pointer to cv `D`”, where `D` is a complete class
|
| 738 |
+
type, can be converted to a prvalue of type “pointer to cv `B`”, where
|
| 739 |
+
`B` is a base class [[class.derived]] of `D`. If `B` is an inaccessible
|
| 740 |
+
[[class.access]] or ambiguous [[class.member.lookup]] base class of `D`,
|
| 741 |
+
a program that necessitates this conversion is ill-formed. The result of
|
| 742 |
+
the conversion is a pointer to the base class subobject of the derived
|
| 743 |
+
class object. The null pointer value is converted to the null pointer
|
| 744 |
+
value of the destination type.
|
| 745 |
+
|
| 746 |
+
### Pointer-to-member conversions <a id="conv.mem">[[conv.mem]]</a>
|
| 747 |
+
|
| 748 |
+
A null pointer constant [[conv.ptr]] can be converted to a
|
| 749 |
+
pointer-to-member type; the result is the *null member pointer value* of
|
| 750 |
+
that type and is distinguishable from any pointer to member not created
|
| 751 |
+
from a null pointer constant. Such a conversion is called a *null member
|
| 752 |
+
pointer conversion*. Two null member pointer values of the same type
|
| 753 |
+
shall compare equal. The conversion of a null pointer constant to a
|
| 754 |
+
pointer to member of cv-qualified type is a single conversion, and not
|
| 755 |
+
the sequence of a pointer-to-member conversion followed by a
|
| 756 |
+
qualification conversion [[conv.qual]].
|
| 757 |
+
|
| 758 |
+
A prvalue of type “pointer to member of `B` of type cv `T`”, where `B`
|
| 759 |
+
is a class type, can be converted to a prvalue of type “pointer to
|
| 760 |
+
member of `D` of type cv `T`”, where `D` is a complete class derived
|
| 761 |
+
[[class.derived]] from `B`. If `B` is an inaccessible [[class.access]],
|
| 762 |
+
ambiguous [[class.member.lookup]], or virtual [[class.mi]] base class of
|
| 763 |
+
`D`, or a base class of a virtual base class of `D`, a program that
|
| 764 |
+
necessitates this conversion is ill-formed. The result of the conversion
|
| 765 |
+
refers to the same member as the pointer to member before the conversion
|
| 766 |
+
took place, but it refers to the base class member as if it were a
|
| 767 |
+
member of the derived class. The result refers to the member in `D`’s
|
| 768 |
+
instance of `B`. Since the result has type “pointer to member of `D` of
|
| 769 |
+
type cv `T`”, indirection through it with a `D` object is valid. The
|
| 770 |
+
result is the same as if indirecting through the pointer to member of
|
| 771 |
+
`B` with the `B` subobject of `D`. The null member pointer value is
|
| 772 |
+
converted to the null member pointer value of the destination type.[^8]
|
| 773 |
+
|
| 774 |
+
### Function pointer conversions <a id="conv.fctptr">[[conv.fctptr]]</a>
|
| 775 |
+
|
| 776 |
+
A prvalue of type “pointer to `noexcept` function” can be converted to a
|
| 777 |
+
prvalue of type “pointer to function”. The result is a pointer to the
|
| 778 |
+
function. A prvalue of type “pointer to member of type `noexcept`
|
| 779 |
+
function” can be converted to a prvalue of type “pointer to member of
|
| 780 |
+
type function”. The result designates the member function.
|
| 781 |
+
|
| 782 |
+
[*Example 1*:
|
| 783 |
+
|
| 784 |
+
``` cpp
|
| 785 |
+
void (*p)();
|
| 786 |
+
void (**pp)() noexcept = &p; // error: cannot convert to pointer to noexcept function
|
| 787 |
+
|
| 788 |
+
struct S { typedef void (*p)(); operator p(); };
|
| 789 |
+
void (*q)() noexcept = S(); // error: cannot convert to pointer to noexcept function
|
| 790 |
+
```
|
| 791 |
+
|
| 792 |
+
— *end example*]
|
| 793 |
+
|
| 794 |
+
### Boolean conversions <a id="conv.bool">[[conv.bool]]</a>
|
| 795 |
+
|
| 796 |
+
A prvalue of arithmetic, unscoped enumeration, pointer, or
|
| 797 |
+
pointer-to-member type can be converted to a prvalue of type `bool`. A
|
| 798 |
+
zero value, null pointer value, or null member pointer value is
|
| 799 |
+
converted to `false`; any other value is converted to `true`.
|
| 800 |
+
|
| 801 |
+
## Usual arithmetic conversions <a id="expr.arith.conv">[[expr.arith.conv]]</a>
|
| 802 |
|
| 803 |
Many binary operators that expect operands of arithmetic or enumeration
|
| 804 |
type cause conversions and yield result types in a similar way. The
|
| 805 |
purpose is to yield a common type, which is also the type of the result.
|
| 806 |
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 |
- If either operand is of type `long double`, the other shall be
|
| 813 |
converted to `long double`.
|
| 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 both operands have signed integer types or both have
|
| 824 |
unsigned integer types, the operand with the type of lesser integer
|
|
|
|
| 834 |
converted to the type of the operand with signed integer type.
|
| 835 |
- Otherwise, both operands shall be converted to the unsigned integer
|
| 836 |
type corresponding to the type of the operand with signed integer
|
| 837 |
type.
|
| 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 |
|
| 843 |
## Primary expressions <a id="expr.prim">[[expr.prim]]</a>
|
| 844 |
|
| 845 |
``` bnf
|
| 846 |
primary-expression:
|
| 847 |
literal
|
| 848 |
+
this
|
| 849 |
'(' expression ')'
|
| 850 |
id-expression
|
| 851 |
lambda-expression
|
| 852 |
fold-expression
|
| 853 |
+
requires-expression
|
| 854 |
```
|
| 855 |
|
| 856 |
### Literals <a id="expr.prim.literal">[[expr.prim.literal]]</a>
|
| 857 |
|
| 858 |
+
A *literal* is a primary expression. The type of a *literal* is
|
| 859 |
+
determined based on its form as specified in [[lex.literal]]. A
|
| 860 |
+
*string-literal* is an lvalue, a *user-defined-literal* has the same
|
| 861 |
+
value category as the corresponding operator call expression described
|
| 862 |
+
in [[lex.ext]], and any other *literal* is a prvalue.
|
| 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 a non-static
|
| 867 |
+
member function [[class.this]] is invoked or a non-static data member’s
|
| 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`” between the optional *cv-qualifier-seq* and the
|
| 873 |
end of the *function-definition*, *member-declarator*, or *declarator*.
|
|
|
|
| 877 |
as they are within a non-static member function).
|
| 878 |
|
| 879 |
[*Note 1*: This is because declaration matching does not occur until
|
| 880 |
the complete declarator is known. — *end note*]
|
| 881 |
|
| 882 |
+
[*Note 2*:
|
|
|
|
|
|
|
| 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 |
|
| 888 |
[*Example 1*:
|
| 889 |
|
| 890 |
``` cpp
|
| 891 |
struct A {
|
|
|
|
| 896 |
template auto A::f(int t) -> decltype(t + g());
|
| 897 |
```
|
| 898 |
|
| 899 |
— *end example*]
|
| 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`” within the optional default member initializer
|
| 906 |
+
[[class.mem]]. It shall not appear elsewhere in the *member-declarator*.
|
| 907 |
|
| 908 |
The expression `this` shall not appear in any other context.
|
| 909 |
|
| 910 |
[*Example 2*:
|
| 911 |
|
|
|
|
| 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 |
+
value, and value category are identical to those of E. The parenthesized
|
| 933 |
+
expression can be used in exactly the same contexts as those where E can
|
| 934 |
+
be used, and with the same meaning, except as otherwise indicated.
|
|
|
|
| 935 |
|
| 936 |
### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
|
| 937 |
|
| 938 |
``` bnf
|
| 939 |
id-expression:
|
|
|
|
| 941 |
qualified-id
|
| 942 |
```
|
| 943 |
|
| 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 |
An *id-expression* that denotes a non-static data member or non-static
|
| 950 |
member function of a class can only be used:
|
| 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 1*:
|
| 959 |
``` cpp
|
| 960 |
struct S {
|
|
|
|
| 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 ([[over.match]],
|
| 977 |
+
[[over.over]]).
|
| 978 |
+
|
| 979 |
+
[*Note 2*:
|
| 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 2*:
|
| 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 [[expr.prop]].
|
| 1002 |
+
|
| 1003 |
+
— *end example*]
|
| 1004 |
+
|
| 1005 |
+
— *end note*]
|
| 1006 |
+
|
| 1007 |
#### Unqualified names <a id="expr.prim.id.unqual">[[expr.prim.id.unqual]]</a>
|
| 1008 |
|
| 1009 |
``` bnf
|
| 1010 |
unqualified-id:
|
| 1011 |
identifier
|
| 1012 |
operator-function-id
|
| 1013 |
conversion-function-id
|
| 1014 |
literal-operator-id
|
| 1015 |
+
'~' type-name
|
| 1016 |
'~' decltype-specifier
|
| 1017 |
template-id
|
| 1018 |
```
|
| 1019 |
|
| 1020 |
+
An *identifier* is only an *id-expression* if it has been suitably
|
| 1021 |
+
declared [[dcl.dcl]] or if it appears as part of a *declarator-id*
|
| 1022 |
+
[[dcl.decl]]. An *identifier* that names a coroutine parameter refers to
|
| 1023 |
+
the copy of the parameter [[dcl.fct.def.coroutine]].
|
| 1024 |
|
| 1025 |
[*Note 1*: For *operator-function-id*s, see [[over.oper]]; for
|
| 1026 |
*conversion-function-id*s, see [[class.conv.fct]]; for
|
| 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 ([[class.mfct.non-static]]). — *end note*]
|
| 1033 |
+
|
| 1034 |
+
The result is the entity denoted by the identifier. If the entity is a
|
| 1035 |
+
local entity and naming it from outside of an unevaluated operand within
|
| 1036 |
+
the declarative region where the *unqualified-id* appears would result
|
| 1037 |
+
in some intervening *lambda-expression* capturing it by copy
|
| 1038 |
+
[[expr.prim.lambda.capture]], the type of the expression is the type of
|
| 1039 |
+
a class member access expression [[expr.ref]] naming the non-static data
|
| 1040 |
+
member that would be declared for such a capture in the closure object
|
| 1041 |
+
of the innermost such intervening *lambda-expression*.
|
| 1042 |
+
|
| 1043 |
+
[*Note 2*: If that *lambda-expression* is not declared `mutable`, the
|
| 1044 |
+
type of such an identifier will typically be `const`
|
| 1045 |
+
qualified. — *end note*]
|
| 1046 |
+
|
| 1047 |
+
The type of the expression is the type of the result.
|
| 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 4*: The type will be adjusted as described in [[expr.type]] if
|
| 1054 |
+
it is cv-qualified or is a reference type. — *end note*]
|
| 1055 |
+
|
| 1056 |
+
The expression is an lvalue if the entity is a function, variable,
|
| 1057 |
+
structured binding [[dcl.struct.bind]], data member, or template
|
| 1058 |
+
parameter object and a prvalue otherwise [[basic.lval]]; it is a
|
| 1059 |
+
bit-field if the identifier designates a bit-field.
|
| 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& because this lambda
|
| 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
|
| 1083 |
```
|
| 1084 |
|
| 1085 |
``` bnf
|
| 1086 |
nested-name-specifier:
|
| 1087 |
'::'
|
| 1088 |
type-name '::'
|
| 1089 |
namespace-name '::'
|
| 1090 |
decltype-specifier '::'
|
| 1091 |
nested-name-specifier identifier '::'
|
| 1092 |
+
nested-name-specifier templateₒₚₜ simple-template-id '::'
|
| 1093 |
```
|
| 1094 |
|
| 1095 |
The type denoted by a *decltype-specifier* in a *nested-name-specifier*
|
| 1096 |
shall be a class or enumeration type.
|
| 1097 |
|
| 1098 |
A *nested-name-specifier* that denotes a class, optionally followed by
|
| 1099 |
+
the keyword `template` [[temp.names]], and then followed by the name of
|
| 1100 |
+
a member of either that class [[class.mem]] or one of its base classes
|
| 1101 |
+
[[class.derived]], is a *qualified-id*; [[class.qual]] describes name
|
| 1102 |
+
lookup for class members that appear in *qualified-id*s. The result is
|
| 1103 |
+
the member. The type of the result is the type of the member. The result
|
| 1104 |
+
is an lvalue if the member is a static member function or a data member
|
| 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*.
|
| 1146 |
+
|
| 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 ([[expr.call]],
|
| 1153 |
+
[[basic.life]]). — *end note*]
|
| 1154 |
+
|
| 1155 |
+
[*Example 1*:
|
| 1156 |
+
|
| 1157 |
+
``` cpp
|
| 1158 |
+
struct C { };
|
| 1159 |
+
void f() {
|
| 1160 |
+
C * pc = new C;
|
| 1161 |
+
using C2 = C;
|
| 1162 |
+
pc->C::~C2(); // OK, destroys *pc
|
| 1163 |
+
C().C::~C(); // undefined behavior: temporary of type C destroyed twice
|
| 1164 |
+
using T = int;
|
| 1165 |
+
0 .T::~T(); // OK, no effect
|
| 1166 |
+
0.T::~T(); // error: 0.T is a user-defined-floating-point-literal[lex.ext]
|
| 1167 |
+
}
|
| 1168 |
+
```
|
| 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 lambda-declaratorₒₚₜ compound-statement
|
| 1177 |
+
lambda-introducer '<' template-parameter-list '>' requires-clauseₒₚₜ lambda-declaratorₒₚₜ compound-statement
|
| 1178 |
```
|
| 1179 |
|
| 1180 |
``` bnf
|
| 1181 |
lambda-introducer:
|
| 1182 |
'[' lambda-captureₒₚₜ ']'
|
| 1183 |
```
|
| 1184 |
|
| 1185 |
``` bnf
|
| 1186 |
lambda-declarator:
|
| 1187 |
'(' parameter-declaration-clause ')' decl-specifier-seqₒₚₜ
|
| 1188 |
+
noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ requires-clauseₒₚₜ
|
| 1189 |
```
|
| 1190 |
|
| 1191 |
+
A *lambda-expression* provides a concise way to create a simple function
|
| 1192 |
+
object.
|
| 1193 |
|
| 1194 |
[*Example 1*:
|
| 1195 |
|
| 1196 |
``` cpp
|
| 1197 |
#include <algorithm>
|
|
|
|
| 1202 |
```
|
| 1203 |
|
| 1204 |
— *end example*]
|
| 1205 |
|
| 1206 |
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 |
In the *decl-specifier-seq* of the *lambda-declarator*, each
|
| 1213 |
+
*decl-specifier* shall be one of `mutable`, `constexpr`, or `consteval`.
|
| 1214 |
+
|
| 1215 |
+
[*Note 2*: The trailing *requires-clause* is described in
|
| 1216 |
+
[[dcl.decl]]. — *end note*]
|
| 1217 |
|
| 1218 |
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 1219 |
as if the *lambda-declarator* were `()`. The lambda return type is
|
| 1220 |
`auto`, which is replaced by the type specified by the
|
| 1221 |
*trailing-return-type* if provided and/or deduced from `return`
|
|
|
|
| 1230 |
auto x3 = []()->auto&& { return j; }; // OK: return type is int&
|
| 1231 |
```
|
| 1232 |
|
| 1233 |
— *end example*]
|
| 1234 |
|
| 1235 |
+
A lambda is a *generic lambda* if the *lambda-expression* has any
|
| 1236 |
+
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: a generic lambda
|
| 1243 |
+
int j = []<class T>(T t, int i) { return i; }(3, 4); // OK: a generic lambda
|
| 1244 |
+
```
|
| 1245 |
+
|
| 1246 |
+
— *end example*]
|
| 1247 |
+
|
| 1248 |
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
| 1249 |
|
| 1250 |
The type of a *lambda-expression* (which is also the type of the closure
|
| 1251 |
object) is a unique, unnamed non-union class type, called the *closure
|
| 1252 |
type*, whose properties are described below.
|
| 1253 |
|
| 1254 |
The closure type is declared in the smallest block scope, class scope,
|
| 1255 |
or namespace scope that contains the corresponding *lambda-expression*.
|
| 1256 |
|
| 1257 |
[*Note 1*: This determines the set of namespaces and classes associated
|
| 1258 |
+
with the closure type [[basic.lookup.argdep]]. The parameter types of a
|
| 1259 |
+
*lambda-declarator* do not affect these associated namespaces and
|
| 1260 |
classes. — *end note*]
|
| 1261 |
|
| 1262 |
+
The closure type is not an aggregate type [[dcl.init.aggr]]. An
|
| 1263 |
implementation may define the closure type differently from what is
|
| 1264 |
described below provided this does not alter the observable behavior of
|
| 1265 |
the program other than by changing:
|
| 1266 |
|
| 1267 |
- the size and/or alignment of the closure type,
|
| 1268 |
+
- whether the closure type is trivially copyable [[class.prop]], or
|
| 1269 |
+
- whether the closure type is a standard-layout class [[class.prop]].
|
|
|
|
|
|
|
| 1270 |
|
| 1271 |
An implementation shall not add members of rvalue reference type to the
|
| 1272 |
closure type.
|
| 1273 |
|
| 1274 |
+
The closure type for a *lambda-expression* has a public inline function
|
| 1275 |
+
call operator (for a non-generic lambda) or function call operator
|
| 1276 |
+
template (for a generic lambda) [[over.call]] whose parameters and
|
| 1277 |
return type are described by the *lambda-expression*’s
|
| 1278 |
+
*parameter-declaration-clause* and *trailing-return-type* respectively,
|
| 1279 |
+
and whose *template-parameter-list* consists of the specified
|
| 1280 |
+
*template-parameter-list*, if any. The *requires-clause* of the function
|
| 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 |
+
might be an abbreviated function template [[dcl.fct]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 1288 |
|
| 1289 |
[*Example 1*:
|
| 1290 |
|
| 1291 |
``` cpp
|
| 1292 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
|
|
|
| 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. The function call operator or any
|
| 1320 |
given operator template specialization is a constexpr function if either
|
| 1321 |
the corresponding *lambda-expression*'s *parameter-declaration-clause*
|
| 1322 |
+
is followed by `constexpr` or `consteval`, or it satisfies the
|
| 1323 |
+
requirements for a constexpr function [[dcl.constexpr]]. It is an
|
| 1324 |
+
immediate function [[dcl.constexpr]] if the corresponding
|
| 1325 |
+
*lambda-expression*'s *parameter-declaration-clause* is followed by
|
| 1326 |
+
`consteval`.
|
| 1327 |
|
| 1328 |
+
[*Note 3*: Names referenced in the *lambda-declarator* are looked up in
|
| 1329 |
the context in which the *lambda-expression* appears. — *end note*]
|
| 1330 |
|
| 1331 |
[*Example 2*:
|
| 1332 |
|
| 1333 |
``` cpp
|
|
|
|
| 1336 |
|
| 1337 |
struct NonLiteral {
|
| 1338 |
NonLiteral(int n) : n(n) { }
|
| 1339 |
int n;
|
| 1340 |
};
|
| 1341 |
+
static_assert(ID(NonLiteral{3}).n == 3); // error
|
| 1342 |
```
|
| 1343 |
|
| 1344 |
— *end example*]
|
| 1345 |
|
| 1346 |
[*Example 3*:
|
|
|
|
| 1364 |
// Since two below is not declared constexpr, an evaluation of its constexpr member function call operator
|
| 1365 |
// cannot perform an lvalue-to-rvalue conversion on one of its subobjects (that represents its capture)
|
| 1366 |
// in a constant expression.
|
| 1367 |
auto two = monoid(2);
|
| 1368 |
assert(two() == 2); // OK, not a constant expression.
|
| 1369 |
+
static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
|
| 1370 |
static_assert(add(one)(one)() == monoid(2)()); // OK
|
| 1371 |
```
|
| 1372 |
|
| 1373 |
— *end example*]
|
| 1374 |
|
| 1375 |
+
[*Note 4*:
|
| 1376 |
+
|
| 1377 |
+
The function call operator or operator template may be constrained
|
| 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 4*:
|
| 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 = ...;
|
| 1388 |
+
|
| 1389 |
+
auto f = []<typename T1, C1 T2> requires C2<sizeof(T1) + sizeof(T2)>
|
| 1390 |
+
(T1 a1, T1 b1, T2 a2, auto a3, auto a4) requires C3<decltype(a4), T2> {
|
| 1391 |
+
// T2 is constrained by a type-constraint.
|
| 1392 |
+
// T1 and T2 are constrained by a requires-clause, and
|
| 1393 |
+
// T2 and the type of a4 are constrained by a trailing requires-clause.
|
| 1394 |
+
};
|
| 1395 |
+
```
|
| 1396 |
+
|
| 1397 |
+
— *end example*]
|
| 1398 |
+
|
| 1399 |
+
— *end note*]
|
| 1400 |
+
|
| 1401 |
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. The value returned by this conversion function
|
| 1408 |
+
is the address of a function `F` that, when invoked, has the same effect
|
| 1409 |
+
as invoking the closure type’s function call operator on a
|
| 1410 |
+
default-constructed instance of the closure type. `F` is a constexpr
|
| 1411 |
+
function if the function call operator is a constexpr function and is an
|
| 1412 |
+
immediate function if the function call operator is an immediate
|
| 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 5*:
|
| 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 |
};
|
| 1455 |
```
|
| 1456 |
|
| 1457 |
— *end note*]
|
| 1458 |
|
| 1459 |
+
[*Example 5*:
|
| 1460 |
|
| 1461 |
``` cpp
|
| 1462 |
void f1(int (*)(int)) { }
|
| 1463 |
void f2(char (*)(int)) { }
|
| 1464 |
|
|
|
|
| 1479 |
— *end example*]
|
| 1480 |
|
| 1481 |
The value returned by any given specialization of this conversion
|
| 1482 |
function template is the address of a function `F` that, when invoked,
|
| 1483 |
has the same effect as invoking the generic lambda’s corresponding
|
| 1484 |
+
function call operator template specialization on a default-constructed
|
| 1485 |
+
instance of the closure type. `F` is a constexpr function if the
|
| 1486 |
+
corresponding specialization is a constexpr function and `F` is an
|
| 1487 |
+
immediate function if the function call operator template specialization
|
| 1488 |
+
is an immediate function.
|
| 1489 |
|
| 1490 |
+
[*Note 6*: This will result in the implicit instantiation of the
|
| 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 6*:
|
| 1496 |
|
| 1497 |
``` cpp
|
| 1498 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 1499 |
int (*GL_int)(int) = GL; // OK: through conversion function template
|
| 1500 |
GL_int(3); // OK: same as GL(3)
|
|
|
|
| 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 7*:
|
| 1510 |
|
| 1511 |
``` cpp
|
| 1512 |
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 1513 |
auto C = [](auto a) { return a; };
|
| 1514 |
|
| 1515 |
static_assert(Fwd(C,3) == 3); // OK
|
| 1516 |
|
| 1517 |
// No specialization of the function call operator template can be constexpr (due to the local static).
|
| 1518 |
auto NC = [](auto a) { static int s; return a; };
|
| 1519 |
+
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 for
|
| 1526 |
+
purposes of name lookup [[basic.lookup]], determining the type and value
|
| 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 8*:
|
| 1533 |
|
| 1534 |
``` cpp
|
| 1535 |
struct S1 {
|
| 1536 |
int x, y;
|
| 1537 |
int operator()(int);
|
|
|
|
| 1549 |
Further, a variable `__func__` is implicitly defined at the beginning of
|
| 1550 |
the *compound-statement* of the *lambda-expression*, with semantics as
|
| 1551 |
described in [[dcl.fct.def.general]].
|
| 1552 |
|
| 1553 |
The closure type associated with a *lambda-expression* has no default
|
| 1554 |
+
constructor if the *lambda-expression* has a *lambda-capture* and a
|
| 1555 |
+
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 7*: These special member functions are implicitly defined as
|
| 1562 |
usual, and might therefore be defined as deleted. — *end note*]
|
| 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
|
| 1568 |
+
[[temp.explicit]], explicitly specialized [[temp.expl.spec]], or named
|
| 1569 |
+
in a friend declaration [[class.friend]].
|
| 1570 |
|
| 1571 |
#### Captures <a id="expr.prim.lambda.capture">[[expr.prim.lambda.capture]]</a>
|
| 1572 |
|
| 1573 |
``` bnf
|
| 1574 |
lambda-capture:
|
|
|
|
| 1583 |
'='
|
| 1584 |
```
|
| 1585 |
|
| 1586 |
``` bnf
|
| 1587 |
capture-list:
|
| 1588 |
+
capture
|
| 1589 |
+
capture-list ',' capture
|
| 1590 |
```
|
| 1591 |
|
| 1592 |
``` bnf
|
| 1593 |
capture:
|
| 1594 |
simple-capture
|
| 1595 |
init-capture
|
| 1596 |
```
|
| 1597 |
|
| 1598 |
``` bnf
|
| 1599 |
simple-capture:
|
| 1600 |
+
identifier '...'ₒₚₜ
|
| 1601 |
+
'&' identifier '...'ₒₚₜ
|
| 1602 |
+
this
|
| 1603 |
+
'*' 'this'
|
| 1604 |
```
|
| 1605 |
|
| 1606 |
``` bnf
|
| 1607 |
init-capture:
|
| 1608 |
+
'...'ₒₚₜ identifier initializer
|
| 1609 |
+
'&' '...'ₒₚₜ identifier initializer
|
| 1610 |
```
|
| 1611 |
|
| 1612 |
The body of a *lambda-expression* may refer to variables with automatic
|
| 1613 |
storage duration and the `*this` object (if any) of enclosing block
|
| 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
|
| 1620 |
+
form “`&` *identifier* `...`ₒₚₜ ”, “`this`”, or “`* this`”.
|
| 1621 |
|
| 1622 |
[*Note 1*: The form `[&,this]` is redundant but accepted for
|
| 1623 |
compatibility with ISO C++14. — *end note*]
|
| 1624 |
|
| 1625 |
Ignoring appearances in *initializer*s of *init-capture*s, an identifier
|
|
|
|
| 1629 |
|
| 1630 |
``` cpp
|
| 1631 |
struct S2 { void f(int i); };
|
| 1632 |
void S2::f(int i) {
|
| 1633 |
[&, i]{ }; // OK
|
| 1634 |
+
[&, this, i]{ }; // OK, equivalent to [&, i]
|
| 1635 |
[&, &i]{ }; // error: i preceded by & when & is the default
|
| 1636 |
[=, *this]{ }; // OK
|
| 1637 |
+
[=, this]{ }; // OK, equivalent to [=]
|
| 1638 |
[i, i]{ }; // error: i repeated
|
| 1639 |
[this, *this]{ }; // error: this appears twice
|
| 1640 |
}
|
| 1641 |
```
|
| 1642 |
|
| 1643 |
— *end example*]
|
| 1644 |
|
| 1645 |
+
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* is looked up using the usual
|
| 1652 |
+
rules for unqualified name lookup [[basic.lookup.unqual]]; each such
|
| 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 *simple-capture* appears as the *declarator-id*
|
| 1658 |
of a parameter of the *lambda-declarator*'s
|
| 1659 |
*parameter-declaration-clause*, the program is ill-formed.
|
| 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 simple-capture have the same name
|
| 1667 |
}
|
| 1668 |
```
|
| 1669 |
|
| 1670 |
— *end example*]
|
| 1671 |
|
| 1672 |
+
An *init-capture* without ellipsis behaves as if it declares and
|
| 1673 |
+
explicitly captures a variable of the form “`auto` *init-capture* `;`”
|
| 1674 |
+
whose declarative region is the *lambda-expression*’s
|
| 1675 |
+
*compound-statement*, except that:
|
| 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
|
| 1681 |
performed, and
|
| 1682 |
- if the capture is by reference, the variable’s lifetime ends when the
|
| 1683 |
closure object’s lifetime ends.
|
| 1684 |
|
| 1685 |
+
[*Note 2*: This enables an *init-capture* like “`x = std::move(x)`”;
|
| 1686 |
the second “`x`” must bind to a declaration in the surrounding
|
| 1687 |
context. — *end note*]
|
| 1688 |
|
| 1689 |
[*Example 3*:
|
| 1690 |
|
|
|
|
| 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
|
| 1704 |
+
local entities as follows:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1705 |
|
| 1706 |
+
- An *id-expression* that names a local entity potentially references
|
| 1707 |
+
that entity; an *id-expression* that names one or more non-static
|
| 1708 |
+
class members and does not form a pointer to member [[expr.unary.op]]
|
| 1709 |
+
potentially references `*this`. \[*Note 3*: This occurs even if
|
| 1710 |
+
overload resolution selects a static member function for the
|
| 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 |
+
declarative region in which it is odr-usable, and the expression would
|
| 1718 |
+
be potentially evaluated if the effect of any enclosing `typeid`
|
| 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]].
|
| 1724 |
|
| 1725 |
[*Example 4*:
|
| 1726 |
|
| 1727 |
``` cpp
|
| 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: calls #1, does not capture x
|
| 1734 |
};
|
| 1735 |
|
| 1736 |
+
auto g1 = [=](auto a) {
|
| 1737 |
+
f(x); // OK: calls #1, captures x
|
| 1738 |
+
};
|
| 1739 |
+
|
| 1740 |
auto g2 = [=](auto a) {
|
| 1741 |
int selector[sizeof(a) == 1 ? 1 : 2]{};
|
| 1742 |
+
f(x, selector); // OK: captures x, might call #1 or #2
|
| 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 might optimize away the capture of `x` as
|
| 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 |
+
might be implicitly captured even if the expression denoting a local
|
| 1760 |
+
entity is within a discarded statement [[stmt.if]].
|
| 1761 |
+
|
| 1762 |
+
[*Example 5*:
|
| 1763 |
+
|
| 1764 |
+
``` cpp
|
| 1765 |
+
template<bool B>
|
| 1766 |
+
void f(int n) {
|
| 1767 |
+
[=](auto a) {
|
| 1768 |
+
if constexpr (B && sizeof(a) > 4) {
|
| 1769 |
+
(void)n; // captures n regardless of the value of B and sizeof(int)
|
| 1770 |
+
}
|
| 1771 |
+
}(0);
|
| 1772 |
+
}
|
| 1773 |
+
```
|
| 1774 |
+
|
| 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 [[basic.def.odr]]
|
| 1781 |
+
in the scope containing the *lambda-expression*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 1787 |
+
[*Example 6*:
|
| 1788 |
|
| 1789 |
``` cpp
|
| 1790 |
void f1(int i) {
|
| 1791 |
int const N = 20;
|
| 1792 |
auto m1 = [=]{
|
|
|
|
| 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: m implicitly captured by m4 and explicitly captured by m3
|
| 1808 |
+
x += i; // error: i is odr-used but not odr-usable
|
| 1809 |
+
// due to intervening function and class scopes
|
| 1810 |
x += f; // OK: this captured implicitly by m4 and explicitly by m3
|
| 1811 |
};
|
| 1812 |
};
|
| 1813 |
}
|
| 1814 |
};
|
|
|
|
| 1818 |
double ohseven = .007;
|
| 1819 |
auto f() {
|
| 1820 |
return [this] {
|
| 1821 |
return [*this] {
|
| 1822 |
return ohseven; // OK
|
| 1823 |
+
};
|
| 1824 |
}();
|
| 1825 |
}
|
| 1826 |
auto g() {
|
| 1827 |
return [] {
|
| 1828 |
return [*this] { }; // error: *this not captured by outer lambda-expression
|
|
|
|
| 1831 |
};
|
| 1832 |
```
|
| 1833 |
|
| 1834 |
— *end example*]
|
| 1835 |
|
| 1836 |
+
[*Note 6*: Because local entities are not odr-usable within a default
|
| 1837 |
+
argument [[basic.def.odr]], a *lambda-expression* appearing in a default
|
| 1838 |
+
argument cannot implicitly or explicitly capture any local entity. Such
|
| 1839 |
+
a *lambda-expression* can still have an *init-capture* if any
|
| 1840 |
+
full-expression in its *initializer* satisfies the constraints of an
|
| 1841 |
+
expression appearing in a default argument
|
| 1842 |
+
[[dcl.fct.default]]. — *end note*]
|
| 1843 |
|
| 1844 |
+
[*Example 7*:
|
| 1845 |
|
| 1846 |
``` cpp
|
| 1847 |
void f2() {
|
| 1848 |
int i = 1;
|
| 1849 |
+
void g1(int = ([i]{ return i; })()); // error
|
| 1850 |
+
void g2(int = ([i]{ return 0; })()); // error
|
| 1851 |
+
void g3(int = ([=]{ return i; })()); // error
|
| 1852 |
void g4(int = ([=]{ return 0; })()); // OK
|
| 1853 |
void g5(int = ([]{ return sizeof i; })()); // OK
|
| 1854 |
+
void g6(int = ([x=1]{ return x; })()); // OK
|
| 1855 |
+
void g7(int = ([x=i]{ return x; })()); // error
|
| 1856 |
}
|
| 1857 |
```
|
| 1858 |
|
| 1859 |
— *end example*]
|
| 1860 |
|
|
|
|
| 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 [[basic.def.odr]] of an entity
|
| 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
|
| 1883 |
+
*id-expression* can still cause the implicit capture of the
|
| 1884 |
entity. — *end note*]
|
| 1885 |
|
| 1886 |
+
If `*this` is captured by copy, each expression that odr-uses `*this` is
|
| 1887 |
+
transformed to instead refer to the corresponding unnamed data member of
|
| 1888 |
+
the closure type.
|
| 1889 |
|
| 1890 |
+
[*Example 8*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1891 |
|
| 1892 |
``` cpp
|
| 1893 |
void f(const int*);
|
| 1894 |
void g() {
|
| 1895 |
const int N = 10;
|
|
|
|
| 1897 |
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 1898 |
f(&N); // OK: causes N to be captured; &N points to
|
| 1899 |
// the corresponding member of the closure type
|
| 1900 |
};
|
| 1901 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1902 |
```
|
| 1903 |
|
| 1904 |
— *end example*]
|
| 1905 |
|
| 1906 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 1907 |
captured but not captured by copy. It is unspecified whether additional
|
| 1908 |
unnamed non-static data members are declared in the closure type for
|
| 1909 |
entities captured by reference. If declared, such non-static data
|
| 1910 |
members shall be of literal type.
|
| 1911 |
|
| 1912 |
+
[*Example 9*:
|
| 1913 |
|
| 1914 |
``` cpp
|
| 1915 |
// The inner closure type must be a literal type regardless of how reference captures are represented.
|
| 1916 |
static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
|
| 1917 |
```
|
|
|
|
| 1919 |
— *end example*]
|
| 1920 |
|
| 1921 |
A bit-field or a member of an anonymous union shall not be captured by
|
| 1922 |
reference.
|
| 1923 |
|
| 1924 |
+
An *id-expression* within the *compound-statement* of a
|
| 1925 |
+
*lambda-expression* that is an odr-use of a reference captured by
|
| 1926 |
+
reference refers to the entity to which the captured reference is bound
|
| 1927 |
+
and not to the captured reference.
|
| 1928 |
+
|
| 1929 |
+
[*Note 8*: The validity of such captures is determined by the lifetime
|
| 1930 |
+
of the object to which the reference refers, not by the lifetime of the
|
| 1931 |
+
reference itself. — *end note*]
|
| 1932 |
+
|
| 1933 |
+
[*Example 10*:
|
| 1934 |
+
|
| 1935 |
+
``` cpp
|
| 1936 |
+
auto h(int &r) {
|
| 1937 |
+
return [&] {
|
| 1938 |
+
++r; // Valid after h returns if the lifetime of the
|
| 1939 |
+
// object to which r is bound has not ended
|
| 1940 |
+
};
|
| 1941 |
+
}
|
| 1942 |
+
```
|
| 1943 |
+
|
| 1944 |
+
— *end example*]
|
| 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 |
- if `m1` captures the entity by copy, `m2` captures the corresponding
|
| 1951 |
non-static data member of `m1`’s closure type;
|
| 1952 |
- if `m1` captures the entity by reference, `m2` captures the same
|
| 1953 |
entity captured by `m1`.
|
| 1954 |
|
| 1955 |
+
[*Example 11*:
|
| 1956 |
|
| 1957 |
+
The nested *lambda-expression*s and invocations below will output
|
| 1958 |
`123234`.
|
| 1959 |
|
| 1960 |
``` cpp
|
| 1961 |
int a = 1, b = 1, c = 1;
|
| 1962 |
auto m1 = [a, &b, &c]() mutable {
|
|
|
|
| 1972 |
std::cout << a << b << c;
|
| 1973 |
```
|
| 1974 |
|
| 1975 |
— *end example*]
|
| 1976 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1977 |
When the *lambda-expression* is evaluated, the entities that are
|
| 1978 |
captured by copy are used to direct-initialize each corresponding
|
| 1979 |
non-static data member of the resulting closure object, and the
|
| 1980 |
non-static data members corresponding to the *init-capture*s are
|
| 1981 |
initialized as indicated by the corresponding *initializer* (which may
|
| 1982 |
be copy- or direct-initialization). (For array members, the array
|
| 1983 |
elements are direct-initialized in increasing subscript order.) These
|
| 1984 |
initializations are performed in the (unspecified) order in which the
|
| 1985 |
non-static data members are declared.
|
| 1986 |
|
| 1987 |
+
[*Note 9*: This ensures that the destructions will occur in the reverse
|
| 1988 |
order of the constructions. — *end note*]
|
| 1989 |
|
| 1990 |
+
[*Note 10*: If a non-reference entity is implicitly or explicitly
|
| 1991 |
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 introduces an *init-capture* pack [[temp.variadic]] whose
|
| 1998 |
+
declarative region is the *lambda-expression*’s *compound-statement*.
|
| 1999 |
|
| 2000 |
+
[*Example 12*:
|
| 2001 |
|
| 2002 |
``` cpp
|
| 2003 |
template<class... Args>
|
| 2004 |
void f(Args... args) {
|
| 2005 |
auto lm = [&, args...] { return g(args...); };
|
| 2006 |
lm();
|
| 2007 |
+
|
| 2008 |
+
auto lm2 = [...xs=std::move(args)] { return g(xs...); };
|
| 2009 |
+
lm2();
|
| 2010 |
}
|
| 2011 |
```
|
| 2012 |
|
| 2013 |
— *end example*]
|
| 2014 |
|
| 2015 |
### Fold expressions <a id="expr.prim.fold">[[expr.prim.fold]]</a>
|
| 2016 |
|
| 2017 |
+
A fold expression performs a fold of a pack [[temp.variadic]] over a
|
| 2018 |
+
binary operator.
|
| 2019 |
|
| 2020 |
``` bnf
|
| 2021 |
fold-expression:
|
| 2022 |
'(' cast-expression fold-operator '...' ')'
|
| 2023 |
'(' '...' fold-operator cast-expression ')'
|
|
|
|
| 2036 |
An expression of the form `(...` *op* `e)` where *op* is a
|
| 2037 |
*fold-operator* is called a *unary left fold*. An expression of the form
|
| 2038 |
`(e` *op* `...)` where *op* is a *fold-operator* is called a *unary
|
| 2039 |
right fold*. Unary left folds and unary right folds are collectively
|
| 2040 |
called *unary folds*. In a unary fold, the *cast-expression* shall
|
| 2041 |
+
contain an unexpanded pack [[temp.variadic]].
|
| 2042 |
|
| 2043 |
An expression of the form `(e1` *op1* `...` *op2* `e2)` where *op1* and
|
| 2044 |
*op2* are *fold-operator*s is called a *binary fold*. In a binary fold,
|
| 2045 |
*op1* and *op2* shall be the same *fold-operator*, and either `e1` shall
|
| 2046 |
+
contain an unexpanded pack or `e2` shall contain an unexpanded pack, but
|
| 2047 |
+
not both. If `e2` contains an unexpanded pack, the expression is called
|
| 2048 |
+
a *binary left fold*. If `e1` contains an unexpanded pack, the
|
| 2049 |
+
expression is called a *binary right fold*.
|
|
|
|
| 2050 |
|
| 2051 |
[*Example 1*:
|
| 2052 |
|
| 2053 |
``` cpp
|
| 2054 |
template<typename ...Args>
|
|
|
|
| 2056 |
return (true && ... && args); // OK
|
| 2057 |
}
|
| 2058 |
|
| 2059 |
template<typename ...Args>
|
| 2060 |
bool f(Args ...args) {
|
| 2061 |
+
return (args + ... + args); // error: both operands contain unexpanded packs
|
| 2062 |
}
|
| 2063 |
```
|
| 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
|
| 2074 |
+
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-seq requirement
|
| 2092 |
+
```
|
| 2093 |
+
|
| 2094 |
+
``` bnf
|
| 2095 |
+
requirement:
|
| 2096 |
+
simple-requirement
|
| 2097 |
+
type-requirement
|
| 2098 |
+
compound-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 [[expr.prop]].
|
| 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:
|
| 2110 |
+
|
| 2111 |
+
``` cpp
|
| 2112 |
+
template<typename T>
|
| 2113 |
+
concept R = requires (T i) {
|
| 2114 |
+
typename T::type;
|
| 2115 |
+
{*i} -> std::convertible_to<const typename T::type&>;
|
| 2116 |
+
};
|
| 2117 |
+
```
|
| 2118 |
+
|
| 2119 |
+
A *requires-expression* can also be used in a *requires-clause*
|
| 2120 |
+
[[temp.pre]] as a way of writing ad hoc constraints on template
|
| 2121 |
+
arguments such as the one below:
|
| 2122 |
+
|
| 2123 |
+
``` cpp
|
| 2124 |
+
template<typename T>
|
| 2125 |
+
requires requires (T x) { x + x; }
|
| 2126 |
+
T add(T a, T b) { return a + b; }
|
| 2127 |
+
```
|
| 2128 |
+
|
| 2129 |
+
The first `requires` introduces the *requires-clause*, and the second
|
| 2130 |
+
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. Each name
|
| 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 |
+
|
| 2144 |
+
[*Example 2*:
|
| 2145 |
+
|
| 2146 |
+
``` cpp
|
| 2147 |
+
template<typename T>
|
| 2148 |
+
concept C = requires(T t, ...) { // error: terminates with an ellipsis
|
| 2149 |
+
t;
|
| 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
|
| 2164 |
+
substitution and semantic constraint checking proceeds in lexical order
|
| 2165 |
+
and stops when a condition that determines the result of the
|
| 2166 |
+
*requires-expression* is encountered. If substitution (if any) and
|
| 2167 |
+
semantic constraint checking succeed, the *requires-expression*
|
| 2168 |
+
evaluates to `true`.
|
| 2169 |
+
|
| 2170 |
+
[*Note 1*: If a *requires-expression* contains invalid types or
|
| 2171 |
+
expressions in its *requirement*s, and it does not appear within the
|
| 2172 |
+
declaration of a templated entity, then the program is
|
| 2173 |
+
ill-formed. — *end note*]
|
| 2174 |
+
|
| 2175 |
+
If the substitution of template arguments into a *requirement* would
|
| 2176 |
+
always result in a substitution failure, the program is ill-formed; no
|
| 2177 |
+
diagnostic required.
|
| 2178 |
+
|
| 2179 |
+
[*Example 3*:
|
| 2180 |
+
|
| 2181 |
+
``` cpp
|
| 2182 |
+
template<typename T> concept C =
|
| 2183 |
+
requires {
|
| 2184 |
+
new int[-(int)sizeof(T)]; // ill-formed, no diagnostic required
|
| 2185 |
+
};
|
| 2186 |
+
```
|
| 2187 |
+
|
| 2188 |
+
— *end example*]
|
| 2189 |
+
|
| 2190 |
+
#### Simple requirements <a id="expr.prim.req.simple">[[expr.prim.req.simple]]</a>
|
| 2191 |
+
|
| 2192 |
+
``` bnf
|
| 2193 |
+
simple-requirement:
|
| 2194 |
+
expression ';'
|
| 2195 |
+
```
|
| 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 [[expr.prop]]. — *end note*]
|
| 2202 |
+
|
| 2203 |
+
[*Example 1*:
|
| 2204 |
+
|
| 2205 |
+
``` cpp
|
| 2206 |
+
template<typename T> concept C =
|
| 2207 |
+
requires (T a, T b) {
|
| 2208 |
+
a + b; // C<T> is true if a + b is a valid expression
|
| 2209 |
+
};
|
| 2210 |
+
```
|
| 2211 |
+
|
| 2212 |
+
— *end example*]
|
| 2213 |
+
|
| 2214 |
+
A *requirement* that starts with a `requires` token is never interpreted
|
| 2215 |
+
as a *simple-requirement*.
|
| 2216 |
+
|
| 2217 |
+
[*Note 2*: This simplifies distinguishing between a
|
| 2218 |
+
*simple-requirement* and a *nested-requirement*. — *end note*]
|
| 2219 |
+
|
| 2220 |
+
#### Type requirements <a id="expr.prim.req.type">[[expr.prim.req.type]]</a>
|
| 2221 |
+
|
| 2222 |
+
``` bnf
|
| 2223 |
+
type-requirement:
|
| 2224 |
+
typename nested-name-specifierₒₚₜ type-name ';'
|
| 2225 |
+
```
|
| 2226 |
+
|
| 2227 |
+
A *type-requirement* asserts the validity of a type.
|
| 2228 |
+
|
| 2229 |
+
[*Note 1*: The enclosing *requires-expression* will evaluate to `false`
|
| 2230 |
+
if substitution of template arguments fails. — *end note*]
|
| 2231 |
+
|
| 2232 |
+
[*Example 1*:
|
| 2233 |
+
|
| 2234 |
+
``` cpp
|
| 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 class template specialization
|
| 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 [[basic.types]].
|
| 2249 |
+
|
| 2250 |
+
#### Compound requirements <a id="expr.prim.req.compound">[[expr.prim.req.compound]]</a>
|
| 2251 |
+
|
| 2252 |
+
``` bnf
|
| 2253 |
+
compound-requirement:
|
| 2254 |
+
'{' expression '}' noexceptₒₚₜ return-type-requirementₒₚₜ ';'
|
| 2255 |
+
```
|
| 2256 |
+
|
| 2257 |
+
``` bnf
|
| 2258 |
+
return-type-requirement:
|
| 2259 |
+
'->' type-constraint
|
| 2260 |
+
```
|
| 2261 |
+
|
| 2262 |
+
A *compound-requirement* asserts properties of the *expression* E.
|
| 2263 |
+
Substitution of template arguments (if any) and verification of semantic
|
| 2264 |
+
properties proceed in the following order:
|
| 2265 |
+
|
| 2266 |
+
- Substitution of template arguments (if any) into the *expression* is
|
| 2267 |
+
performed.
|
| 2268 |
+
- If the `noexcept` specifier is present, E shall not be a
|
| 2269 |
+
potentially-throwing expression [[except.spec]].
|
| 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;
|
| 2280 |
+
{ E2 } -> D<A₁, ⋯, Aₙ>;
|
| 2281 |
+
};
|
| 2282 |
+
```
|
| 2283 |
+
|
| 2284 |
+
is equivalent to
|
| 2285 |
+
``` cpp
|
| 2286 |
+
requires {
|
| 2287 |
+
E1; requires C<decltype((E1))>;
|
| 2288 |
+
E2; requires D<decltype((E2)), A₁, ⋯, Aₙ>;
|
| 2289 |
+
};
|
| 2290 |
+
```
|
| 2291 |
+
|
| 2292 |
+
(including in the case where n is zero).
|
| 2293 |
+
— *end example*]
|
| 2294 |
+
|
| 2295 |
+
[*Example 2*:
|
| 2296 |
+
|
| 2297 |
+
``` cpp
|
| 2298 |
+
template<typename T> concept C1 = requires(T x) {
|
| 2299 |
+
{x++};
|
| 2300 |
+
};
|
| 2301 |
+
```
|
| 2302 |
+
|
| 2303 |
+
The *compound-requirement* in `C1` requires that `x++` is a valid
|
| 2304 |
+
expression. It is equivalent to the *simple-requirement* `x++;`.
|
| 2305 |
+
|
| 2306 |
+
``` cpp
|
| 2307 |
+
template<typename T> concept C2 = requires(T x) {
|
| 2308 |
+
{*x} -> std::same_as<typename T::inner>;
|
| 2309 |
+
};
|
| 2310 |
+
```
|
| 2311 |
+
|
| 2312 |
+
The *compound-requirement* in `C2` requires that `*x` is a valid
|
| 2313 |
+
expression, that `typename T::inner` is a valid type, and that
|
| 2314 |
+
`std::same_as<decltype((*x)), typename T::inner>` is satisfied.
|
| 2315 |
+
|
| 2316 |
+
``` cpp
|
| 2317 |
+
template<typename T> concept C3 =
|
| 2318 |
+
requires(T x) {
|
| 2319 |
+
{g(x)} noexcept;
|
| 2320 |
+
};
|
| 2321 |
+
```
|
| 2322 |
+
|
| 2323 |
+
The *compound-requirement* in `C3` requires that `g(x)` is a valid
|
| 2324 |
+
expression and that `g(x)` is non-throwing.
|
| 2325 |
+
|
| 2326 |
+
— *end example*]
|
| 2327 |
+
|
| 2328 |
+
#### Nested requirements <a id="expr.prim.req.nested">[[expr.prim.req.nested]]</a>
|
| 2329 |
+
|
| 2330 |
+
``` bnf
|
| 2331 |
+
nested-requirement:
|
| 2332 |
+
requires constraint-expression ';'
|
| 2333 |
+
```
|
| 2334 |
+
|
| 2335 |
+
A *nested-requirement* can be used to specify additional constraints in
|
| 2336 |
+
terms of local parameters. The *constraint-expression* shall be
|
| 2337 |
+
satisfied [[temp.constr.decl]] by the substituted template arguments, if
|
| 2338 |
+
any. Substitution of template arguments into a *nested-requirement* does
|
| 2339 |
+
not result in substitution into the *constraint-expression* other than
|
| 2340 |
+
as specified in [[temp.constr.constr]].
|
| 2341 |
+
|
| 2342 |
+
[*Example 1*:
|
| 2343 |
+
|
| 2344 |
+
``` cpp
|
| 2345 |
+
template<typename U> concept C = sizeof(U) == 1;
|
| 2346 |
+
|
| 2347 |
+
template<typename T> concept D = requires (T t) {
|
| 2348 |
+
requires C<decltype (+t)>;
|
| 2349 |
+
};
|
| 2350 |
+
```
|
| 2351 |
+
|
| 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:
|
|
|
|
| 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 ')'
|
| 2395 |
+
typeid '(' type-id ')'
|
| 2396 |
```
|
| 2397 |
|
| 2398 |
``` bnf
|
| 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` may be the product of
|
| 2405 |
+
replacing a `>{>}` token by two consecutive `>` tokens
|
| 2406 |
+
[[temp.names]]. — *end note*]
|
| 2407 |
|
| 2408 |
+
#### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 2409 |
|
| 2410 |
A postfix expression followed by an expression in square brackets is a
|
| 2411 |
postfix expression. One of the expressions shall be a glvalue of type
|
| 2412 |
“array of `T`” or a prvalue of type “pointer to `T`” and the other shall
|
| 2413 |
be a prvalue of unscoped enumeration or integral type. The result is of
|
| 2414 |
type “`T`”. The type “`T`” shall be a completely-defined object
|
| 2415 |
+
type.[^11] The expression `E1[E2]` is identical (by definition) to
|
| 2416 |
+
`*((E1)+(E2))`, except that in the case of an array operand, the result
|
| 2417 |
+
is an lvalue if that operand is an lvalue and an xvalue otherwise. The
|
| 2418 |
+
expression `E1` is sequenced before the expression `E2`.
|
| 2419 |
|
| 2420 |
+
[*Note 1*: A comma expression [[expr.comma]] appearing as the
|
| 2421 |
+
*expr-or-braced-init-list* of a subscripting expression is deprecated;
|
| 2422 |
+
see [[depr.comma.subscript]]. — *end note*]
|
| 2423 |
|
| 2424 |
+
[*Note 2*: Despite its asymmetric appearance, subscripting is a
|
| 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.
|
| 2437 |
+
|
| 2438 |
+
[*Note 1*: If the postfix expression is a function or member function
|
| 2439 |
+
name, the appropriate function and the validity of the call are
|
| 2440 |
+
determined according to the rules in [[over.match]]. — *end note*]
|
| 2441 |
+
|
| 2442 |
The postfix expression shall have function type or function pointer
|
| 2443 |
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 3*: The dynamic type is the type of the object referred to by
|
| 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 4*: If a function or member function name is used, and name
|
| 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. This return type shall be an object type, a reference type or
|
| 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 |
+
When a function is called, each parameter [[dcl.fct]] is initialized (
|
| 2494 |
+
[[dcl.init]], [[class.copy.ctor]]) with its corresponding argument. If
|
| 2495 |
+
there is no corresponding argument, the default argument for the
|
| 2496 |
+
parameter is used.
|
| 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 a non-static member function, the `this` parameter of
|
| 2508 |
+
the function [[class.this]] is initialized with a pointer to the object
|
| 2509 |
+
of the call, converted as if by an explicit type conversion
|
| 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*]
|
| 2516 |
|
| 2517 |
+
When a function is called, the type of any parameter shall not be a
|
| 2518 |
+
class type that is either incomplete or abstract.
|
| 2519 |
|
| 2520 |
+
[*Note 6*: This still allows a parameter to be a pointer or reference
|
| 2521 |
+
to such a type. However, it prevents a passed-by-value parameter to have
|
| 2522 |
+
an incomplete or abstract class type. — *end note*]
|
| 2523 |
|
| 2524 |
It is *implementation-defined* whether the lifetime of a parameter ends
|
| 2525 |
when the function in which it is defined returns or at the end of the
|
| 2526 |
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 scope of the calling function; in
|
| 2533 |
+
particular, if the function called has a *function-try-block*
|
| 2534 |
+
[[except.pre]] with a handler that could handle the exception, this
|
| 2535 |
+
handler is not considered. — *end example*]
|
| 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
|
| 2541 |
parameter.
|
| 2542 |
|
| 2543 |
+
[*Note 7*: All side effects of argument evaluations are sequenced
|
| 2544 |
before the function is entered (see
|
| 2545 |
[[intro.execution]]). — *end note*]
|
| 2546 |
|
| 2547 |
+
[*Example 3*:
|
| 2548 |
|
| 2549 |
``` cpp
|
| 2550 |
void f() {
|
| 2551 |
std::string s = "but I have heard it works even if you don't believe in it";
|
| 2552 |
s.replace(0, 4, "").replace(s.find("even"), 4, "only").replace(s.find(" don't"), 6, "");
|
|
|
|
| 2554 |
}
|
| 2555 |
```
|
| 2556 |
|
| 2557 |
— *end example*]
|
| 2558 |
|
| 2559 |
+
[*Note 8*: If an operator function is invoked using operator notation,
|
| 2560 |
argument evaluation is sequenced as specified for the built-in operator;
|
| 2561 |
see [[over.match.oper]]. — *end note*]
|
| 2562 |
|
| 2563 |
+
[*Example 4*:
|
| 2564 |
|
| 2565 |
``` cpp
|
| 2566 |
struct S {
|
| 2567 |
S(int);
|
| 2568 |
};
|
|
|
|
| 2576 |
[[expr.shift]]), but it is unspecified whether the value of `j` is 1 or
|
| 2577 |
2.
|
| 2578 |
|
| 2579 |
— *end example*]
|
| 2580 |
|
| 2581 |
+
The result of a function call is the result of the possibly-converted
|
| 2582 |
+
operand of the `return` statement [[stmt.return]] that transferred
|
| 2583 |
+
control out of the called function (if any), except in a virtual
|
| 2584 |
+
function call if the return type of the final overrider is different
|
| 2585 |
+
from the return type of the statically chosen function, the value
|
| 2586 |
+
returned from the final overrider is converted to the return type of the
|
| 2587 |
+
statically chosen function.
|
| 2588 |
|
| 2589 |
+
[*Note 9*: A function can change the values of its non-const
|
| 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 ([[dcl.type]], [[lex.literal]], [[lex.string]],
|
| 2596 |
+
[[dcl.array]], [[class.temporary]]). In addition, it is possible to
|
| 2597 |
modify the values of non-constant objects through pointer
|
| 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]].
|
| 2604 |
|
| 2605 |
+
[*Note 10*: This implies that, except where the ellipsis (`...`) or a
|
| 2606 |
function parameter pack is used, a parameter is available for each
|
| 2607 |
argument. — *end note*]
|
| 2608 |
|
| 2609 |
When there is no parameter for a given argument, the argument is passed
|
| 2610 |
in such a way that the receiving function can obtain the value of the
|
| 2611 |
+
argument by invoking `va_arg` [[support.runtime]].
|
| 2612 |
|
| 2613 |
+
[*Note 11*: This paragraph does not apply to arguments passed to a
|
| 2614 |
function parameter pack. Function parameter packs are expanded during
|
| 2615 |
+
template instantiation [[temp.variadic]], thus each such argument has a
|
| 2616 |
+
corresponding parameter when a function template specialization is
|
| 2617 |
actually called. — *end note*]
|
| 2618 |
|
| 2619 |
+
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 2620 |
+
function-to-pointer [[conv.func]] standard conversions are performed on
|
| 2621 |
+
the argument expression. An argument that has type cv `std::nullptr_t`
|
| 2622 |
+
is converted to type `void*` [[conv.ptr]]. After these conversions, if
|
| 2623 |
+
the argument does not have arithmetic, enumeration, pointer,
|
| 2624 |
+
pointer-to-member, or class type, the program is ill-formed. Passing a
|
| 2625 |
+
potentially-evaluated argument of a scoped enumeration type or of a
|
| 2626 |
+
class type [[class]] having an eligible non-trivial copy constructor, an
|
| 2627 |
+
eligible non-trivial move constructor, or a non-trivial destructor
|
| 2628 |
+
[[special]], with no corresponding parameter, is conditionally-supported
|
| 2629 |
+
with *implementation-defined* semantics. If the argument has integral or
|
| 2630 |
+
enumeration type that is subject to the integral promotions
|
| 2631 |
+
[[conv.prom]], or a floating-point type that is subject to the
|
| 2632 |
+
floating-point promotion [[conv.fpprom]], the value of the argument is
|
| 2633 |
+
converted to the promoted type before the call. These promotions are
|
| 2634 |
referred to as the *default argument promotions*.
|
| 2635 |
|
| 2636 |
+
Recursive calls are permitted, except to the `main` function
|
| 2637 |
+
[[basic.start.main]].
|
| 2638 |
|
| 2639 |
A function call is an lvalue if the result type is an lvalue reference
|
| 2640 |
type or an rvalue reference to function type, an xvalue if the result
|
| 2641 |
type is an rvalue reference to object type, and a prvalue otherwise.
|
| 2642 |
|
| 2643 |
+
#### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 2644 |
|
| 2645 |
+
A *simple-type-specifier* [[dcl.type.simple]] or *typename-specifier*
|
| 2646 |
+
[[temp.res]] followed by a parenthesized optional *expression-list* or
|
| 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 the specified type that performs no initialization.
|
| 2658 |
Otherwise, the expression is a prvalue of the specified type whose
|
| 2659 |
+
result object is direct-initialized [[dcl.init]] with the initializer.
|
| 2660 |
+
If the initializer is a parenthesized optional *expression-list*, the
|
| 2661 |
+
specified type shall not be an array type.
|
| 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` [[temp.names]], and then followed by
|
| 2667 |
+
an *id-expression*, is a postfix expression. The postfix expression
|
| 2668 |
+
before the dot or arrow is evaluated;[^12] the result of that
|
| 2669 |
+
evaluation, together with the *id-expression*, determines the result of
|
| 2670 |
+
the entire postfix expression.
|
| 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 an lvalue of type “function of ()
|
| 2682 |
+
returning `void`”.
|
| 2683 |
+
|
| 2684 |
+
[*Note 1*: This value can only be used for a notional function call
|
| 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 2*: If the class is incomplete, lookup in the complete class
|
| 2692 |
+
type is required to refer to the same declaration
|
| 2693 |
+
[[basic.scope.class]]. — *end note*]
|
| 2694 |
+
|
| 2695 |
+
The *id-expression* shall name a member of the class or of one of its
|
| 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; the type of `E1.E2` is `T`. Otherwise, one of the following
|
| 2714 |
rules applies.
|
| 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*
|
| 2720 |
`X`”, and the type of `E2` is “*cq2 vq2* `T`”, the expression
|
| 2721 |
+
designates the corresponding member subobject of the object designated
|
| 2722 |
+
by the first expression. If `E1` is an lvalue, then `E1.E2` is an
|
| 2723 |
+
lvalue; otherwise `E1.E2` is an xvalue. Let the notation *vq12* stand
|
| 2724 |
+
for the “union” of *vq1* and *vq2*; that is, if *vq1* or *vq2* is
|
| 2725 |
+
`volatile`, then *vq12* is `volatile`. Similarly, let the notation
|
| 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 a (possibly overloaded) member function, function overload
|
| 2732 |
+
resolution [[over.match]] is used to select the function to which `E2`
|
| 2733 |
+
refers. The type of `E1.E2` is the type of `E2` and `E1.E2` refers to
|
| 2734 |
+
the function referred to by `E2`.
|
| 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. The type of `E1.E2` is `T`.
|
| 2744 |
|
| 2745 |
If `E2` is a non-static data member or a non-static member function, the
|
| 2746 |
program is ill-formed if the class of which `E2` is directly a member is
|
| 2747 |
+
an ambiguous base [[class.member.lookup]] of the naming class
|
| 2748 |
+
[[class.access.base]] of `E2`.
|
| 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
|
| 2759 |
+
value. — *end note*]
|
| 2760 |
|
| 2761 |
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 2762 |
be an arithmetic type other than cv `bool`, or a pointer to a complete
|
| 2763 |
+
object type. An operand with volatile-qualified type is deprecated; see
|
| 2764 |
+
[[depr.volatile.type]]. The value of the operand object is modified
|
| 2765 |
+
[[defns.access]] by adding `1` to it. The value computation of the `++`
|
| 2766 |
+
expression is sequenced before the modification of the operand object.
|
| 2767 |
+
With respect to an indeterminately-sequenced function call, the
|
| 2768 |
+
operation of postfix `++` is a single evaluation.
|
| 2769 |
|
| 2770 |
+
[*Note 2*: Therefore, a function call cannot intervene between the
|
| 2771 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 2772 |
+
single postfix `++` operator. — *end note*]
|
| 2773 |
|
| 2774 |
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
|
|
|
|
| 2781 |
`++` operator.
|
| 2782 |
|
| 2783 |
[*Note 3*: For prefix increment and decrement, see
|
| 2784 |
[[expr.pre.incr]]. — *end note*]
|
| 2785 |
|
| 2786 |
+
#### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 2787 |
|
| 2788 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 2789 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 2790 |
+
reference to a complete class type, or “pointer to cv `void`”. The
|
| 2791 |
+
`dynamic_cast` operator shall not cast away constness
|
| 2792 |
+
[[expr.const.cast]].
|
| 2793 |
|
| 2794 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 2795 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 2796 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 2797 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 2798 |
is an rvalue reference type, `v` shall be a glvalue having a complete
|
| 2799 |
class type, and the result is an xvalue of the type referred to by `T`.
|
| 2800 |
|
| 2801 |
+
If the type of `v` is the same as `T` (ignoring cv-qualifications), the
|
| 2802 |
+
result is `v` (converted if necessary).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2803 |
|
| 2804 |
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] In both the pointer and reference cases,
|
| 2811 |
+
the program is ill-formed if `B` is an inaccessible or ambiguous base
|
| 2812 |
+
class of `D`.
|
| 2813 |
|
| 2814 |
[*Example 1*:
|
| 2815 |
|
| 2816 |
``` cpp
|
| 2817 |
struct B { };
|
|
|
|
| 2821 |
}
|
| 2822 |
```
|
| 2823 |
|
| 2824 |
— *end example*]
|
| 2825 |
|
| 2826 |
+
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic type
|
| 2827 |
+
[[class.virtual]].
|
| 2828 |
|
| 2829 |
+
If `v` is a null pointer value, the result is a null pointer value.
|
| 2830 |
+
|
| 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 |
If `C` is the class type to which `T` points or refers, the runtime
|
| 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
|
| 2842 |
+
(referred) to by `v` the result points (refers) to that `C` object.
|
| 2843 |
+
- Otherwise, if `v` points (refers) to a public base class subobject of
|
| 2844 |
+
the most derived object, and the type of the most derived object has a
|
| 2845 |
+
base class, of type `C`, that is unambiguous and public, the result
|
| 2846 |
+
points (refers) to the `C` subobject of the most derived object.
|
|
|
|
|
|
|
| 2847 |
- Otherwise, the runtime check *fails*.
|
| 2848 |
|
| 2849 |
The value of a failed cast to pointer type is the null pointer value of
|
| 2850 |
the required result type. A failed cast to reference type throws an
|
| 2851 |
+
exception [[except.throw]] of a type that would match a handler
|
| 2852 |
+
[[except.handle]] of type `std::bad_cast` [[bad.cast]].
|
| 2853 |
|
| 2854 |
[*Example 2*:
|
| 2855 |
|
| 2856 |
``` cpp
|
| 2857 |
class A { virtual void f(); };
|
|
|
|
| 2872 |
class F : public E, public D { };
|
| 2873 |
void h() {
|
| 2874 |
F f;
|
| 2875 |
A* ap = &f; // succeeds: finds unique A
|
| 2876 |
D* dp = dynamic_cast<D*>(ap); // fails: yields null; f has two D subobjects
|
| 2877 |
+
E* ep = (E*)ap; // error: cast from virtual base
|
| 2878 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 2879 |
}
|
| 2880 |
```
|
| 2881 |
|
| 2882 |
— *end example*]
|
| 2883 |
|
| 2884 |
+
[*Note 1*: Subclause [[class.cdtor]] describes the behavior of a
|
| 2885 |
+
`dynamic_cast` applied to an object under construction or
|
| 2886 |
+
destruction. — *end note*]
|
| 2887 |
|
| 2888 |
+
#### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 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] The lifetime of the object referred to
|
| 2895 |
+
by the lvalue extends to the end of the program. Whether or not the
|
| 2896 |
+
destructor is called for the `std::type_info` object at the end of the
|
| 2897 |
+
program is unspecified.
|
| 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] and the
|
| 2904 |
+
pointer is a null pointer value [[basic.compound]], the `typeid`
|
| 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 |
+
[[expr.prop]].
|
| 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. If the type of
|
| 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
|
| 2929 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 2930 |
representing the cv-unqualified type.
|
| 2931 |
|
| 2932 |
[*Example 1*:
|
|
|
|
| 2942 |
typeid(D) == typeid(const D&); // yields true
|
| 2943 |
```
|
| 2944 |
|
| 2945 |
— *end example*]
|
| 2946 |
|
| 2947 |
+
If the header `<typeinfo>` is not imported or included prior to a use of
|
| 2948 |
+
`typeid`, the program is ill-formed.
|
| 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>
|
| 2954 |
|
| 2955 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 2956 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 2957 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 2958 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 2959 |
otherwise, the result is a prvalue. The `static_cast` operator shall not
|
| 2960 |
+
cast away constness [[expr.const.cast]].
|
| 2961 |
|
| 2962 |
An lvalue of type “*cv1* `B`”, where `B` is a class type, can be cast to
|
| 2963 |
+
type “reference to *cv2* `D`”, where `D` is a class derived
|
| 2964 |
+
[[class.derived]] from `B`, if *cv2* is the same cv-qualification as, or
|
| 2965 |
+
greater cv-qualification than, *cv1*. If `B` is a virtual base class of
|
| 2966 |
+
`D` or a base class of a virtual base class of `D`, or if no valid
|
| 2967 |
+
standard conversion from “pointer to `D`” to “pointer to `B`” exists
|
| 2968 |
+
[[conv.ptr]], the program is ill-formed. An xvalue of type “*cv1* `B`”
|
| 2969 |
can be cast to type “rvalue reference to *cv2* `D`” with the same
|
| 2970 |
constraints as for an lvalue of type “*cv1* `B`”. If the object of type
|
| 2971 |
“*cv1* `B`” is actually a base class subobject of an object of type `D`,
|
| 2972 |
the result refers to the enclosing object of type `D`. Otherwise, the
|
| 2973 |
behavior is undefined.
|
|
|
|
| 2978 |
struct B { };
|
| 2979 |
struct D : public B { };
|
| 2980 |
D d;
|
| 2981 |
B &br = d;
|
| 2982 |
|
| 2983 |
+
static_cast<D&>(br); // produces lvalue denoting the original d object
|
| 2984 |
```
|
| 2985 |
|
| 2986 |
— *end example*]
|
| 2987 |
|
| 2988 |
An lvalue of type “*cv1* `T1`” can be cast to type “rvalue reference to
|
| 2989 |
+
*cv2* `T2`” if “*cv2* `T2`” is reference-compatible with “*cv1* `T1`”
|
| 2990 |
+
[[dcl.init.ref]]. If the value is not a bit-field, the result refers to
|
| 2991 |
the object or the specified base class subobject thereof; otherwise, the
|
| 2992 |
+
lvalue-to-rvalue conversion [[conv.lval]] is applied to the bit-field
|
| 2993 |
and the resulting prvalue is used as the *expression* of the
|
| 2994 |
+
`static_cast` for the remainder of this subclause. If `T2` is an
|
| 2995 |
+
inaccessible [[class.access]] or ambiguous [[class.member.lookup]] base
|
| 2996 |
+
class of `T1`, a program that necessitates such a cast is ill-formed.
|
|
|
|
| 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
|
| 3002 |
+
function [[over.match.viable]], or if `T` is an aggregate type
|
| 3003 |
+
[[dcl.init.aggr]] having a first element `x` and there is an implicit
|
| 3004 |
+
conversion sequence from E to the type of `x`. If `T` is a reference
|
| 3005 |
+
type, the effect is the same as performing the declaration and
|
| 3006 |
+
initialization
|
| 3007 |
|
| 3008 |
``` cpp
|
| 3009 |
+
T t(E);
|
| 3010 |
```
|
| 3011 |
|
| 3012 |
+
for some invented temporary variable `t` [[dcl.init]] and then using the
|
| 3013 |
+
temporary variable as the result of the conversion. Otherwise, the
|
| 3014 |
+
result object is direct-initialized from E.
|
| 3015 |
|
| 3016 |
[*Note 1*: The conversion is ill-formed when attempting to convert an
|
| 3017 |
expression of class type to an inaccessible or ambiguous base
|
| 3018 |
class. — *end note*]
|
| 3019 |
|
| 3020 |
+
[*Note 2*: If `T` is “array of unknown bound of `U`”, this
|
| 3021 |
+
direct-initialization defines the type of the expression as
|
| 3022 |
+
`U[1]`. — *end note*]
|
| 3023 |
+
|
| 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 it becomes a discarded-value expression [[expr.prop]].
|
| 3030 |
|
| 3031 |
+
[*Note 3*: However, if the value is in a temporary object
|
| 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
|
| 3037 |
+
an lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]],
|
| 3038 |
+
function-to-pointer [[conv.func]], null pointer [[conv.ptr]], null
|
| 3039 |
+
member pointer [[conv.mem]], boolean [[conv.bool]], or function pointer
|
| 3040 |
+
[[conv.fctptr]] conversion, can be performed explicitly using
|
| 3041 |
+
`static_cast`. A program is ill-formed if it uses `static_cast` to
|
| 3042 |
+
perform the inverse of an ill-formed standard conversion sequence.
|
|
|
|
| 3043 |
|
| 3044 |
[*Example 2*:
|
| 3045 |
|
| 3046 |
``` cpp
|
| 3047 |
struct B { };
|
|
|
|
| 3052 |
}
|
| 3053 |
```
|
| 3054 |
|
| 3055 |
— *end example*]
|
| 3056 |
|
| 3057 |
+
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 3058 |
+
function-to-pointer [[conv.func]] conversions are applied to the
|
| 3059 |
+
operand. Such a `static_cast` is subject to the restriction that the
|
| 3060 |
+
explicit conversion does not cast away constness [[expr.const.cast]],
|
| 3061 |
+
and the following additional rules for specific cases:
|
|
|
|
| 3062 |
|
| 3063 |
+
A value of a scoped enumeration type [[dcl.enum]] can be explicitly
|
| 3064 |
+
converted to an integral type; the result is the same as that of
|
| 3065 |
+
converting to the enumeration’s underlying type and then to the
|
| 3066 |
+
destination type. A value of a scoped enumeration type can also be
|
| 3067 |
+
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 |
+
conversion, if necessary, and then to the enumeration type. If the
|
| 3074 |
+
enumeration type does not have a fixed underlying type, the value is
|
| 3075 |
+
unchanged if the original value is within the range of the enumeration
|
| 3076 |
+
values [[dcl.enum]], and otherwise, the behavior is undefined. A value
|
| 3077 |
+
of floating-point type can also be explicitly converted to an
|
| 3078 |
+
enumeration type. The resulting value is the same as converting the
|
| 3079 |
+
original value to the underlying type of the enumeration [[conv.fpint]],
|
| 3080 |
+
and subsequently to the enumeration type.
|
| 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 subobject of an object of type
|
| 3092 |
`D`, the resulting pointer points to the enclosing object of type `D`.
|
| 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 4*: Function types (including those used in
|
| 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
|
| 3107 |
+
program is ill-formed. The null member pointer value [[conv.mem]] is
|
| 3108 |
+
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 5*: Although class `B` need not contain the original member, the
|
| 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
|
|
|
|
| 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` (ignoring cv-qualification) that is
|
| 3127 |
+
pointer-interconvertible [[basic.compound]] with *a*, the result is a
|
| 3128 |
pointer to *b*. Otherwise, the pointer value is unchanged by the
|
| 3129 |
conversion.
|
| 3130 |
|
| 3131 |
[*Example 3*:
|
| 3132 |
|
|
|
|
| 3136 |
bool b = p1 == p2; // b will have the value true.
|
| 3137 |
```
|
| 3138 |
|
| 3139 |
— *end example*]
|
| 3140 |
|
| 3141 |
+
#### Reinterpret cast <a id="expr.reinterpret.cast">[[expr.reinterpret.cast]]</a>
|
| 3142 |
|
| 3143 |
The result of the expression `reinterpret_cast<T>(v)` is the result of
|
| 3144 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 3145 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 3146 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 3147 |
+
otherwise, the result is a prvalue and the lvalue-to-rvalue
|
| 3148 |
+
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
| 3149 |
+
[[conv.func]] standard conversions are performed on the expression `v`.
|
| 3150 |
+
Conversions that can be performed explicitly using `reinterpret_cast`
|
| 3151 |
+
are listed below. No other conversion can be performed explicitly using
|
| 3152 |
+
`reinterpret_cast`.
|
| 3153 |
|
| 3154 |
+
The `reinterpret_cast` operator shall not cast away constness
|
| 3155 |
+
[[expr.const.cast]]. An expression of integral, enumeration, pointer, or
|
| 3156 |
+
pointer-to-member type can be explicitly converted to its own type; such
|
| 3157 |
+
a cast yields the value of its operand.
|
| 3158 |
|
| 3159 |
[*Note 1*: The mapping performed by `reinterpret_cast` might, or might
|
| 3160 |
not, produce a representation different from the original
|
| 3161 |
value. — *end note*]
|
| 3162 |
|
| 3163 |
A pointer can be explicitly converted to any integral type large enough
|
| 3164 |
+
to hold all values of its type. The mapping function is
|
| 3165 |
+
*implementation-defined*.
|
| 3166 |
|
| 3167 |
[*Note 2*: It is intended to be unsurprising to those who know the
|
| 3168 |
addressing structure of the underlying machine. — *end note*]
|
| 3169 |
|
| 3170 |
A value of type `std::nullptr_t` can be converted to an integral type;
|
|
|
|
| 3186 |
|
| 3187 |
A function pointer can be explicitly converted to a function pointer of
|
| 3188 |
a different type.
|
| 3189 |
|
| 3190 |
[*Note 5*: The effect of calling a function through a pointer to a
|
| 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 6*: See also [[conv.ptr]] for more details of pointer
|
| 3200 |
conversions. — *end note*]
|
| 3201 |
|
| 3202 |
An object pointer can be explicitly converted to an object pointer of a
|
| 3203 |
+
different type.[^17] When a prvalue `v` of object pointer type is
|
| 3204 |
converted to the object pointer type “pointer to cv `T`”, the result is
|
| 3205 |
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 3206 |
|
| 3207 |
[*Note 7*: Converting a prvalue of type “pointer to `T1`” to the type
|
| 3208 |
“pointer to `T2`” (where `T1` and `T2` are object types and where the
|
|
|
|
| 3215 |
*implementation-defined*, except that if an implementation supports
|
| 3216 |
conversions in both directions, converting a prvalue of one type to the
|
| 3217 |
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 8*: A null pointer constant of type `std::nullptr_t` cannot be
|
| 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] The null member pointer value [[conv.mem]] is
|
| 3232 |
converted to the null member pointer value of the destination type. The
|
| 3233 |
result of this conversion is unspecified, except in the following cases:
|
| 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
|
| 3239 |
`T1`” to the type “pointer to data member of `Y` of type `T2`” (where
|
| 3240 |
the alignment requirements of `T2` are no stricter than those of `T1`)
|
| 3241 |
+
and back to its original type yields the original pointer-to-member
|
| 3242 |
value.
|
| 3243 |
|
| 3244 |
+
A glvalue of type `T1`, designating an object *x*, can be cast to the
|
| 3245 |
+
type “reference to `T2`” if an expression of type “pointer to `T1`” can
|
| 3246 |
+
be explicitly converted to the type “pointer to `T2`” using a
|
| 3247 |
+
`reinterpret_cast`. The result is that of `*reinterpret_cast<T2 *>(p)`
|
| 3248 |
+
where `p` is a pointer to *x* of type “pointer to `T1`”. No temporary is
|
| 3249 |
+
created, no copy is made, and no constructors [[class.ctor]] or
|
| 3250 |
+
conversion functions [[class.conv]] are called. [^19]
|
| 3251 |
|
| 3252 |
+
#### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3253 |
|
| 3254 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 3255 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
| 3256 |
is an rvalue reference to object type, the result is an xvalue;
|
| 3257 |
+
otherwise, the result is a prvalue and the lvalue-to-rvalue
|
| 3258 |
+
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
| 3259 |
+
[[conv.func]] standard conversions are performed on the expression `v`.
|
| 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 |
may be cast to its own type using a `const_cast`
|
| 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 cv-decompositions of both types, each P¹ᵢ is 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
|
| 3277 |
typedef int *A[3]; // array of 3 pointer to int
|
| 3278 |
typedef const int *const CA[3]; // array of 3 const pointer to const int
|
| 3279 |
|
| 3280 |
+
CA &&r = A{}; // OK, reference binds to temporary array object
|
| 3281 |
+
// after qualification conversion to type CA
|
| 3282 |
A &&r1 = const_cast<A>(CA{}); // error: temporary array decayed to pointer
|
| 3283 |
A &&r2 = const_cast<A&&>(CA{}); // OK
|
| 3284 |
```
|
| 3285 |
|
| 3286 |
— *end example*]
|
|
|
|
| 3296 |
- if `T1` is a class type, a prvalue of type `T1` can be explicitly
|
| 3297 |
converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
|
| 3298 |
|
| 3299 |
The result of a reference `const_cast` refers to the original object if
|
| 3300 |
the operand is a glvalue and to the result of applying the temporary
|
| 3301 |
+
materialization conversion [[conv.rval]] otherwise.
|
| 3302 |
|
| 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*: Depending on the type of the object, a write operation
|
| 3309 |
through the pointer, lvalue or pointer to data member resulting from a
|
| 3310 |
+
`const_cast` that casts away a const-qualifier[^20] may produce
|
| 3311 |
+
undefined behavior [[dcl.type.cv]]. — *end note*]
|
| 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 cv-decomposition [[conv.qual]]
|
| 3315 |
+
of `T1` yielding *n* such that `T2` has a cv-decomposition of the form
|
|
|
|
| 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
|
|
|
|
| 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 |
'-{-}' cast-expression
|
| 3344 |
+
await-expression
|
| 3345 |
+
sizeof unary-expression
|
| 3346 |
+
sizeof '(' type-id ')'
|
| 3347 |
+
sizeof '...' '(' identifier ')'
|
| 3348 |
+
alignof '(' type-id ')'
|
| 3349 |
noexcept-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*: the expression to which
|
| 3362 |
it is applied shall be a pointer to an object type, or a pointer to a
|
| 3363 |
function type and the result is an lvalue referring to the object or
|
| 3364 |
function to which the expression points. If the type of the expression
|
| 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 |
The result of each of the following unary operators is a prvalue.
|
| 3373 |
|
| 3374 |
+
The result of the unary `&` operator is a pointer to its operand.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3375 |
|
| 3376 |
+
- If the operand is a *qualified-id* naming a non-static or variant
|
| 3377 |
+
member `m` of some class `C` with type `T`, the result has type
|
| 3378 |
+
“pointer to member of class `C` of type `T`” and is a prvalue
|
| 3379 |
+
designating `C::m`.
|
| 3380 |
+
- Otherwise, if the operand is an lvalue of type `T`, the resulting
|
| 3381 |
+
expression is a prvalue of type “pointer to `T`” whose result is a
|
| 3382 |
+
pointer to the designated object [[intro.memory]] or function.
|
| 3383 |
+
\[*Note 2*: In particular, taking the address of a variable of type
|
| 3384 |
+
“cv `T`” yields a pointer of type “pointer to cv `T`”. — *end note*]
|
| 3385 |
+
- Otherwise, the program is ill-formed.
|
| 3386 |
|
| 3387 |
[*Example 1*:
|
| 3388 |
|
| 3389 |
``` cpp
|
| 3390 |
struct A { int i; };
|
|
|
|
| 3397 |
```
|
| 3398 |
|
| 3399 |
— *end example*]
|
| 3400 |
|
| 3401 |
[*Note 3*: A pointer to member formed from a `mutable` non-static data
|
| 3402 |
+
member [[dcl.stc]] does not reflect the `mutable` specifier associated
|
| 3403 |
+
with the non-static data member. — *end note*]
|
| 3404 |
|
| 3405 |
A pointer to member is only formed when an explicit `&` is used and its
|
| 3406 |
operand is a *qualified-id* not enclosed in parentheses.
|
| 3407 |
|
| 3408 |
[*Note 4*: That is, the expression `&(qualified-id)`, where the
|
| 3409 |
*qualified-id* is enclosed in parentheses, does not form an expression
|
| 3410 |
of type “pointer to member”. Neither does `qualified-id`, because there
|
| 3411 |
is no implicit conversion from a *qualified-id* for a non-static member
|
| 3412 |
function to the type “pointer to member function” as there is from an
|
| 3413 |
+
lvalue of function type to the type “pointer to function” [[conv.func]].
|
| 3414 |
+
Nor is `&unqualified-id` a pointer to member, even within the scope of
|
| 3415 |
+
the *unqualified-id*’s class. — *end note*]
|
| 3416 |
|
| 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 overloaded function [[over]] can be taken
|
| 3423 |
only in a context that uniquely determines which version of the
|
| 3424 |
+
overloaded function is referred to (see [[over.over]]). Since the
|
| 3425 |
+
context might determine whether the operand is a static or non-static
|
| 3426 |
+
member function, the context can also affect whether the expression has
|
| 3427 |
+
type “pointer to function” or “pointer to member
|
| 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.
|
|
|
|
| 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 enumeration type; the
|
| 3448 |
result is the ones’ complement of its operand. Integral promotions are
|
| 3449 |
performed. The type of the result is the type of the promoted operand.
|
| 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 unary complement operator rather than as the start
|
| 3453 |
of an *unqualified-id* naming a destructor.
|
| 3454 |
|
| 3455 |
[*Note 6*: Because the grammar does not permit an operator to follow
|
| 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>
|
| 3461 |
|
| 3462 |
+
The operand of prefix `++` is modified [[defns.access]] by adding `1`.
|
| 3463 |
+
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 3464 |
+
be an arithmetic type other than cv `bool`, or a pointer to a
|
| 3465 |
+
completely-defined object type. An operand with volatile-qualified type
|
| 3466 |
+
is deprecated; see [[depr.volatile.type]]. The result is the updated
|
| 3467 |
+
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 `\dcr` is modified [[defns.access]] by subtracting
|
| 3474 |
+
`1`. The requirements on the operand of prefix `\dcr` and the properties
|
| 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 |
|
| 3480 |
+
#### Await <a id="expr.await">[[expr.await]]</a>
|
| 3481 |
+
|
| 3482 |
+
The `co_await` expression is used to suspend evaluation of a coroutine
|
| 3483 |
+
[[dcl.fct.def.coroutine]] while awaiting completion of the computation
|
| 3484 |
+
represented by the operand expression.
|
| 3485 |
+
|
| 3486 |
+
``` bnf
|
| 3487 |
+
await-expression:
|
| 3488 |
+
'co_await' cast-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 a *for-init-statement*, an
|
| 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-scope
|
| 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 |
+
- *a* is the *cast-expression* if the *await-expression* was implicitly
|
| 3509 |
+
produced by a *yield-expression* [[expr.yield]], an initial suspend
|
| 3510 |
+
point, or a final suspend point [[dcl.fct.def.coroutine]]. Otherwise,
|
| 3511 |
+
the *unqualified-id* `await_transform` is looked up within the scope
|
| 3512 |
+
of `P` by class member access lookup [[basic.lookup.classref]], and if
|
| 3513 |
+
this lookup finds at least one declaration, then *a* is
|
| 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
|
| 3519 |
+
resolution is ambiguous, the program is ill-formed. If no viable
|
| 3520 |
+
functions are found, *o* is *a*. Otherwise, *o* is a call to the
|
| 3521 |
+
selected function with the argument *a*. If *o* would be a prvalue,
|
| 3522 |
+
the temporary materialization conversion [[conv.rval]] is applied.
|
| 3523 |
+
- *e* is an lvalue referring to the result of evaluating the
|
| 3524 |
+
(possibly-converted) *o*.
|
| 3525 |
+
- *h* is an object of type `std::coroutine_handle<P>` referring to the
|
| 3526 |
+
enclosing coroutine.
|
| 3527 |
+
- *await-ready* is the expression *e*`.await_ready()`, contextually
|
| 3528 |
+
converted to `bool`.
|
| 3529 |
+
- *await-suspend* is the expression *e*`.await_suspend(`*h*`)`, which
|
| 3530 |
+
shall be a prvalue of type `void`, `bool`, or
|
| 3531 |
+
`std::coroutine_handle<Z>` for some type `Z`.
|
| 3532 |
+
- *await-resume* is the expression *e*`.await_resume()`.
|
| 3533 |
+
|
| 3534 |
+
The *await-expression* has the same type and value category as the
|
| 3535 |
+
*await-resume* expression.
|
| 3536 |
+
|
| 3537 |
+
The *await-expression* evaluates the (possibly-converted) *o* expression
|
| 3538 |
+
and the *await-ready* expression, then:
|
| 3539 |
+
|
| 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 may be successively resumed in this fashion,
|
| 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 re-thrown [[except.throw]]. Otherwise, control flow
|
| 3555 |
+
returns to the current coroutine caller or resumer
|
| 3556 |
+
[[dcl.fct.def.coroutine]] without exiting any scopes [[stmt.jump]].
|
| 3557 |
+
- If the result of *await-ready* is `true`, or when the coroutine is
|
| 3558 |
+
resumed, the *await-resume* expression is evaluated, and its result is
|
| 3559 |
+
the result of the *await-expression*.
|
| 3560 |
+
|
| 3561 |
+
[*Example 1*:
|
| 3562 |
+
|
| 3563 |
+
``` cpp
|
| 3564 |
+
template <typename T>
|
| 3565 |
+
struct my_future {
|
| 3566 |
+
...
|
| 3567 |
+
bool await_ready();
|
| 3568 |
+
void await_suspend(std::coroutine_handle<>);
|
| 3569 |
+
T await_resume();
|
| 3570 |
+
};
|
| 3571 |
+
|
| 3572 |
+
template <class Rep, class Period>
|
| 3573 |
+
auto operator co_await(std::chrono::duration<Rep, Period> d) {
|
| 3574 |
+
struct awaiter {
|
| 3575 |
+
std::chrono::system_clock::duration duration;
|
| 3576 |
+
...
|
| 3577 |
+
awaiter(std::chrono::system_clock::duration d) : duration(d) {}
|
| 3578 |
+
bool await_ready() const { return duration.count() <= 0; }
|
| 3579 |
+
void await_resume() {}
|
| 3580 |
+
void await_suspend(std::coroutine_handle<> h) { ... }
|
| 3581 |
+
};
|
| 3582 |
+
return awaiter{d};
|
| 3583 |
+
}
|
| 3584 |
+
|
| 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 |
+
|
| 3596 |
+
auto f(int x = co_await h()); // error: await-expression outside of function suspension context
|
| 3597 |
+
int a[] = { co_await h() }; // error: await-expression outside of function suspension context
|
| 3598 |
+
```
|
| 3599 |
+
|
| 3600 |
+
— *end example*]
|
| 3601 |
+
|
| 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 |
+
[[expr.prop]], or a parenthesized *type-id*. The `sizeof` operator shall
|
| 3608 |
+
not be applied to an expression that has function or incomplete type, to
|
| 3609 |
+
the parenthesized name of such types, or to a glvalue that designates a
|
| 3610 |
+
bit-field. The result of `sizeof` applied to any of the narrow character
|
| 3611 |
+
types is `1`. The result of `sizeof` applied to any other fundamental
|
| 3612 |
+
type [[basic.fundamental]] is *implementation-defined*.
|
| 3613 |
|
| 3614 |
[*Note 1*: In particular, `sizeof(bool)`, `sizeof(char16_t)`,
|
| 3615 |
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 3616 |
+
implementation-defined.[^21] — *end note*]
|
| 3617 |
|
| 3618 |
+
[*Note 2*: See [[intro.memory]] for the definition of byte and
|
| 3619 |
+
[[basic.types]] for the definition of object
|
| 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. [^22] When applied to an array, the
|
| 3628 |
+
result is the total number of bytes in the array. This implies that the
|
| 3629 |
+
size of an array of n elements is n times the size of an element.
|
|
|
|
| 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.
|
| 3635 |
|
| 3636 |
+
The identifier in a `sizeof...` expression shall name a pack. The
|
| 3637 |
+
`sizeof...` operator yields the number of elements in the pack
|
| 3638 |
+
[[temp.variadic]]. A `sizeof...` expression is a pack expansion
|
| 3639 |
+
[[temp.variadic]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3640 |
|
| 3641 |
[*Example 1*:
|
| 3642 |
|
| 3643 |
``` cpp
|
| 3644 |
template<class... Types>
|
|
|
|
| 3647 |
};
|
| 3648 |
```
|
| 3649 |
|
| 3650 |
— *end example*]
|
| 3651 |
|
| 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>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 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
|
| 3663 |
+
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>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 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 [[expr.prop]], can throw an
|
| 3679 |
+
exception [[except.throw]].
|
| 3680 |
+
|
| 3681 |
+
``` bnf
|
| 3682 |
+
noexcept-expression:
|
| 3683 |
+
noexcept '(' expression ')'
|
| 3684 |
+
```
|
| 3685 |
+
|
| 3686 |
+
The result of the `noexcept` operator is a prvalue of type `bool`.
|
| 3687 |
+
|
| 3688 |
+
[*Note 1*: A *noexcept-expression* is an integral constant expression
|
| 3689 |
+
[[expr.const]]. — *end note*]
|
| 3690 |
+
|
| 3691 |
+
The result of the `noexcept` operator is `true` unless the *expression*
|
| 3692 |
+
is potentially-throwing [[except.spec]].
|
| 3693 |
+
|
| 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 or array thereof (
|
| 3700 |
+
[[intro.object]], [[basic.types]], [[class.abstract]]).
|
| 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* may be a cv-qualified type, in which case the
|
| 3706 |
object created by the *new-expression* has a cv-qualified
|
| 3707 |
type. — *end note*]
|
| 3708 |
|
| 3709 |
``` bnf
|
| 3710 |
new-expression:
|
| 3711 |
+
'::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 3712 |
+
'::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
| 3713 |
```
|
| 3714 |
|
| 3715 |
``` bnf
|
| 3716 |
new-placement:
|
| 3717 |
'(' expression-list ')'
|
|
|
|
| 3728 |
noptr-new-declarator
|
| 3729 |
```
|
| 3730 |
|
| 3731 |
``` bnf
|
| 3732 |
noptr-new-declarator:
|
| 3733 |
+
'[' expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
|
| 3734 |
noptr-new-declarator '[' constant-expression ']' attribute-specifier-seqₒₚₜ
|
| 3735 |
```
|
| 3736 |
|
| 3737 |
``` bnf
|
| 3738 |
new-initializer:
|
| 3739 |
'(' expression-listₒₚₜ ')'
|
| 3740 |
braced-init-list
|
| 3741 |
```
|
| 3742 |
|
| 3743 |
+
If a placeholder type [[dcl.spec.auto]] appears in the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3744 |
*type-specifier-seq* of a *new-type-id* or *type-id* of a
|
| 3745 |
*new-expression*, the allocated type is deduced as follows: Let *init*
|
| 3746 |
be the *new-initializer*, if any, and `T` be the *new-type-id* or
|
| 3747 |
*type-id* of the *new-expression*, then the allocated type is the type
|
| 3748 |
+
deduced for the variable `x` in the invented declaration
|
| 3749 |
+
[[dcl.spec.auto]]:
|
| 3750 |
|
| 3751 |
``` cpp
|
| 3752 |
T x init ;
|
| 3753 |
```
|
| 3754 |
|
|
|
|
| 3765 |
— *end example*]
|
| 3766 |
|
| 3767 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 3768 |
of *new-declarator*s.
|
| 3769 |
|
| 3770 |
+
[*Note 3*: This prevents ambiguities between the declarator operators
|
| 3771 |
`&`, `&&`, `*`, and `[]` and their expression
|
| 3772 |
counterparts. — *end note*]
|
| 3773 |
|
| 3774 |
[*Example 2*:
|
| 3775 |
|
|
|
|
| 3779 |
|
| 3780 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 3781 |
|
| 3782 |
— *end example*]
|
| 3783 |
|
| 3784 |
+
[*Note 4*:
|
| 3785 |
|
| 3786 |
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 3787 |
effects.
|
| 3788 |
|
| 3789 |
[*Example 3*:
|
|
|
|
| 3797 |
``` cpp
|
| 3798 |
(new int) (*[10])(); // error
|
| 3799 |
```
|
| 3800 |
|
| 3801 |
Instead, the explicitly parenthesized version of the `new` operator can
|
| 3802 |
+
be used to create objects of compound types [[basic.compound]]:
|
| 3803 |
|
| 3804 |
``` cpp
|
| 3805 |
new (int (*[10])());
|
| 3806 |
```
|
| 3807 |
|
|
|
|
| 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 |
|
|
|
|
| 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
|
| 3837 |
+
its value shall be greater than zero.
|
|
|
|
| 3838 |
|
| 3839 |
[*Example 4*: Given the definition `int n = 42`, `new float[n][5]` is
|
| 3840 |
well-formed (because `n` is the *expression* of a
|
| 3841 |
*noptr-new-declarator*), but `new float[5][n]` is ill-formed (because
|
| 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
|
| 3854 |
`std::size_t` is less than zero;
|
| 3855 |
- the expression is of class type and its value before application of
|
| 3856 |
+
the second standard conversion [[over.ics.user]][^23] is less than
|
| 3857 |
zero;
|
| 3858 |
- its value is such that the size of the allocated object would exceed
|
| 3859 |
+
the *implementation-defined* limit [[implimits]]; or
|
| 3860 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 3861 |
elements for which initializers are provided (including the
|
| 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 expression, the program is
|
| 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;
|
| 3874 |
- otherwise, the *new-expression* terminates by throwing an exception
|
| 3875 |
+
of a type that would match a handler [[except.handle]] of type
|
| 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
|
| 3886 |
+
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 ([[basic.stc.dynamic]],
|
| 3893 |
+
[[new.delete.single]], [[new.delete.array]]). A C++ program can provide
|
| 3894 |
+
alternative definitions of these functions [[replacement.functions]]
|
| 3895 |
+
and/or class-specific versions [[class.free]]. The set of allocation and
|
| 3896 |
+
deallocation functions that may be called by a *new-expression* may
|
| 3897 |
include functions that do not perform allocation or deallocation; for
|
| 3898 |
example, see [[new.delete.placement]]. — *end note*]
|
| 3899 |
|
| 3900 |
If the *new-expression* begins with a unary `::` operator, the
|
| 3901 |
allocation function’s name is looked up in the global scope. Otherwise,
|
|
|
|
| 3905 |
type, the allocation function’s name is looked up in the global scope.
|
| 3906 |
|
| 3907 |
An implementation is allowed to omit a call to a replaceable global
|
| 3908 |
allocation function ([[new.delete.single]], [[new.delete.array]]). When
|
| 3909 |
it does so, the storage is instead provided by the implementation or
|
| 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 |
+
|
| 3915 |
+
[*Note 8*: Only *new-expression*s that would otherwise result in a call
|
| 3916 |
+
to a replaceable global allocation function can be evaluated in constant
|
| 3917 |
+
expressions [[expr.const]]. — *end note*]
|
| 3918 |
+
|
| 3919 |
+
The implementation may extend the allocation of a *new-expression* `e1`
|
| 3920 |
+
to provide storage for a *new-expression* `e2` if the following would be
|
| 3921 |
true were the allocation not extended:
|
| 3922 |
|
| 3923 |
- the evaluation of `e1` is sequenced before the evaluation of `e2`, and
|
| 3924 |
- `e2` is evaluated whenever `e1` obtains storage, and
|
| 3925 |
- both `e1` and `e2` invoke the same replaceable global allocation
|
|
|
|
| 3934 |
`e1`.
|
| 3935 |
|
| 3936 |
[*Example 5*:
|
| 3937 |
|
| 3938 |
``` cpp
|
| 3939 |
+
void can_merge(int x) {
|
| 3940 |
// These allocations are safe for merging:
|
| 3941 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 3942 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
| 3943 |
std::unique_ptr<char[]> c{new (std::nothrow) char[x]};
|
| 3944 |
|
| 3945 |
g(a.get(), b.get(), c.get());
|
| 3946 |
}
|
| 3947 |
|
| 3948 |
+
void cannot_merge(int x) {
|
| 3949 |
std::unique_ptr<char[]> a{new char[8]};
|
| 3950 |
try {
|
| 3951 |
// Merging this allocation would change its catch handler.
|
| 3952 |
std::unique_ptr<char[]> b{new char[x]};
|
| 3953 |
} catch (const std::bad_alloc& e) {
|
|
|
|
| 3962 |
When a *new-expression* calls an allocation function and that allocation
|
| 3963 |
has not been extended, the *new-expression* passes the amount of space
|
| 3964 |
requested to the allocation function as the first argument of type
|
| 3965 |
`std::size_t`. That argument shall be no less than the size of the
|
| 3966 |
object being created; it may be greater than the size of the object
|
| 3967 |
+
being created only if the object is an array and the allocation function
|
| 3968 |
+
is not a non-allocating form [[new.delete.placement]]. For arrays of
|
| 3969 |
+
`char`, `unsigned char`, and `std::byte`, the difference between the
|
| 3970 |
+
result of the *new-expression* and the address returned by the
|
| 3971 |
+
allocation function shall be an integral multiple of the strictest
|
| 3972 |
+
fundamental alignment requirement [[basic.align]] of any object type
|
| 3973 |
+
whose size is no greater than the size of the array being created.
|
| 3974 |
|
| 3975 |
+
[*Note 9*: Because allocation functions are assumed to return pointers
|
| 3976 |
to storage that is appropriately aligned for objects of any type with
|
| 3977 |
fundamental alignment, this constraint on array allocation overhead
|
| 3978 |
permits the common idiom of allocating character arrays into which
|
| 3979 |
objects of other types will later be placed. — *end note*]
|
| 3980 |
|
|
|
|
| 3993 |
assembling an argument list. The first argument is the amount of space
|
| 3994 |
requested, and has type `std::size_t`. If the type of the allocated
|
| 3995 |
object has new-extended alignment, the next argument is the type’s
|
| 3996 |
alignment, and has type `std::align_val_t`. If the *new-placement*
|
| 3997 |
syntax is used, the *initializer-clause*s in its *expression-list* are
|
| 3998 |
+
the succeeding arguments. If no matching function is found then
|
| 3999 |
+
|
| 4000 |
+
- if the allocated object type has new-extended alignment, the alignment
|
| 4001 |
+
argument is removed from the argument list;
|
| 4002 |
+
- otherwise, an argument that is the type’s alignment and has type
|
| 4003 |
+
`std::align_val_t` is added into the argument list immediately after
|
| 4004 |
+
the first argument;
|
| 4005 |
+
|
| 4006 |
+
and then overload resolution is performed again.
|
| 4007 |
|
| 4008 |
[*Example 6*:
|
| 4009 |
|
| 4010 |
- `new T` results in one of the following calls:
|
| 4011 |
``` cpp
|
|
|
|
| 4030 |
|
| 4031 |
Here, each instance of `x` is a non-negative unspecified value
|
| 4032 |
representing array allocation overhead; the result of the
|
| 4033 |
*new-expression* will be offset by this amount from the value returned
|
| 4034 |
by `operator new[]`. This overhead may be applied in all array
|
| 4035 |
+
*new-expression*s, including those referencing a placement allocation
|
| 4036 |
+
function, except when referencing the library function
|
| 4037 |
+
`operator new[](std::size_t, void*)`. The amount of overhead may vary
|
| 4038 |
+
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]]); it returns
|
| 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 |
|
| 4051 |
+
If the allocation function is a non-allocating form
|
| 4052 |
+
[[new.delete.placement]] that returns null, the behavior is undefined.
|
| 4053 |
Otherwise, if the allocation function returns null, initialization shall
|
| 4054 |
not be done, the deallocation function shall not be called, and the
|
| 4055 |
value of the *new-expression* shall be null.
|
| 4056 |
|
| 4057 |
+
[*Note 11*: When the allocation function returns a value other than
|
| 4058 |
null, it must be a pointer to a block of storage in which space for the
|
| 4059 |
object has been reserved. The block of storage is assumed to be
|
| 4060 |
appropriately aligned and of the requested size. The address of the
|
| 4061 |
created object will not necessarily be the same as that of the block if
|
| 4062 |
the object is an array. — *end note*]
|
| 4063 |
|
| 4064 |
A *new-expression* that creates an object of type `T` initializes that
|
| 4065 |
object as follows:
|
| 4066 |
|
| 4067 |
+
- If the *new-initializer* is omitted, the object is default-initialized
|
| 4068 |
+
[[dcl.init]]. \[*Note 12*: If no initialization is performed, the
|
| 4069 |
+
object has an indeterminate value. — *end note*]
|
| 4070 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 4071 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 4072 |
|
| 4073 |
The invocation of the allocation function is sequenced before the
|
| 4074 |
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 [[class.free]], and the constructor
|
| 4081 |
+
[[class.ctor]] selected for the initialization (if any). If the
|
| 4082 |
+
*new-expression* creates an array of objects of class type, the
|
| 4083 |
+
destructor is potentially invoked [[class.dtor]].
|
| 4084 |
|
| 4085 |
+
If any part of the object initialization described above[^24] terminates
|
| 4086 |
by throwing an exception and a suitable deallocation function can be
|
| 4087 |
found, the deallocation function is called to free the memory in which
|
| 4088 |
the object was being constructed, after which the exception continues to
|
| 4089 |
propagate in the context of the *new-expression*. If no unambiguous
|
| 4090 |
matching deallocation function can be found, propagating the exception
|
| 4091 |
does not cause the object’s memory to be freed.
|
| 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* begins with a unary `::` operator, the
|
| 4098 |
deallocation function’s name is looked up in the global scope.
|
|
|
|
| 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
|
| 4109 |
single matching deallocation function, that function will be called;
|
| 4110 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 4111 |
+
a usual deallocation function and that function, considered as a
|
|
|
|
| 4112 |
placement deallocation function, would have been selected as a match for
|
| 4113 |
the allocation function, the program is ill-formed. For a non-placement
|
| 4114 |
allocation function, the normal deallocation function lookup is used to
|
| 4115 |
+
find the matching deallocation function [[expr.delete]].
|
| 4116 |
|
| 4117 |
[*Example 7*:
|
| 4118 |
|
| 4119 |
``` cpp
|
| 4120 |
struct S {
|
|
|
|
| 4123 |
|
| 4124 |
// Usual (non-placement) deallocation function:
|
| 4125 |
static void operator delete(void*, std::size_t);
|
| 4126 |
};
|
| 4127 |
|
| 4128 |
+
S* p = new (0) S; // error: non-placement deallocation function matches
|
| 4129 |
// placement allocation function
|
| 4130 |
```
|
| 4131 |
|
| 4132 |
— *end example*]
|
| 4133 |
|
| 4134 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 4135 |
returned from the allocation function call as the first argument of type
|
| 4136 |
`void*`. If a placement deallocation function is called, it is passed
|
| 4137 |
the same additional arguments as were passed to the placement allocation
|
| 4138 |
function, that is, the same arguments as those specified with the
|
| 4139 |
+
*new-placement* syntax. If the implementation is allowed to introduce a
|
| 4140 |
+
temporary object or make a copy of any argument as part of the call to
|
| 4141 |
+
the allocation function, it is unspecified whether the same object is
|
| 4142 |
+
used in the call to both the allocation and deallocation functions.
|
|
|
|
|
|
|
| 4143 |
|
| 4144 |
+
#### Delete <a id="expr.delete">[[expr.delete]]</a>
|
| 4145 |
|
| 4146 |
+
The *delete-expression* operator destroys a most derived object
|
| 4147 |
+
[[intro.object]] or array created by a *new-expression*.
|
| 4148 |
|
| 4149 |
``` bnf
|
| 4150 |
delete-expression:
|
| 4151 |
+
'::'ₒₚₜ delete cast-expression
|
| 4152 |
+
'::'ₒₚₜ delete '[' ']' cast-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] The operand shall be of pointer to
|
| 4159 |
+
object type or of class type. If of class type, the operand is
|
| 4160 |
+
contextually implicitly converted [[conv]] to a pointer to object
|
| 4161 |
+
type.[^26] The *delete-expression*’s result has type `void`.
|
| 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 |
+
to a non-array object created by a previous *new-expression*, or a
|
| 4169 |
+
pointer to a subobject [[intro.object]] representing a base class of
|
| 4170 |
+
such an object [[class.derived]]. If not, the behavior is undefined. In
|
| 4171 |
+
an array delete expression, the value of the operand of `delete` may be
|
| 4172 |
+
a null pointer value or a pointer value that resulted from a previous
|
| 4173 |
+
array *new-expression*.[^27] If not, the behavior is undefined.
|
|
|
|
| 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 |
|
| 4179 |
[*Note 2*: A pointer to a `const` type can be the operand of a
|
| 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 different from its dynamic type and the selected
|
| 4186 |
+
deallocation function (see below) is not a destroying operator delete,
|
| 4187 |
+
the static type shall be a base class of the dynamic type of the object
|
| 4188 |
+
to be deleted and the static type shall have a virtual destructor or the
|
| 4189 |
+
behavior is undefined. In an array delete expression, if the dynamic
|
| 4190 |
type of the object to be deleted differs from its static type, the
|
| 4191 |
behavior is undefined.
|
| 4192 |
|
| 4193 |
The *cast-expression* in a *delete-expression* shall be evaluated
|
| 4194 |
exactly once.
|
|
|
|
| 4196 |
If the object being deleted has incomplete class type at the point of
|
| 4197 |
deletion and the complete class has a non-trivial destructor or a
|
| 4198 |
deallocation function, the behavior is undefined.
|
| 4199 |
|
| 4200 |
If the value of the operand of the *delete-expression* is not a null
|
| 4201 |
+
pointer value and the selected deallocation function (see below) is not
|
| 4202 |
+
a destroying operator delete, the *delete-expression* will invoke the
|
| 4203 |
+
destructor (if any) for the object or the elements of the array being
|
| 4204 |
+
deleted. In the case of an array, the elements will be destroyed in
|
| 4205 |
+
order of decreasing address (that is, in reverse order of the completion
|
| 4206 |
+
of their constructor; see [[class.base.init]]).
|
| 4207 |
|
| 4208 |
If the value of the operand of the *delete-expression* is not a null
|
| 4209 |
pointer value, then:
|
| 4210 |
|
| 4211 |
- If the allocation call for the *new-expression* for the object to be
|
| 4212 |
+
deleted was not omitted and the allocation was not extended
|
| 4213 |
+
[[expr.new]], the *delete-expression* shall call a deallocation
|
| 4214 |
+
function [[basic.stc.dynamic.deallocation]]. The value returned from
|
| 4215 |
+
the allocation call of the *new-expression* shall be passed as the
|
| 4216 |
+
first argument to the deallocation function.
|
| 4217 |
- Otherwise, if the allocation was extended or was provided by extending
|
| 4218 |
the allocation of another *new-expression*, and the
|
| 4219 |
*delete-expression* for every other pointer value produced by a
|
| 4220 |
*new-expression* that had storage provided by the extended
|
| 4221 |
*new-expression* has been evaluated, the *delete-expression* shall
|
|
|
|
| 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 |
[*Note 4*: An implementation provides default definitions of the global
|
| 4237 |
+
deallocation functions `operator delete` for non-arrays
|
| 4238 |
+
[[new.delete.single]] and `operator delete[]` for arrays
|
| 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 |
When the keyword `delete` in a *delete-expression* is preceded by the
|
| 4244 |
unary `::` operator, the deallocation function’s name is looked up in
|
| 4245 |
global scope. Otherwise, the lookup considers class-specific
|
| 4246 |
+
deallocation functions [[class.free]]. If no class-specific deallocation
|
| 4247 |
+
function is found, the deallocation function’s name is looked up in
|
| 4248 |
+
global scope.
|
| 4249 |
|
| 4250 |
If deallocation function lookup finds more than one usual deallocation
|
| 4251 |
function, the function to be called is selected as follows:
|
| 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 have class scope, the one without a
|
| 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 multi-dimensional) array thereof, the function with a
|
| 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 |
+
denoted by the operand if its static type does not have a virtual
|
| 4273 |
+
destructor, and its most-derived object otherwise.
|
| 4274 |
+
|
| 4275 |
+
[*Note 5*: If the deallocation function is not a destroying operator
|
| 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 6*: Any cv-qualifiers in the type of the deleted object are
|
| 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
|
| 4292 |
of type `std::align_val_t` is used, the alignment of the type of the
|
| 4293 |
+
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 7*: If this results in a call to a replaceable deallocation
|
| 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 ([[new.delete.single]],
|
| 4304 |
+
[[new.delete.array]]). — *end note*]
|
| 4305 |
|
| 4306 |
Access and ambiguity control are done for both the deallocation function
|
| 4307 |
+
and the destructor ([[class.dtor]], [[class.free]]).
|
| 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
|
| 4313 |
reference to function type and an xvalue if `T` is an rvalue reference
|
| 4314 |
to object type; otherwise the result is a prvalue.
|
| 4315 |
|
| 4316 |
[*Note 1*: If `T` is a non-class type that is cv-qualified, the
|
| 4317 |
*cv-qualifier*s are discarded when determining the type of the resulting
|
| 4318 |
+
prvalue; see [[expr.prop]]. — *end note*]
|
| 4319 |
|
| 4320 |
+
An explicit type conversion can be expressed using functional notation
|
| 4321 |
+
[[expr.type.conv]], a type conversion operator (`dynamic_cast`,
|
| 4322 |
`static_cast`, `reinterpret_cast`, `const_cast`), or the *cast*
|
| 4323 |
notation.
|
| 4324 |
|
| 4325 |
``` bnf
|
| 4326 |
cast-expression:
|
| 4327 |
unary-expression
|
| 4328 |
'(' type-id ')' cast-expression
|
| 4329 |
```
|
| 4330 |
|
| 4331 |
Any type conversion not mentioned below and not explicitly defined by
|
| 4332 |
+
the user [[class.conv]] is ill-formed.
|
| 4333 |
|
| 4334 |
The conversions performed by
|
| 4335 |
|
| 4336 |
+
- a `const_cast` [[expr.const.cast]],
|
| 4337 |
+
- a `static_cast` [[expr.static.cast]],
|
| 4338 |
- a `static_cast` followed by a `const_cast`,
|
| 4339 |
+
- a `reinterpret_cast` [[expr.reinterpret.cast]], or
|
| 4340 |
- a `reinterpret_cast` followed by a `const_cast`,
|
| 4341 |
|
| 4342 |
can be performed using the cast notation of explicit type conversion.
|
| 4343 |
The same semantic restrictions and behaviors apply, with the exception
|
| 4344 |
that in performing a `static_cast` in the following situations the
|
|
|
|
| 4386 |
[*Note 2*: For example, if the classes were defined later in the
|
| 4387 |
translation unit, a multi-pass compiler would be permitted to interpret
|
| 4388 |
a cast between pointers to the classes as if the class types were
|
| 4389 |
complete at the point of the cast. — *end note*]
|
| 4390 |
|
| 4391 |
+
### Pointer-to-member operators <a id="expr.mptr.oper">[[expr.mptr.oper]]</a>
|
| 4392 |
|
| 4393 |
The pointer-to-member operators `->*` and `.*` group left-to-right.
|
| 4394 |
|
| 4395 |
``` bnf
|
| 4396 |
pm-expression:
|
|
|
|
| 4414 |
Abbreviating *pm-expression*`.*`*cast-expression* as `E1.*E2`, `E1` is
|
| 4415 |
called the *object expression*. If the dynamic type of `E1` does not
|
| 4416 |
contain the member to which `E2` refers, the behavior is undefined.
|
| 4417 |
Otherwise, the expression `E1` is sequenced before the expression `E2`.
|
| 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]].
|
| 4423 |
|
| 4424 |
[*Note 1*:
|
| 4425 |
|
| 4426 |
It is not possible to use a pointer to member that refers to a `mutable`
|
| 4427 |
+
member to modify a const class object. For example,
|
| 4428 |
|
| 4429 |
``` cpp
|
| 4430 |
struct S {
|
| 4431 |
S() : i(0) { }
|
| 4432 |
mutable int i;
|
| 4433 |
};
|
| 4434 |
void f()
|
| 4435 |
{
|
| 4436 |
const S cs;
|
| 4437 |
int S::* pm = &S::i; // pm refers to mutable member S::i
|
| 4438 |
+
cs.*pm = 88; // error: cs is a const object
|
| 4439 |
}
|
| 4440 |
```
|
| 4441 |
|
| 4442 |
— *end note*]
|
| 4443 |
|
|
|
|
| 4454 |
pointed to by `ptr_to_obj`.
|
| 4455 |
|
| 4456 |
— *end example*]
|
| 4457 |
|
| 4458 |
In a `.*` expression whose object expression is an rvalue, the program
|
| 4459 |
+
is ill-formed if the second operand is a pointer to member function
|
| 4460 |
+
whose *ref-qualifier* is `&`, unless its *cv-qualifier-seq* is `const`.
|
| 4461 |
+
In a `.*` expression whose object expression is an lvalue, the program
|
| 4462 |
+
is ill-formed if the second operand is a pointer to member function
|
| 4463 |
+
whose *ref-qualifier* is `&&`. The result of a `.*` expression whose
|
| 4464 |
+
second operand is a pointer to a data member is an lvalue if the first
|
| 4465 |
+
operand is an lvalue and an xvalue otherwise. The result of a `.*`
|
| 4466 |
+
expression whose second operand is a pointer to a member function is a
|
| 4467 |
+
prvalue. If the second operand is the null member pointer value
|
| 4468 |
+
[[conv.mem]], the behavior is undefined.
|
| 4469 |
|
| 4470 |
+
### Multiplicative operators <a id="expr.mul">[[expr.mul]]</a>
|
| 4471 |
|
| 4472 |
The multiplicative operators `*`, `/`, and `%` group left-to-right.
|
| 4473 |
|
| 4474 |
``` bnf
|
| 4475 |
multiplicative-expression:
|
|
|
|
| 4479 |
multiplicative-expression '%' pm-expression
|
| 4480 |
```
|
| 4481 |
|
| 4482 |
The operands of `*` and `/` shall have arithmetic or unscoped
|
| 4483 |
enumeration type; the operands of `%` shall have integral or unscoped
|
| 4484 |
+
enumeration type. The usual arithmetic conversions [[expr.arith.conv]]
|
| 4485 |
+
are performed on the operands and determine the type of the result.
|
| 4486 |
|
| 4487 |
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] if the quotient `a/b`
|
| 4494 |
is representable in the type of the result, `(a/b)*b + a%b` is equal to
|
| 4495 |
`a`; otherwise, the behavior of both `a/b` and `a%b` is undefined.
|
| 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
|
| 4501 |
+
arithmetic or enumeration type.
|
| 4502 |
|
| 4503 |
``` bnf
|
| 4504 |
additive-expression:
|
| 4505 |
multiplicative-expression
|
| 4506 |
additive-expression '+' multiplicative-expression
|
|
|
|
| 4522 |
|
| 4523 |
The result of the binary `+` operator is the sum of the operands. The
|
| 4524 |
result of the binary `-` operator is the difference resulting from the
|
| 4525 |
subtraction of the second operand from the first.
|
| 4526 |
|
| 4527 |
+
When an expression `J` that has integral type is added to or subtracted
|
| 4528 |
+
from an expression `P` of pointer type, the result has the type of `P`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4529 |
|
| 4530 |
+
- If `P` evaluates to a null pointer value and `J` evaluates to 0, the
|
| 4531 |
+
result is a null pointer value.
|
| 4532 |
+
- Otherwise, if `P` points to an array element i of an array object `x`
|
| 4533 |
+
with n elements [[dcl.array]], [^29] the expressions `P + J` and
|
| 4534 |
+
`J + P` (where `J` has the value j) point to the
|
| 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 1*: If the value i - j
|
| 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 |
[*Note 2*: In particular, a pointer to a base class cannot be used for
|
| 4558 |
pointer arithmetic when the array contains objects of a derived class
|
| 4559 |
type. — *end note*]
|
| 4560 |
|
| 4561 |
+
### Shift operators <a id="expr.shift">[[expr.shift]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4562 |
|
| 4563 |
The shift operators `<<` and `>>` group left-to-right.
|
| 4564 |
|
| 4565 |
``` bnf
|
| 4566 |
shift-expression:
|
|
|
|
| 4570 |
```
|
| 4571 |
|
| 4572 |
The operands shall be of integral or unscoped enumeration type and
|
| 4573 |
integral promotions are performed. The type of the result is that of the
|
| 4574 |
promoted left operand. The behavior is undefined if the right operand is
|
| 4575 |
+
negative, or greater than or equal to the width of the promoted left
|
| 4576 |
+
operand.
|
| 4577 |
|
| 4578 |
+
The value of `E1 << E2` is the unique value congruent to `E1` × 2^`E2`
|
| 4579 |
+
modulo 2ᴺ, where N is the width of the type of the result.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4580 |
|
| 4581 |
+
[*Note 1*: `E1` is left-shifted `E2` bit positions; vacated bits are
|
| 4582 |
+
zero-filled. — *end note*]
|
| 4583 |
+
|
| 4584 |
+
The value of `E1 >> E2` is `E1` / 2^`E2`, rounded down.
|
| 4585 |
+
|
| 4586 |
+
[*Note 2*: `E1` is right-shifted `E2` bit positions. Right-shift on
|
| 4587 |
+
signed integral types is an arithmetic right shift, which performs
|
| 4588 |
+
sign-extension. — *end note*]
|
| 4589 |
|
| 4590 |
The expression `E1` is sequenced before the expression `E2`.
|
| 4591 |
|
| 4592 |
+
### Three-way comparison operator <a id="expr.spaceship">[[expr.spaceship]]</a>
|
| 4593 |
+
|
| 4594 |
+
The three-way comparison operator groups left-to-right.
|
| 4595 |
+
|
| 4596 |
+
``` bnf
|
| 4597 |
+
compare-expression:
|
| 4598 |
+
shift-expression
|
| 4599 |
+
compare-expression '<=>' shift-expression
|
| 4600 |
+
```
|
| 4601 |
+
|
| 4602 |
+
The expression `p <=> q` is a prvalue indicating whether `p` is less
|
| 4603 |
+
than, equal to, greater than, or incomparable with `q`.
|
| 4604 |
+
|
| 4605 |
+
If one of the operands is of type `bool` and the other is not, the
|
| 4606 |
+
program is ill-formed.
|
| 4607 |
+
|
| 4608 |
+
If both operands have arithmetic types, or one operand has integral type
|
| 4609 |
+
and the other operand has unscoped enumeration type, the usual
|
| 4610 |
+
arithmetic conversions [[expr.arith.conv]] are applied to the operands.
|
| 4611 |
+
Then:
|
| 4612 |
+
|
| 4613 |
+
- If a narrowing conversion [[dcl.init.list]] is required, other than
|
| 4614 |
+
from an integral type to a floating-point type, the program is
|
| 4615 |
+
ill-formed.
|
| 4616 |
+
- Otherwise, if the operands have integral type, the result is of type
|
| 4617 |
+
`std::strong_ordering`. The result is `std::strong_ordering::equal` if
|
| 4618 |
+
both operands are arithmetically equal, `std::strong_ordering::less`
|
| 4619 |
+
if the first operand is arithmetically less than the second operand,
|
| 4620 |
+
and `std::strong_ordering::greater` otherwise.
|
| 4621 |
+
- Otherwise, the operands have floating-point type, and the result is of
|
| 4622 |
+
type `std::partial_ordering`. The expression `a <=> b` yields
|
| 4623 |
+
`std::partial_ordering::less` if `a` is less than `b`,
|
| 4624 |
+
`std::partial_ordering::greater` if `a` is greater than `b`,
|
| 4625 |
+
`std::partial_ordering::equivalent` if `a` is equivalent to `b`, and
|
| 4626 |
+
`std::partial_ordering::unordered` otherwise.
|
| 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 operand
|
| 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 |
+
If the composite pointer type is an object pointer type, `p <=> q` is of
|
| 4643 |
+
type `std::strong_ordering`. If two pointer operands `p` and `q` compare
|
| 4644 |
+
equal [[expr.eq]], `p <=> q` yields `std::strong_ordering::equal`; if
|
| 4645 |
+
`p` and `q` compare unequal, `p <=> q` yields
|
| 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]]. Otherwise, the result is unspecified.
|
| 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 the header `<compare>`
|
| 4655 |
+
is not imported or included prior to a use of such a class type – even
|
| 4656 |
+
an implicit use in which the type is not named (e.g., via the `auto`
|
| 4657 |
+
specifier [[dcl.spec.auto]] in a defaulted three-way comparison
|
| 4658 |
+
[[class.spaceship]] or use of the built-in operator) – the program is
|
| 4659 |
+
ill-formed.
|
| 4660 |
+
|
| 4661 |
+
### Relational operators <a id="expr.rel">[[expr.rel]]</a>
|
| 4662 |
|
| 4663 |
The relational operators group left-to-right.
|
| 4664 |
|
| 4665 |
[*Example 1*: `a<b<c` means `(a<b)<c` and *not*
|
| 4666 |
`(a<b)&&(b<c)`. — *end example*]
|
| 4667 |
|
| 4668 |
``` bnf
|
| 4669 |
relational-expression:
|
| 4670 |
+
compare-expression
|
| 4671 |
+
relational-expression '<' compare-expression
|
| 4672 |
+
relational-expression '>' compare-expression
|
| 4673 |
+
relational-expression '<=' compare-expression
|
| 4674 |
+
relational-expression '>=' compare-expression
|
| 4675 |
```
|
| 4676 |
|
| 4677 |
+
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 4678 |
+
function-to-pointer [[conv.func]] standard conversions are performed on
|
| 4679 |
+
the operands. The comparison is deprecated if both operands were of
|
| 4680 |
+
array type prior to these conversions [[depr.array.comp]].
|
| 4681 |
|
| 4682 |
+
The converted operands shall have arithmetic, enumeration, or pointer
|
| 4683 |
+
type. The operators `<` (less than), `>` (greater than), `<=` (less than
|
| 4684 |
+
or equal to), and `>=` (greater than or equal to) all yield `false` or
|
| 4685 |
+
`true`. The type of the result is `bool`.
|
|
|
|
|
|
|
| 4686 |
|
| 4687 |
+
The usual arithmetic conversions [[expr.arith.conv]] are performed on
|
| 4688 |
+
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 [^30] is defined in
|
| 4695 |
+
terms of a partial order consistent with the following rules:
|
| 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 the
|
| 4703 |
+
two members have the same access control [[class.access]], neither
|
| 4704 |
+
member is a subobject of zero size, and their class is not a union.
|
| 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`, `p>q`,
|
| 4711 |
+
`q<=p`, and `q<p` all yield `true` and `p<=q`, `p<q`, `q>=p`, and `q>p`
|
| 4712 |
+
all yield `false`. Otherwise, the result of each of the operators is
|
| 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 |
|
| 4719 |
+
### Equality operators <a id="expr.eq">[[expr.eq]]</a>
|
| 4720 |
|
| 4721 |
``` bnf
|
| 4722 |
equality-expression:
|
| 4723 |
relational-expression
|
| 4724 |
equality-expression '==' relational-expression
|
| 4725 |
equality-expression '!=' relational-expression
|
| 4726 |
```
|
| 4727 |
|
| 4728 |
The `==` (equal to) and the `!=` (not equal to) operators group
|
| 4729 |
+
left-to-right. The lvalue-to-rvalue [[conv.lval]], array-to-pointer
|
| 4730 |
+
[[conv.array]], and function-to-pointer [[conv.func]] standard
|
| 4731 |
+
conversions are performed on the operands. The comparison is deprecated
|
| 4732 |
+
if both operands were of array type prior to these conversions
|
| 4733 |
+
[[depr.array.comp]].
|
| 4734 |
+
|
| 4735 |
+
The converted operands shall have arithmetic, enumeration, pointer, or
|
| 4736 |
+
pointer-to-member type, or type `std::nullptr_t`. The operators `==` and
|
| 4737 |
+
`!=` both yield `true` or `false`, i.e., a result of type `bool`. In
|
| 4738 |
each case below, the operands shall have the same type after the
|
| 4739 |
specified conversions have been applied.
|
| 4740 |
|
| 4741 |
+
If at least one of the operands is a pointer, pointer conversions
|
| 4742 |
+
[[conv.ptr]], function pointer conversions [[conv.fctptr]], and
|
| 4743 |
+
qualification conversions [[conv.qual]] are performed on both operands
|
| 4744 |
+
to bring them to their composite pointer type [[expr.type]]. Comparing
|
| 4745 |
+
pointers is defined as follows:
|
| 4746 |
|
| 4747 |
- If one pointer represents the address of a complete object, and
|
| 4748 |
another pointer represents the address one past the last element of a
|
| 4749 |
+
different complete object, [^31] the result of the comparison is
|
| 4750 |
unspecified.
|
| 4751 |
- Otherwise, if the pointers are both null, both point to the same
|
| 4752 |
+
function, or both represent the same address [[basic.compound]], they
|
| 4753 |
+
compare equal.
|
| 4754 |
- Otherwise, the pointers compare unequal.
|
| 4755 |
|
| 4756 |
+
If at least one of the operands is a pointer to member,
|
| 4757 |
+
pointer-to-member conversions [[conv.mem]], function pointer conversions
|
| 4758 |
+
[[conv.fctptr]], and qualification conversions [[conv.qual]] are
|
| 4759 |
+
performed on both operands to bring them to their composite pointer type
|
| 4760 |
+
[[expr.type]]. Comparing pointers to members is defined as follows:
|
| 4761 |
|
| 4762 |
- If two pointers to members are both the null member pointer value,
|
| 4763 |
they compare equal.
|
| 4764 |
- If only one of two pointers to members is the null member pointer
|
| 4765 |
value, they compare unequal.
|
|
|
|
| 4779 |
|
| 4780 |
bool b1 = (bx == cx); // unspecified
|
| 4781 |
```
|
| 4782 |
|
| 4783 |
— *end example*]
|
| 4784 |
+
- If both refer to (possibly different) members of the same union
|
| 4785 |
+
[[class.union]], they compare equal.
|
| 4786 |
- Otherwise, two pointers to members compare equal if they would refer
|
| 4787 |
+
to the same member of the same most derived object [[intro.object]] or
|
| 4788 |
+
the same subobject if indirection with a hypothetical object of the
|
| 4789 |
associated class type were performed, otherwise they compare unequal.
|
| 4790 |
\[*Example 2*:
|
| 4791 |
``` cpp
|
| 4792 |
struct B {
|
| 4793 |
int f();
|
|
|
|
| 4815 |
unequal, the result is `false` for the `==` operator and `true` for the
|
| 4816 |
`!=` operator. Otherwise, the result of each of the operators is
|
| 4817 |
unspecified.
|
| 4818 |
|
| 4819 |
If both operands are of arithmetic or enumeration type, the usual
|
| 4820 |
+
arithmetic conversions [[expr.arith.conv]] are performed on both
|
| 4821 |
+
operands; each of the operators shall yield `true` if the specified
|
| 4822 |
+
relationship is true and `false` if it is false.
|
| 4823 |
|
| 4824 |
+
### Bitwise AND operator <a id="expr.bit.and">[[expr.bit.and]]</a>
|
| 4825 |
|
| 4826 |
``` bnf
|
| 4827 |
and-expression:
|
| 4828 |
equality-expression
|
| 4829 |
and-expression '&' equality-expression
|
| 4830 |
```
|
| 4831 |
|
| 4832 |
+
The `&` operator groups left-to-right. The operands shall be of integral
|
| 4833 |
+
or unscoped enumeration type. The usual arithmetic conversions
|
| 4834 |
+
[[expr.arith.conv]] are performed. Given the coefficients `xᵢ` and `yᵢ`
|
| 4835 |
+
of the base-2 representation [[basic.fundamental]] of the converted
|
| 4836 |
+
operands `x` and `y`, the coefficient `rᵢ` of the base-2 representation
|
| 4837 |
+
of the result `r` is 1 if both `xᵢ` and `yᵢ` are 1, and 0 otherwise.
|
| 4838 |
|
| 4839 |
+
[*Note 1*: The result is the bitwise function of the
|
| 4840 |
+
operands. — *end note*]
|
| 4841 |
+
|
| 4842 |
+
### Bitwise exclusive OR operator <a id="expr.xor">[[expr.xor]]</a>
|
| 4843 |
|
| 4844 |
``` bnf
|
| 4845 |
exclusive-or-expression:
|
| 4846 |
and-expression
|
| 4847 |
exclusive-or-expression '^' and-expression
|
| 4848 |
```
|
| 4849 |
|
| 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ᵢ` are 1,
|
| 4856 |
+
and 0 otherwise.
|
| 4857 |
|
| 4858 |
+
[*Note 1*: The result is the bitwise exclusive function of the
|
| 4859 |
+
operands. — *end note*]
|
| 4860 |
+
|
| 4861 |
+
### Bitwise inclusive OR operator <a id="expr.or">[[expr.or]]</a>
|
| 4862 |
|
| 4863 |
``` bnf
|
| 4864 |
inclusive-or-expression:
|
| 4865 |
exclusive-or-expression
|
| 4866 |
inclusive-or-expression '|' exclusive-or-expression
|
| 4867 |
```
|
| 4868 |
|
| 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ᵢ` are 1, and 0
|
| 4875 |
+
otherwise.
|
| 4876 |
|
| 4877 |
+
[*Note 1*: The result is the bitwise inclusive function of the
|
| 4878 |
+
operands. — *end note*]
|
| 4879 |
+
|
| 4880 |
+
### Logical AND operator <a id="expr.log.and">[[expr.log.and]]</a>
|
| 4881 |
|
| 4882 |
``` bnf
|
| 4883 |
logical-and-expression:
|
| 4884 |
inclusive-or-expression
|
| 4885 |
logical-and-expression '&&' inclusive-or-expression
|
| 4886 |
```
|
| 4887 |
|
| 4888 |
The `&&` operator groups left-to-right. The operands are both
|
| 4889 |
+
contextually converted to `bool` [[conv]]. The result is `true` if both
|
| 4890 |
+
operands are `true` and `false` otherwise. Unlike `&`, `&&` guarantees
|
| 4891 |
+
left-to-right evaluation: the second operand is not evaluated if the
|
| 4892 |
+
first operand is `false`.
|
| 4893 |
|
| 4894 |
+
The result is a `bool`. If the second expression is evaluated, the first
|
| 4895 |
+
expression is sequenced before the second expression
|
| 4896 |
+
[[intro.execution]].
|
|
|
|
| 4897 |
|
| 4898 |
+
### Logical OR operator <a id="expr.log.or">[[expr.log.or]]</a>
|
| 4899 |
|
| 4900 |
``` bnf
|
| 4901 |
logical-or-expression:
|
| 4902 |
logical-and-expression
|
| 4903 |
logical-or-expression '||' logical-and-expression
|
| 4904 |
```
|
| 4905 |
|
| 4906 |
The `||` operator groups left-to-right. The operands are both
|
| 4907 |
+
contextually converted to `bool` [[conv]]. The result is `true` if
|
| 4908 |
+
either of its operands is `true`, and `false` otherwise. Unlike `|`,
|
| 4909 |
`||` guarantees left-to-right evaluation; moreover, the second operand
|
| 4910 |
is not evaluated if the first operand evaluates to `true`.
|
| 4911 |
|
| 4912 |
+
The result is a `bool`. If the second expression is evaluated, the first
|
| 4913 |
+
expression is sequenced before the second expression
|
| 4914 |
+
[[intro.execution]].
|
|
|
|
| 4915 |
|
| 4916 |
+
### Conditional operator <a id="expr.cond">[[expr.cond]]</a>
|
| 4917 |
|
| 4918 |
``` bnf
|
| 4919 |
conditional-expression:
|
| 4920 |
logical-or-expression
|
| 4921 |
logical-or-expression '?' expression ':' assignment-expression
|
| 4922 |
```
|
| 4923 |
|
| 4924 |
Conditional expressions group right-to-left. The first expression is
|
| 4925 |
+
contextually converted to `bool` [[conv]]. It is evaluated and if it is
|
| 4926 |
+
`true`, the result of the conditional expression is the value of the
|
| 4927 |
+
second expression, otherwise that of the third expression. Only one of
|
| 4928 |
+
the second and third expressions is evaluated. The first expression is
|
| 4929 |
+
sequenced before the second or third expression [[intro.execution]].
|
|
|
|
|
|
|
| 4930 |
|
| 4931 |
If either the second or the third operand has type `void`, one of the
|
| 4932 |
following shall hold:
|
| 4933 |
|
| 4934 |
- The second or the third operand (but not both) is a (possibly
|
| 4935 |
+
parenthesized) *throw-expression* [[expr.throw]]; the result is of the
|
| 4936 |
+
type and value category of the other. The *conditional-expression* is
|
| 4937 |
+
a bit-field if that operand is a bit-field.
|
| 4938 |
- Both the second and the third operands have type `void`; the result is
|
| 4939 |
of type `void` and is a prvalue. \[*Note 1*: This includes the case
|
| 4940 |
where both operands are *throw-expression*s. — *end note*]
|
| 4941 |
|
| 4942 |
Otherwise, if the second and third operand are glvalue bit-fields of the
|
| 4943 |
same value category and of types *cv1* `T` and *cv2* `T`, respectively,
|
| 4944 |
+
the operands are considered to be of type cv `T` for the remainder of
|
| 4945 |
+
this subclause, where cv is the union of *cv1* and *cv2*.
|
| 4946 |
|
| 4947 |
Otherwise, if the second and third operand have different types and
|
| 4948 |
either has (possibly cv-qualified) class type, or if both are glvalues
|
| 4949 |
of the same value category and the same type except for
|
| 4950 |
cv-qualification, an attempt is made to form an implicit conversion
|
| 4951 |
+
sequence [[over.best.ics]] from each of those operands to the type of
|
| 4952 |
the other.
|
| 4953 |
|
| 4954 |
[*Note 2*: Properties such as access, whether an operand is a
|
| 4955 |
bit-field, or whether a conversion function is deleted are ignored for
|
| 4956 |
that determination. — *end note*]
|
|
|
|
| 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 |
subject to the constraint that in the conversion the reference must
|
| 4964 |
+
bind directly [[dcl.init.ref]] to a glvalue.
|
| 4965 |
- If `E2` is an xvalue, the target type is “rvalue reference to `T2`”,
|
| 4966 |
subject to the constraint that the reference must bind directly.
|
| 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
|
| 4972 |
+
`T2`,
|
| 4973 |
+
- otherwise, if `T2` is a base class of `T1`, the target type is *cv1*
|
| 4974 |
+
`T2`, where *cv1* denotes the cv-qualifiers of `T1`,
|
| 4975 |
- otherwise, the target type is the type that `E2` would have after
|
| 4976 |
+
applying the lvalue-to-rvalue [[conv.lval]], array-to-pointer
|
| 4977 |
+
[[conv.array]], and function-to-pointer [[conv.func]] standard
|
| 4978 |
conversions.
|
| 4979 |
|
| 4980 |
Using this process, it is determined whether an implicit conversion
|
| 4981 |
sequence can be formed from the second operand to the target type
|
| 4982 |
determined for the third operand, and vice versa. If both sequences can
|
|
|
|
| 4984 |
sequence, the program is ill-formed. If no conversion sequence can be
|
| 4985 |
formed, the operands are left unchanged and further checking is
|
| 4986 |
performed as described below. Otherwise, if exactly one conversion
|
| 4987 |
sequence can be formed, that conversion is applied to the chosen operand
|
| 4988 |
and the converted operand is used in place of the original operand for
|
| 4989 |
+
the remainder of this subclause.
|
| 4990 |
|
| 4991 |
[*Note 3*: The conversion might be ill-formed even if an implicit
|
| 4992 |
conversion sequence could be formed. — *end note*]
|
| 4993 |
|
| 4994 |
If the second and third operands are glvalues of the same value category
|
|
|
|
| 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 ([[over.match.oper]], [[over.built]]). If
|
| 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 |
|
| 5008 |
+
Lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 5009 |
+
function-to-pointer [[conv.func]] standard conversions are performed on
|
| 5010 |
+
the second and third operands. After those conversions, one of the
|
| 5011 |
+
following shall hold:
|
| 5012 |
|
| 5013 |
- The second and third operands have the same type; the result is of
|
| 5014 |
that type and the result object is initialized using the selected
|
| 5015 |
operand.
|
| 5016 |
- The second and third operands have arithmetic or enumeration type; the
|
| 5017 |
+
usual arithmetic conversions [[expr.arith.conv]] are performed to
|
| 5018 |
+
bring them to a common type, and the result is of that type.
|
| 5019 |
- One or both of the second and third operands have pointer type;
|
| 5020 |
+
pointer conversions [[conv.ptr]], function pointer conversions
|
| 5021 |
+
[[conv.fctptr]], and qualification conversions [[conv.qual]] are
|
| 5022 |
+
performed to bring them to their composite pointer type [[expr.type]].
|
| 5023 |
+
The result is of the composite pointer type.
|
| 5024 |
+
- One or both of the second and third operands have pointer-to-member
|
| 5025 |
+
type; pointer to member conversions [[conv.mem]], function pointer
|
| 5026 |
+
conversions [[conv.fctptr]], and qualification conversions
|
| 5027 |
+
[[conv.qual]] are performed to bring them to their composite pointer
|
| 5028 |
+
type [[expr.type]]. The result is of the composite pointer type.
|
| 5029 |
- Both the second and third operands have type `std::nullptr_t` or one
|
| 5030 |
has that type and the other is a null pointer constant. The result is
|
| 5031 |
of type `std::nullptr_t`.
|
| 5032 |
|
| 5033 |
+
### Yielding a value <a id="expr.yield">[[expr.yield]]</a>
|
| 5034 |
+
|
| 5035 |
+
``` bnf
|
| 5036 |
+
yield-expression:
|
| 5037 |
+
'co_yield' assignment-expression
|
| 5038 |
+
'co_yield' braced-init-list
|
| 5039 |
+
```
|
| 5040 |
+
|
| 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 `*p*`.yield_value(`*e*`)`.
|
| 5047 |
+
|
| 5048 |
+
[*Example 1*:
|
| 5049 |
+
|
| 5050 |
+
``` cpp
|
| 5051 |
+
template <typename T>
|
| 5052 |
+
struct my_generator {
|
| 5053 |
+
struct promise_type {
|
| 5054 |
+
T current_value;
|
| 5055 |
+
...
|
| 5056 |
+
auto yield_value(T v) {
|
| 5057 |
+
current_value = std::move(v);
|
| 5058 |
+
return std::suspend_always{};
|
| 5059 |
+
}
|
| 5060 |
+
};
|
| 5061 |
+
struct iterator { ... };
|
| 5062 |
+
iterator begin();
|
| 5063 |
+
iterator end();
|
| 5064 |
+
};
|
| 5065 |
+
|
| 5066 |
+
my_generator<pair<int,int>> g1() {
|
| 5067 |
+
for (int i = i; i < 10; ++i) co_yield {i,i};
|
| 5068 |
+
}
|
| 5069 |
+
my_generator<pair<int,int>> g2() {
|
| 5070 |
+
for (int i = i; i < 10; ++i) co_yield make_pair(i,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 |
+
|
| 5076 |
+
int main() {
|
| 5077 |
+
auto r1 = g1();
|
| 5078 |
+
auto r2 = g2();
|
| 5079 |
+
assert(std::equal(r1.begin(), r1.end(), r2.begin(), r2.end()));
|
| 5080 |
+
}
|
| 5081 |
+
```
|
| 5082 |
+
|
| 5083 |
+
— *end example*]
|
| 5084 |
+
|
| 5085 |
+
### Throwing an exception <a id="expr.throw">[[expr.throw]]</a>
|
| 5086 |
|
| 5087 |
``` bnf
|
| 5088 |
throw-expression:
|
| 5089 |
+
throw assignment-expressionₒₚₜ
|
| 5090 |
```
|
| 5091 |
|
| 5092 |
A *throw-expression* is of type `void`.
|
| 5093 |
|
| 5094 |
+
Evaluating a *throw-expression* with an operand throws an exception
|
| 5095 |
+
[[except.throw]]; the type of the exception object is determined by
|
| 5096 |
removing any top-level *cv-qualifier*s from the static type of the
|
| 5097 |
operand and adjusting the type from “array of `T`” or function type `T`
|
| 5098 |
to “pointer to `T`”.
|
| 5099 |
|
| 5100 |
A *throw-expression* with no operand rethrows the currently handled
|
| 5101 |
+
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 |
|
|
|
|
| 5117 |
```
|
| 5118 |
|
| 5119 |
— *end example*]
|
| 5120 |
|
| 5121 |
If no exception is presently being handled, evaluating a
|
| 5122 |
+
*throw-expression* with no operand calls `std::{}terminate()`
|
| 5123 |
+
[[except.terminate]].
|
| 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 referring to the left operand. The
|
| 5130 |
+
result in all cases is a bit-field if the left operand is a bit-field.
|
| 5131 |
+
In all cases, the assignment is sequenced after the value computation of
|
| 5132 |
+
the right and left operands, and before the value computation of the
|
| 5133 |
assignment expression. The right operand is sequenced before the left
|
| 5134 |
operand. With respect to an indeterminately-sequenced function call, the
|
| 5135 |
operation of a compound assignment is a single evaluation.
|
| 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 |
|
| 5141 |
``` bnf
|
| 5142 |
assignment-expression:
|
| 5143 |
conditional-expression
|
| 5144 |
+
yield-expression
|
| 5145 |
throw-expression
|
| 5146 |
+
logical-or-expression assignment-operator initializer-clause
|
| 5147 |
```
|
| 5148 |
|
| 5149 |
``` bnf
|
| 5150 |
assignment-operator: one of
|
| 5151 |
'= *= /= %= += -= >>= <<= &= ^= |='
|
| 5152 |
```
|
| 5153 |
|
| 5154 |
+
In simple assignment (`=`), the object referred to by the left operand
|
| 5155 |
+
is modified [[defns.access]] by replacing its value with the result of
|
| 5156 |
+
the right operand.
|
| 5157 |
|
| 5158 |
+
If the right operand is an expression, it is implicitly converted
|
| 5159 |
+
[[conv]] to the cv-unqualified type of the left operand.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
A simple assignment whose left operand is of a volatile-qualified type
|
| 5166 |
+
is deprecated [[depr.volatile.type]] unless the (possibly parenthesized)
|
| 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. Such
|
| 5171 |
+
expressions are deprecated if `E1` has volatile-qualified type; see
|
| 5172 |
+
[[depr.volatile.type]]. For `+=` and `-=`, `E1` shall either have
|
| 5173 |
+
arithmetic type or be a pointer to a possibly cv-qualified
|
| 5174 |
+
completely-defined object type. In all other cases, `E1` shall have
|
| 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 2*: This restriction applies to the relationship between the
|
| 5183 |
left and right sides of the assignment operation; it is not a statement
|
| 5184 |
about how the target of the assignment may be aliased in general. See
|
| 5185 |
[[basic.lval]]. — *end note*]
|
| 5186 |
|
| 5187 |
A *braced-init-list* may appear on the right-hand side of
|
|
|
|
| 5206 |
a = { 1 } = b; // syntax error
|
| 5207 |
```
|
| 5208 |
|
| 5209 |
— *end example*]
|
| 5210 |
|
| 5211 |
+
### Comma operator <a id="expr.comma">[[expr.comma]]</a>
|
| 5212 |
|
| 5213 |
The comma operator groups left-to-right.
|
| 5214 |
|
| 5215 |
``` bnf
|
| 5216 |
expression:
|
| 5217 |
assignment-expression
|
| 5218 |
expression ',' assignment-expression
|
| 5219 |
```
|
| 5220 |
|
| 5221 |
A pair of expressions separated by a comma is evaluated left-to-right;
|
| 5222 |
+
the left expression is a discarded-value expression [[expr.prop]]. The
|
| 5223 |
+
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 |
In contexts where comma is given a special meaning,
|
| 5230 |
|
| 5231 |
+
[*Example 1*: in lists of arguments to functions [[expr.call]] and
|
| 5232 |
+
lists of initializers [[dcl.init]] — *end example*]
|
| 5233 |
|
| 5234 |
+
the comma operator as described in this subclause can appear only in
|
| 5235 |
parentheses.
|
| 5236 |
|
| 5237 |
[*Example 2*:
|
| 5238 |
|
| 5239 |
``` cpp
|
|
|
|
| 5242 |
|
| 5243 |
has three arguments, the second of which has the value `5`.
|
| 5244 |
|
| 5245 |
— *end example*]
|
| 5246 |
|
| 5247 |
+
[*Note 1*: A comma expression appearing as the
|
| 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
|
| 5255 |
different semantics depending on whether or not an expression satisfies
|
| 5256 |
these requirements. Expressions that satisfy these requirements,
|
| 5257 |
+
assuming that copy elision [[class.copy.elision]] is not performed, are
|
| 5258 |
+
called *constant expressions*.
|
| 5259 |
|
| 5260 |
[*Note 1*: Constant expressions can be evaluated during
|
| 5261 |
translation. — *end note*]
|
| 5262 |
|
| 5263 |
``` bnf
|
| 5264 |
constant-expression:
|
| 5265 |
conditional-expression
|
| 5266 |
```
|
| 5267 |
|
| 5268 |
+
A variable or temporary object `o` is *constant-initialized* if
|
| 5269 |
+
|
| 5270 |
+
- either it has an initializer or its default-initialization results in
|
| 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 may have a non-trivial
|
| 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 its initializing declaration D is
|
| 5285 |
+
reachable from P and
|
| 5286 |
+
|
| 5287 |
+
- it is constexpr,
|
| 5288 |
+
- it is not initialized to a TU-local value, or
|
| 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
|
| 5294 |
+
- a template parameter object [[temp.param]], or
|
| 5295 |
+
- a string literal object [[lex.string]], or
|
| 5296 |
+
- a temporary object of non-volatile const-qualified literal type whose
|
| 5297 |
+
lifetime is extended [[class.temporary]] to that of a variable that is
|
| 5298 |
+
usable in constant expressions, or
|
| 5299 |
+
- a non-mutable subobject or reference member of any of the above.
|
| 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 in a constexpr function
|
| 5306 |
+
[[dcl.constexpr]] that is being evaluated as part of E;
|
| 5307 |
+
- an invocation of a non-constexpr function \[*Note 3*: Overload
|
| 5308 |
+
resolution [[over.match]] is applied as usual. — *end note*] ;
|
| 5309 |
+
- an invocation of an undefined constexpr function;
|
| 5310 |
+
- an invocation of an instantiated constexpr function that fails to
|
| 5311 |
+
satisfy the requirements for a constexpr function;
|
| 5312 |
+
- an invocation of a virtual function [[class.virtual]] for an object
|
| 5313 |
+
unless
|
| 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]] of this document \[*Note 4*: including, for
|
| 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 [[conv.lval]] that is applied to a
|
| 5329 |
glvalue that refers to a non-active member of a union or a subobject
|
| 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 ([[basic.def.odr]],
|
| 5345 |
[[expr.prim.lambda]]);
|
| 5346 |
\[*Example 1*:
|
| 5347 |
``` cpp
|
| 5348 |
void g() {
|
| 5349 |
const int n = 0;
|
| 5350 |
[=] {
|
| 5351 |
+
constexpr int i = n; // OK, n is not odr-used here
|
| 5352 |
+
constexpr int j = *&n; // error: &n would be an odr-use of n
|
| 5353 |
};
|
| 5354 |
}
|
| 5355 |
```
|
| 5356 |
|
| 5357 |
— *end example*]
|
| 5358 |
+
\[*Note 5*:
|
| 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 2*:
|
| 5365 |
``` cpp
|
| 5366 |
auto monad = [](auto v) { return [=] { return v; }; };
|
| 5367 |
auto bind = [](auto m) {
|
| 5368 |
return [=](auto fvm) { return fvm(m()); };
|
| 5369 |
};
|
| 5370 |
|
| 5371 |
+
// OK to capture objects with automatic storage duration created during constant expression evaluation.
|
| 5372 |
static_assert(bind(monad(2))(monad)() == monad(2)());
|
| 5373 |
```
|
| 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 ([[expr.ass]], [[expr.post.incr]],
|
|
|
|
|
|
|
| 5380 |
[[expr.pre.incr]]) unless it is applied to a non-volatile lvalue of
|
| 5381 |
literal type that refers to a non-volatile object whose lifetime began
|
| 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]]) and the allocated storage
|
| 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
|
| 5391 |
+
within the evaluation of E;
|
| 5392 |
+
- a call to an instance of `std::allocator<T>::deallocate`
|
| 5393 |
+
[[allocator.members]], unless it deallocates a region of storage
|
| 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]] or a dynamic cast
|
| 5400 |
+
[[expr.dynamic.cast]] or `typeid` [[expr.typeid]] expression that
|
| 5401 |
+
would throw an exception;
|
| 5402 |
+
- an *asm-declaration* [[dcl.asm]]; or
|
| 5403 |
+
- an invocation of the `va_arg` macro [[cstdarg.syn]].
|
| 5404 |
|
| 5405 |
+
If E satisfies the constraints of a core constant expression, but
|
| 5406 |
+
evaluation of E would evaluate an operation that has undefined behavior
|
| 5407 |
+
as specified in [[library]] through [[thread]] of this document, or an
|
| 5408 |
+
invocation of the `va_start` macro [[cstdarg.syn]], it is unspecified
|
| 5409 |
+
whether E is a core constant expression.
|
| 5410 |
|
| 5411 |
[*Example 3*:
|
| 5412 |
|
| 5413 |
``` cpp
|
| 5414 |
int x; // not constant
|
|
|
|
| 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 a call to a member function of
|
| 5455 |
+
`std::allocator<T>` as defined in [[allocator.members]], where `T` is a
|
| 5456 |
+
literal type, does not disqualify E from being a core constant
|
| 5457 |
+
expression, even if the actual evaluation of such a call would otherwise
|
| 5458 |
+
fail the requirements for a core constant expression. Similarly, the
|
| 5459 |
+
evaluation of a call to `std::destroy_at`, `std::ranges::destroy_at`,
|
| 5460 |
+
`std::construct_at`, or `std::ranges::construct_at` does not disqualify
|
| 5461 |
+
E from being a core constant expression unless:
|
| 5462 |
+
|
| 5463 |
+
- for a call to `std::construct_at` or `std::ranges::construct_at`, the
|
| 5464 |
+
first argument, of type `T*`, does not point to storage allocated with
|
| 5465 |
+
`std::allocator<T>` or to an object whose lifetime began within the
|
| 5466 |
+
evaluation of E, or the evaluation of the underlying constructor call
|
| 5467 |
+
disqualifies E from being a core constant expression, or
|
| 5468 |
+
- for a call to `std::destroy_at` or `std::ranges::destroy_at`, the
|
| 5469 |
+
first argument, of type `T*`, does not point to storage allocated with
|
| 5470 |
+
`std::allocator<T>` or to an object whose lifetime began within the
|
| 5471 |
+
evaluation of E, or the evaluation of the underlying destructor call
|
| 5472 |
+
disqualifies E from being a core constant expression.
|
| 5473 |
+
|
| 5474 |
+
An object `a` is said to have *constant destruction* if:
|
| 5475 |
+
|
| 5476 |
+
- it is not of class type nor (possibly multi-dimensional) array
|
| 5477 |
+
thereof, or
|
| 5478 |
+
- it is of class type or (possibly multi-dimensional) array thereof,
|
| 5479 |
+
that class type has a constexpr destructor, and for a hypothetical
|
| 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 may be used as bit-field lengths
|
| 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 4*:
|
| 5500 |
+
|
| 5501 |
+
``` cpp
|
| 5502 |
+
struct A {
|
| 5503 |
+
constexpr A(int i) : val(i) { }
|
| 5504 |
+
constexpr operator int() const { return val; }
|
| 5505 |
+
constexpr operator long() const { return 42; }
|
| 5506 |
+
private:
|
| 5507 |
+
int val;
|
| 5508 |
+
};
|
| 5509 |
+
constexpr A a = alignof(int);
|
| 5510 |
+
alignas(a) int n; // error: ambiguous conversion
|
| 5511 |
+
struct B { int n : a; }; // error: ambiguous conversion
|
| 5512 |
+
```
|
| 5513 |
+
|
| 5514 |
+
— *end example*]
|
| 5515 |
|
| 5516 |
A *converted constant expression* of type `T` is an expression,
|
| 5517 |
implicitly converted to type `T`, where the converted expression is a
|
| 5518 |
constant expression and the implicit conversion sequence contains only
|
| 5519 |
|
| 5520 |
- user-defined conversions,
|
| 5521 |
+
- lvalue-to-rvalue conversions [[conv.lval]],
|
| 5522 |
+
- array-to-pointer conversions [[conv.array]],
|
| 5523 |
+
- function-to-pointer conversions [[conv.func]],
|
| 5524 |
+
- qualification conversions [[conv.qual]],
|
| 5525 |
+
- integral promotions [[conv.prom]],
|
| 5526 |
+
- integral conversions [[conv.integral]] other than narrowing
|
| 5527 |
+
conversions [[dcl.init.list]],
|
| 5528 |
+
- null pointer conversions [[conv.ptr]] from `std::nullptr_t`,
|
| 5529 |
+
- null member pointer conversions [[conv.mem]] from `std::nullptr_t`,
|
| 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 may be used in `new` 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 |
|
| 5541 |
A *contextually converted constant expression of type `bool`* is an
|
| 5542 |
+
expression, contextually converted to `bool` [[conv]], where the
|
| 5543 |
+
converted expression is a constant expression and the conversion
|
| 5544 |
sequence contains only the conversions above.
|
| 5545 |
|
| 5546 |
A *constant expression* is either a glvalue core constant expression
|
| 5547 |
that refers to an entity that is a permitted result of a constant
|
| 5548 |
expression (as defined below), or a prvalue core constant expression
|
|
|
|
| 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
|
| 5559 |
+
designate an immediate function, and
|
| 5560 |
- if the value is an object of class or array type, each subobject
|
| 5561 |
satisfies these constraints for the value.
|
| 5562 |
|
| 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 |
+
[*Example 5*:
|
| 5569 |
|
| 5570 |
+
``` cpp
|
| 5571 |
+
consteval int f() { return 42; }
|
| 5572 |
+
consteval auto g() { return f; }
|
| 5573 |
+
consteval int h(int (*p)() = g()) { return p(); }
|
| 5574 |
+
constexpr int r = h(); // OK
|
| 5575 |
+
constexpr auto e = g(); // error: a pointer to an immediate function is
|
| 5576 |
+
// not a permitted result of a constant expression
|
| 5577 |
+
```
|
| 5578 |
|
| 5579 |
+
— *end example*]
|
| 5580 |
+
|
| 5581 |
+
[*Note 8*:
|
| 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.[^32]
|
| 5588 |
+
|
| 5589 |
+
[*Example 6*:
|
| 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
|
|
|
|
| 5600 |
|
| 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 its innermost non-block scope is a function
|
| 5607 |
+
parameter scope of an immediate function. An expression or conversion is
|
| 5608 |
+
an *immediate invocation* if it is a potentially-evaluated explicit or
|
| 5609 |
+
implicit invocation of an immediate function and is not in an immediate
|
| 5610 |
+
function context. An immediate invocation shall be a constant
|
| 5611 |
+
expression.
|
| 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.[^33]
|
| 5622 |
+
\[*Example 7*:
|
| 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
|
| 5628 |
+
double z[a]; // error: a is not usable
|
| 5629 |
+
// in constant expressions
|
| 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 might be 13 or 17 (see below)
|
| 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 9*: A manifestly constant-evaluated expression is evaluated even
|
| 5646 |
+
in an unevaluated operand. — *end note*]
|
| 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*, [^34]
|
| 5654 |
+
- an expression of the form `&` *cast-expression* that occurs within a
|
| 5655 |
+
templated entity, [^35] or
|
| 5656 |
+
- a subexpression of one of the above that is not a subexpression of a
|
| 5657 |
+
nested unevaluated operand.
|
| 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 whose name appears as a potentially constant evaluated
|
| 5664 |
+
expression that is either a constexpr variable or is of non-volatile
|
| 5665 |
+
const-qualified integral type or of reference type.
|
| 5666 |
+
|
| 5667 |
<!-- Link reference definitions -->
|
| 5668 |
+
[allocator.members]: utilities.md#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
|
| 5674 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 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.classref]: basic.md#basic.lookup.classref
|
| 5681 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 5682 |
+
[basic.lval]: #basic.lval
|
| 5683 |
[basic.namespace]: dcl.md#basic.namespace
|
| 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
|
|
|
|
| 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
|
| 5698 |
[class.bit]: class.md#class.bit
|
| 5699 |
+
[class.cdtor]: class.md#class.cdtor
|
| 5700 |
+
[class.conv]: class.md#class.conv
|
| 5701 |
+
[class.conv.fct]: class.md#class.conv.fct
|
| 5702 |
+
[class.copy.assign]: class.md#class.copy.assign
|
| 5703 |
+
[class.copy.ctor]: class.md#class.copy.ctor
|
| 5704 |
+
[class.copy.elision]: class.md#class.copy.elision
|
| 5705 |
+
[class.ctor]: class.md#class.ctor
|
| 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]: class.md#class.member.lookup
|
| 5712 |
[class.mfct]: class.md#class.mfct
|
| 5713 |
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 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
|
| 5737 |
+
[conv.qual]: #conv.qual
|
| 5738 |
+
[conv.rank]: basic.md#conv.rank
|
| 5739 |
+
[conv.rval]: #conv.rval
|
| 5740 |
[cpp]: cpp.md#cpp
|
| 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
|
| 5751 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 5752 |
+
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
| 5753 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 5754 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5755 |
[dcl.init]: dcl.md#dcl.init
|
| 5756 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 5757 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5758 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 5759 |
+
[dcl.init.string]: dcl.md#dcl.init.string
|
| 5760 |
[dcl.link]: dcl.md#dcl.link
|
| 5761 |
+
[dcl.mptr]: dcl.md#dcl.mptr
|
| 5762 |
[dcl.name]: dcl.md#dcl.name
|
| 5763 |
+
[dcl.ptr]: dcl.md#dcl.ptr
|
| 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
|
| 5781 |
[except.terminate]: except.md#except.terminate
|
| 5782 |
[except.throw]: except.md#except.throw
|
| 5783 |
[expr]: #expr
|
| 5784 |
[expr.add]: #expr.add
|
| 5785 |
[expr.alignof]: #expr.alignof
|
| 5786 |
+
[expr.arith.conv]: #expr.arith.conv
|
| 5787 |
[expr.ass]: #expr.ass
|
| 5788 |
+
[expr.await]: #expr.await
|
| 5789 |
[expr.bit.and]: #expr.bit.and
|
| 5790 |
[expr.call]: #expr.call
|
| 5791 |
[expr.cast]: #expr.cast
|
| 5792 |
[expr.comma]: #expr.comma
|
| 5793 |
+
[expr.compound]: #expr.compound
|
| 5794 |
[expr.cond]: #expr.cond
|
| 5795 |
[expr.const]: #expr.const
|
| 5796 |
[expr.const.cast]: #expr.const.cast
|
| 5797 |
+
[expr.context]: #expr.context
|
| 5798 |
[expr.delete]: #expr.delete
|
| 5799 |
[expr.dynamic.cast]: #expr.dynamic.cast
|
| 5800 |
[expr.eq]: #expr.eq
|
| 5801 |
[expr.log.and]: #expr.log.and
|
| 5802 |
[expr.log.or]: #expr.log.or
|
|
|
|
| 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
|
| 5829 |
[expr.ref]: #expr.ref
|
| 5830 |
[expr.reinterpret.cast]: #expr.reinterpret.cast
|
| 5831 |
[expr.rel]: #expr.rel
|
| 5832 |
[expr.shift]: #expr.shift
|
| 5833 |
[expr.sizeof]: #expr.sizeof
|
| 5834 |
+
[expr.spaceship]: #expr.spaceship
|
| 5835 |
[expr.static.cast]: #expr.static.cast
|
| 5836 |
[expr.sub]: #expr.sub
|
| 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
|
| 5847 |
[implimits]: limits.md#implimits
|
| 5848 |
[intro]: intro.md#intro
|
| 5849 |
+
[intro.execution]: basic.md#intro.execution
|
| 5850 |
+
[intro.memory]: basic.md#intro.memory
|
| 5851 |
+
[intro.object]: basic.md#intro.object
|
| 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]: utilities.md#meta.const.eval
|
| 5858 |
[namespace.qual]: basic.md#namespace.qual
|
| 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
|
|
|
|
| 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
|
| 5887 |
+
[temp.constr.atomic]: temp.md#temp.constr.atomic
|
| 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
|
| 5905 |
+
commutative.
|
|
|
|
| 5906 |
|
| 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 may or may not be aliased.
|
| 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]].
|
| 5918 |
+
|
| 5919 |
+
[^6]: In C++ class and array prvalues can have cv-qualified types. This
|
| 5920 |
+
differs from ISO C, in which non-lvalues never have cv-qualified
|
| 5921 |
+
types.
|
| 5922 |
+
|
| 5923 |
+
[^7]: This conversion never applies to non-static member functions
|
| 5924 |
+
because an lvalue that refers to a non-static member function cannot
|
| 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) ([[conv.ptr]], [[class.derived]]). This
|
| 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 an enumerated type are converted to some
|
| 5939 |
+
integral type.
|
| 5940 |
+
|
| 5941 |
+
[^10]: This also applies when the object expression is an implicit
|
| 5942 |
`(*this)` ([[class.mfct.non-static]]).
|
| 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
|
| 5948 |
subexpression evaluation happens even if the result is unnecessary
|
| 5949 |
to determine the value of the entire postfix expression, for example
|
| 5950 |
if the *id-expression* denotes a static member.
|
| 5951 |
|
| 5952 |
+
[^13]: Note that `(*(E1))` is an lvalue.
|
| 5953 |
|
| 5954 |
+
[^14]: The most derived object [[intro.object]] pointed or referred to
|
| 5955 |
by `v` can contain other `B` objects as base classes, but these are
|
| 5956 |
ignored.
|
| 5957 |
|
| 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 may have different cv-qualifiers, subject to the
|
|
|
|
|
|
|
|
|
|
| 5964 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 5965 |
constness.
|
| 5966 |
|
| 5967 |
+
[^18]: `T1` and `T2` may have different cv-qualifiers, subject to the
|
| 5968 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 5969 |
constness.
|
| 5970 |
|
| 5971 |
+
[^19]: This is sometimes referred to as a *type pun* when the result
|
| 5972 |
+
refers to the same object as the source glvalue.
|
| 5973 |
|
| 5974 |
+
[^20]: `const_cast`
|
| 5975 |
|
| 5976 |
is not limited to conversions that cast away a const-qualifier.
|
| 5977 |
|
| 5978 |
+
[^21]: `sizeof(bool)` is not required to be `1`.
|
| 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 may include evaluating a *new-initializer* and/or calling a
|
| 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.
|
| 5996 |
|
| 5997 |
+
[^26]: This implies that an object cannot be deleted using a pointer of
|
| 5998 |
type `void*` because `void` is not an object type.
|
| 5999 |
|
| 6000 |
+
[^27]: For nonzero-length arrays, this is the same as a pointer to the
|
| 6001 |
first element of the array created by that *new-expression*.
|
| 6002 |
Zero-length arrays do not have a first element.
|
| 6003 |
|
| 6004 |
+
[^28]: This is often called truncation towards zero.
|
|
|
|
|
|
|
|
|
|
| 6005 |
|
| 6006 |
+
[^29]: As specified in [[basic.compound]], an object that is not an
|
| 6007 |
+
array element is considered to belong to a single-element array for
|
| 6008 |
+
this purpose and a pointer past the last element of an array of n
|
| 6009 |
+
elements is considered to be equivalent to a pointer to a
|
| 6010 |
+
hypothetical array element n for this purpose.
|
| 6011 |
|
| 6012 |
+
[^30]: As specified in [[basic.compound]], an object that is not an
|
| 6013 |
+
array element is considered to belong to a single-element array for
|
| 6014 |
+
this purpose and a pointer past the last element of an array of n
|
| 6015 |
+
elements is considered to be equivalent to a pointer to a
|
| 6016 |
+
hypothetical array element n for this purpose.
|
| 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]: Nonetheless, implementations should provide consistent results,
|
| 6023 |
+
irrespective of whether the evaluation was performed during
|
|
|
|
|
|
|
|
|
|
| 6024 |
translation and/or during program execution.
|
| 6025 |
+
|
| 6026 |
+
[^33]: Testing this condition may involve a trial evaluation of its
|
| 6027 |
+
initializer as described above.
|
| 6028 |
+
|
| 6029 |
+
[^34]: Constant evaluation may be necessary to determine whether a
|
| 6030 |
+
narrowing conversion is performed [[dcl.init.list]].
|
| 6031 |
+
|
| 6032 |
+
[^35]: Constant evaluation may be necessary to determine whether such an
|
| 6033 |
+
expression is value-dependent [[temp.dep.constexpr]].
|