- tmp/tmpg8odqrvm/{from.md → to.md} +1947 -1167
tmp/tmpg8odqrvm/{from.md → to.md}
RENAMED
|
@@ -1,21 +1,21 @@
|
|
| 1 |
# Expressions <a id="expr">[[expr]]</a>
|
| 2 |
|
| 3 |
-
Clause [[expr]] defines the syntax, order of evaluation,
|
| 4 |
-
expressions.[^1] An expression is a sequence of operators
|
| 5 |
-
that specifies a computation. An expression can result in a
|
| 6 |
-
can cause side effects.
|
| 7 |
|
| 8 |
-
Operators can be overloaded, that is, given meaning when
|
| 9 |
-
expressions of class type (Clause [[class]]) or enumeration
|
| 10 |
-
[[dcl.enum]]). Uses of overloaded operators are transformed into
|
| 11 |
function calls as described in [[over.oper]]. Overloaded operators obey
|
| 12 |
-
the rules for syntax specified in Clause [[expr]],
|
| 13 |
-
of operand type
|
| 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 |
Clause [[expr]] defines the effects of operators when applied to types
|
| 19 |
for which they have not been overloaded. Operator overloading shall not
|
| 20 |
modify the rules for the *built-in operators*, that is, for operators
|
| 21 |
applied to types for which they are defined by this Standard. However,
|
|
@@ -27,25 +27,32 @@ applied to the operands before the operation is considered further
|
|
| 27 |
according to the rules in Clause [[expr]]; see [[over.match.oper]],
|
| 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 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
If an expression initially has the type “reference to `T`” (
|
| 38 |
[[dcl.ref]], [[dcl.init.ref]]), the type is adjusted to `T` prior to
|
| 39 |
any further analysis. The expression designates the object or function
|
| 40 |
denoted by the reference, and the expression is an lvalue or an xvalue,
|
| 41 |
depending on the expression.
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
cv-unqualified non-class, non-array type, the type of the expression is
|
| 45 |
adjusted to `T` prior to any further analysis.
|
| 46 |
|
|
|
|
|
|
|
| 47 |
An expression is an xvalue if it is:
|
| 48 |
|
| 49 |
- the result of calling a function, whether implicitly or explicitly,
|
| 50 |
whose return type is an rvalue reference to object type,
|
| 51 |
- a cast to an rvalue reference to object type,
|
|
@@ -57,10 +64,14 @@ An expression is an xvalue if it is:
|
|
| 57 |
In general, the effect of this rule is that named rvalue references are
|
| 58 |
treated as lvalues and unnamed rvalue references to objects are treated
|
| 59 |
as xvalues; rvalue references to functions are treated as lvalues
|
| 60 |
whether named or not.
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
``` cpp
|
| 63 |
struct A {
|
| 64 |
int m;
|
| 65 |
};
|
| 66 |
A&& operator+(A, A);
|
|
@@ -71,39 +82,50 @@ A&& ar = static_cast<A&&>(a);
|
|
| 71 |
```
|
| 72 |
|
| 73 |
The expressions `f()`, `f().m`, `static_cast<A&&>(a)`, and `a + a` are
|
| 74 |
xvalues. The expression `ar` is an lvalue.
|
| 75 |
|
|
|
|
|
|
|
| 76 |
In some contexts, *unevaluated operands* appear ([[expr.typeid]],
|
| 77 |
[[expr.sizeof]], [[expr.unary.noexcept]], [[dcl.type.simple]]). An
|
| 78 |
-
unevaluated operand is not evaluated.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
[[basic.def.odr]]).
|
|
|
|
|
|
|
| 83 |
|
| 84 |
Whenever a glvalue expression appears as an operand of an operator that
|
| 85 |
expects a prvalue for that operand, the lvalue-to-rvalue (
|
| 86 |
[[conv.lval]]), array-to-pointer ([[conv.array]]), or
|
| 87 |
function-to-pointer ([[conv.func]]) standard conversions are applied to
|
| 88 |
-
convert the expression to a prvalue.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
Many binary operators that expect operands of arithmetic or enumeration
|
| 95 |
type cause conversions and yield result types in a similar way. The
|
| 96 |
purpose is to yield a common type, which is also the type of the result.
|
| 97 |
This pattern is called the *usual arithmetic conversions*, which are
|
| 98 |
defined as follows:
|
| 99 |
|
| 100 |
- If either operand is of scoped enumeration type ([[dcl.enum]]), no
|
| 101 |
conversions are performed; if the other operand does not have the same
|
| 102 |
type, the expression is ill-formed.
|
| 103 |
-
- If either operand is of type `long
|
| 104 |
-
converted to `long
|
| 105 |
- Otherwise, if either operand is `double`, the other shall be converted
|
| 106 |
to `double`.
|
| 107 |
- Otherwise, if either operand is `float`, the other shall be converted
|
| 108 |
to `float`.
|
| 109 |
- Otherwise, the integral promotions ([[conv.prom]]) shall be performed
|
|
@@ -126,69 +148,83 @@ defined as follows:
|
|
| 126 |
- Otherwise, both operands shall be converted to the unsigned integer
|
| 127 |
type corresponding to the type of the operand with signed integer
|
| 128 |
type.
|
| 129 |
|
| 130 |
In some contexts, an expression only appears for its side effects. Such
|
| 131 |
-
an expression is called a *discarded-value expression*. The
|
| 132 |
-
|
| 133 |
-
[[conv.
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
- `(` *expression* `)`, where *expression* is one of these expressions,
|
| 139 |
-
- *id-expression* ([[expr.prim.
|
| 140 |
- subscripting ([[expr.sub]]),
|
| 141 |
- class member access ([[expr.ref]]),
|
| 142 |
- indirection ([[expr.unary.op]]),
|
| 143 |
- pointer-to-member operation ([[expr.mptr.oper]]),
|
| 144 |
- conditional expression ([[expr.cond]]) where both the second and the
|
| 145 |
third operands are one of these expressions, or
|
| 146 |
- comma expression ([[expr.comma]]) where the right operand is one of
|
| 147 |
these expressions.
|
| 148 |
|
| 149 |
-
Using an overloaded operator causes a function call; the
|
| 150 |
-
only operators with built-in meaning.
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
The values of the floating operands and the results of floating
|
| 155 |
expressions may be represented in greater precision and range than that
|
| 156 |
required by the type; the types are not changed thereby.[^3]
|
| 157 |
|
| 158 |
The *cv-combined type* of two types `T1` and `T2` is a type `T3` similar
|
| 159 |
to `T1` whose cv-qualification signature ([[conv.qual]]) is:
|
| 160 |
|
| 161 |
-
- for every
|
| 162 |
-
- if the resulting cv
|
| 163 |
-
|
| 164 |
|
| 165 |
-
Given similar types `T1` and `T2`, this construction
|
| 166 |
-
can be converted to `T3`.
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
| 169 |
|
| 170 |
- if both `p1` and `p2` are null pointer constants, `std::nullptr_t`;
|
| 171 |
- if either `p1` or `p2` is a null pointer constant, `T2` or `T1`,
|
| 172 |
respectively;
|
| 173 |
- if `T1` or `T2` is “pointer to *cv1* `void`” and the other type is
|
| 174 |
-
“pointer to *cv2* T”,
|
| 175 |
-
union of *cv1* and *cv2*;
|
|
|
|
|
|
|
|
|
|
| 176 |
- if `T1` is “pointer to *cv1* `C1`” and `T2` is “pointer to *cv2*
|
| 177 |
`C2`”, where `C1` is reference-related to `C2` or `C2` is
|
| 178 |
reference-related to `C1` ([[dcl.init.ref]]), the cv-combined type of
|
| 179 |
`T1` and `T2` or the cv-combined type of `T2` and `T1`, respectively;
|
| 180 |
- if `T1` is “pointer to member of `C1` of type *cv1* `U1`” and `T2` is
|
| 181 |
“pointer to member of `C2` of type *cv2* `U2`” where `C1` is
|
| 182 |
reference-related to `C2` or `C2` is reference-related to `C1` (
|
| 183 |
[[dcl.init.ref]]), the cv-combined type of `T2` and `T1` or the
|
| 184 |
cv-combined type of `T1` and `T2`, respectively;
|
| 185 |
-
- if `T1` and `T2` are similar
|
| 186 |
-
|
| 187 |
- otherwise, a program that necessitates the determination of a
|
| 188 |
composite pointer type is ill-formed.
|
| 189 |
|
|
|
|
|
|
|
| 190 |
``` cpp
|
| 191 |
typedef void *p;
|
| 192 |
typedef const int *q;
|
| 193 |
typedef int **pi;
|
| 194 |
typedef const int **pci;
|
|
@@ -196,82 +232,82 @@ typedef const int **pci;
|
|
| 196 |
|
| 197 |
The composite pointer type of `p` and `q` is “pointer to `const void`”;
|
| 198 |
the composite pointer type of `pi` and `pci` is “pointer to `const`
|
| 199 |
pointer to `const int`”.
|
| 200 |
|
|
|
|
|
|
|
| 201 |
## Primary expressions <a id="expr.prim">[[expr.prim]]</a>
|
| 202 |
|
| 203 |
-
### General <a id="expr.prim.general">[[expr.prim.general]]</a>
|
| 204 |
-
|
| 205 |
``` bnf
|
| 206 |
primary-expression:
|
| 207 |
literal
|
| 208 |
'this'
|
| 209 |
'(' expression ')'
|
| 210 |
id-expression
|
| 211 |
lambda-expression
|
|
|
|
| 212 |
```
|
| 213 |
|
| 214 |
-
|
| 215 |
-
id-expression:
|
| 216 |
-
unqualified-id
|
| 217 |
-
qualified-id
|
| 218 |
-
```
|
| 219 |
-
|
| 220 |
-
``` bnf
|
| 221 |
-
unqualified-id:
|
| 222 |
-
identifier
|
| 223 |
-
operator-function-id
|
| 224 |
-
conversion-function-id
|
| 225 |
-
literal-operator-id
|
| 226 |
-
'~' class-name
|
| 227 |
-
'~' decltype-specifier
|
| 228 |
-
template-id
|
| 229 |
-
```
|
| 230 |
|
| 231 |
A *literal* is a primary expression. Its type depends on its form (
|
| 232 |
[[lex.literal]]). A string literal is an lvalue; all other literals are
|
| 233 |
prvalues.
|
| 234 |
|
|
|
|
|
|
|
| 235 |
The keyword `this` names a pointer to the object for which a non-static
|
| 236 |
member function ([[class.this]]) is invoked or a non-static data
|
| 237 |
member’s initializer ([[class.mem]]) is evaluated.
|
| 238 |
|
| 239 |
If a declaration declares a member function or member function template
|
| 240 |
of a class `X`, the expression `this` is a prvalue of type “pointer to
|
| 241 |
-
*cv-qualifier-seq* `X`” between the optional *cv-
|
| 242 |
end of the *function-definition*, *member-declarator*, or *declarator*.
|
| 243 |
It shall not appear before the optional *cv-qualifier-seq* and it shall
|
| 244 |
not appear within the declaration of a static member function (although
|
| 245 |
its type and value category are defined within a static member function
|
| 246 |
-
as they are within a non-static member function).
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
``` cpp
|
| 254 |
struct A {
|
| 255 |
char g();
|
| 256 |
template<class T> auto f(T t) -> decltype(t + g())
|
| 257 |
{ return t + g(); }
|
| 258 |
};
|
| 259 |
template auto A::f(int t) -> decltype(t + g());
|
| 260 |
```
|
| 261 |
|
|
|
|
|
|
|
| 262 |
Otherwise, if a *member-declarator* declares a non-static data member (
|
| 263 |
[[class.mem]]) of a class `X`, the expression `this` is a prvalue of
|
| 264 |
-
type “pointer to `X`” within the optional
|
| 265 |
-
It shall not appear elsewhere in the
|
|
|
|
| 266 |
|
| 267 |
The expression `this` shall not appear in any other context.
|
| 268 |
|
|
|
|
|
|
|
| 269 |
``` cpp
|
| 270 |
class Outer {
|
| 271 |
int a[sizeof(*this)]; // error: not inside a member function
|
| 272 |
-
unsigned int sz = sizeof(*this); // OK: in
|
| 273 |
|
| 274 |
void f() {
|
| 275 |
int b[sizeof(*this)]; // OK
|
| 276 |
|
| 277 |
struct Inner {
|
|
@@ -279,32 +315,85 @@ class Outer {
|
|
| 279 |
};
|
| 280 |
}
|
| 281 |
};
|
| 282 |
```
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
| 287 |
parenthesized expression can be used in exactly the same contexts as
|
| 288 |
-
those where
|
| 289 |
-
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
An *identifier* is an *id-expression* provided it has been suitably
|
| 295 |
-
declared (Clause [[dcl.dcl]]).
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
see [[
|
| 299 |
-
|
| 300 |
-
|
|
|
|
|
|
|
| 301 |
member is transformed to a class member access expression (
|
| 302 |
-
[[class.mfct.non-static]]).
|
| 303 |
-
|
| 304 |
-
The
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
``` bnf
|
| 308 |
qualified-id:
|
| 309 |
nested-name-specifier 'template'ₒₚₜ unqualified-id
|
| 310 |
```
|
|
@@ -325,29 +414,36 @@ shall be a class or enumeration type.
|
|
| 325 |
A *nested-name-specifier* that denotes a class, optionally followed by
|
| 326 |
the keyword `template` ([[temp.names]]), and then followed by the name
|
| 327 |
of a member of either that class ([[class.mem]]) or one of its base
|
| 328 |
classes (Clause [[class.derived]]), is a *qualified-id*;
|
| 329 |
[[class.qual]] describes name lookup for class members that appear in
|
| 330 |
-
*qualified-
|
| 331 |
type of the member. The result is an lvalue if the member is a static
|
| 332 |
-
member function or a data member and a prvalue otherwise.
|
| 333 |
-
can be referred to using a *qualified-id* at any point in its potential
|
| 334 |
-
scope ([[basic.scope.class]]). Where *class-name* `::~` *class-name* is
|
| 335 |
-
used, the two *class-name*s shall refer to the same class; this notation
|
| 336 |
-
names the destructor ([[class.dtor]]). The form
|
| 337 |
-
`~` *decltype-specifier* also denotes the destructor, but it shall not
|
| 338 |
-
be used as the *unqualified-id* in a *qualified-id*. a *typedef-name*
|
| 339 |
-
that names a class is a *class-name* ([[class.name]]).
|
| 340 |
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
name
|
| 346 |
-
|
| 347 |
-
The
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
A *nested-name-specifier* that denotes an enumeration ([[dcl.enum]]),
|
| 351 |
followed by the name of an enumerator of that enumeration, is a
|
| 352 |
*qualified-id* that refers to the enumerator. The result is the
|
| 353 |
enumerator. The type of the result is the type of the enumeration. The
|
|
@@ -356,141 +452,104 @@ result is a prvalue.
|
|
| 356 |
In a *qualified-id*, if the *unqualified-id* is a
|
| 357 |
*conversion-function-id*, its *conversion-type-id* shall denote the same
|
| 358 |
type in both the context in which the entire *qualified-id* occurs and
|
| 359 |
in the context of the class denoted by the *nested-name-specifier*.
|
| 360 |
|
| 361 |
-
An *id-expression* that denotes a non-static data member or non-static
|
| 362 |
-
member function of a class can only be used:
|
| 363 |
-
|
| 364 |
-
- as part of a class member access ([[expr.ref]]) in which the object
|
| 365 |
-
expression refers to the member’s class[^4] or a class derived from
|
| 366 |
-
that class, or
|
| 367 |
-
- to form a pointer to member ([[expr.unary.op]]), or
|
| 368 |
-
- if that *id-expression* denotes a non-static data member and it
|
| 369 |
-
appears in an unevaluated operand.
|
| 370 |
-
``` cpp
|
| 371 |
-
struct S {
|
| 372 |
-
int m;
|
| 373 |
-
};
|
| 374 |
-
int i = sizeof(S::m); // OK
|
| 375 |
-
int j = sizeof(S::m + 42); // OK
|
| 376 |
-
```
|
| 377 |
-
|
| 378 |
### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
|
| 379 |
|
| 380 |
-
Lambda expressions provide a concise way to create simple function
|
| 381 |
-
objects.
|
| 382 |
-
|
| 383 |
-
``` cpp
|
| 384 |
-
#include <algorithm>
|
| 385 |
-
#include <cmath>
|
| 386 |
-
void abssort(float* x, unsigned N) {
|
| 387 |
-
std::sort(x, x + N,
|
| 388 |
-
[](float a, float b) {
|
| 389 |
-
return std::abs(a) < std::abs(b);
|
| 390 |
-
});
|
| 391 |
-
}
|
| 392 |
-
```
|
| 393 |
-
|
| 394 |
``` bnf
|
| 395 |
lambda-expression:
|
| 396 |
lambda-introducer lambda-declaratorₒₚₜ compound-statement
|
| 397 |
```
|
| 398 |
|
| 399 |
``` bnf
|
| 400 |
lambda-introducer:
|
| 401 |
'[' lambda-captureₒₚₜ ']'
|
| 402 |
```
|
| 403 |
|
| 404 |
-
``` bnf
|
| 405 |
-
lambda-capture:
|
| 406 |
-
capture-default
|
| 407 |
-
capture-list
|
| 408 |
-
capture-default ',' capture-list
|
| 409 |
-
```
|
| 410 |
-
|
| 411 |
-
``` bnf
|
| 412 |
-
capture-default:
|
| 413 |
-
'&'
|
| 414 |
-
'='
|
| 415 |
-
```
|
| 416 |
-
|
| 417 |
-
``` bnf
|
| 418 |
-
capture-list:
|
| 419 |
-
capture '...'ₒₚₜ
|
| 420 |
-
capture-list ',' capture '...'ₒₚₜ
|
| 421 |
-
```
|
| 422 |
-
|
| 423 |
-
``` bnf
|
| 424 |
-
capture:
|
| 425 |
-
simple-capture
|
| 426 |
-
init-capture
|
| 427 |
-
```
|
| 428 |
-
|
| 429 |
-
``` bnf
|
| 430 |
-
simple-capture:
|
| 431 |
-
identifier
|
| 432 |
-
'&' identifier
|
| 433 |
-
'this'
|
| 434 |
-
```
|
| 435 |
-
|
| 436 |
-
``` bnf
|
| 437 |
-
init-capture:
|
| 438 |
-
identifier initializer
|
| 439 |
-
'&' identifier initializer
|
| 440 |
-
```
|
| 441 |
-
|
| 442 |
``` bnf
|
| 443 |
lambda-declarator:
|
| 444 |
-
'(' parameter-declaration-clause ')'
|
| 445 |
-
|
| 446 |
```
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
[
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
*lambda-expression*
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
- the size and/or alignment of the closure type,
|
| 471 |
- whether the closure type is trivially copyable (Clause [[class]]),
|
| 472 |
- whether the closure type is a standard-layout class (Clause
|
| 473 |
[[class]]), or
|
| 474 |
- whether the closure type is a POD class (Clause [[class]]).
|
| 475 |
|
| 476 |
An implementation shall not add members of rvalue reference type to the
|
| 477 |
closure type.
|
| 478 |
|
| 479 |
-
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 480 |
-
as if the *lambda-declarator* were `()`. The lambda return type is
|
| 481 |
-
`auto`, which is replaced by the *trailing-return-type* if provided
|
| 482 |
-
and/or deduced from `return` statements as described in
|
| 483 |
-
[[dcl.spec.auto]].
|
| 484 |
-
|
| 485 |
-
``` cpp
|
| 486 |
-
auto x1 = [](int i){ return i; }; // OK: return type is int
|
| 487 |
-
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 488 |
-
int j;
|
| 489 |
-
auto x3 = []()->auto&& { return j; }; // OK: return type is int&
|
| 490 |
-
```
|
| 491 |
-
|
| 492 |
The closure type for a non-generic *lambda-expression* has a public
|
| 493 |
inline function call operator ([[over.call]]) whose parameters and
|
| 494 |
return type are described by the *lambda-expression*’s
|
| 495 |
*parameter-declaration-clause* and *trailing-return-type* respectively.
|
| 496 |
For a generic lambda, the closure type has a public inline function call
|
|
@@ -504,13 +563,16 @@ of the function call operator template are derived from the
|
|
| 504 |
*lambda-expression*'s *trailing-return-type* and
|
| 505 |
*parameter-declaration-clause* by replacing each occurrence of `auto` in
|
| 506 |
the *decl-specifier*s of the *parameter-declaration-clause* with the
|
| 507 |
name of the corresponding invented *template-parameter*.
|
| 508 |
|
|
|
|
|
|
|
| 509 |
``` cpp
|
| 510 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
| 511 |
bool b = glambda(3, 3.14); // OK
|
|
|
|
| 512 |
auto vglambda = [](auto printer) {
|
| 513 |
return [=](auto&& ... ts) { // OK: ts is a function parameter pack
|
| 514 |
printer(std::forward<decltype(ts)>(ts)...);
|
| 515 |
|
| 516 |
return [=]() {
|
|
@@ -522,42 +584,99 @@ auto glambda = [](auto a, auto&& b) { return a < b; };
|
|
| 522 |
{ std::cout << v1 << v2 << v3; } );
|
| 523 |
auto q = p(1, 'a', 3.14); // OK: outputs 1a3.14
|
| 524 |
q(); // OK: outputs 1a3.14
|
| 525 |
```
|
| 526 |
|
| 527 |
-
|
|
|
|
|
|
|
| 528 |
[[class.mfct.non-static]]) if and only if the *lambda-expression*’s
|
| 529 |
*parameter-declaration-clause* is not followed by `mutable`. It is
|
| 530 |
-
neither virtual nor declared `volatile`. Any *
|
| 531 |
specified on a *lambda-expression* applies to the corresponding function
|
| 532 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 533 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 534 |
-
call operator or operator template.
|
| 535 |
-
|
| 536 |
-
*lambda-expression*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 537 |
|
| 538 |
The closure type for a non-generic *lambda-expression* with no
|
| 539 |
-
*lambda-capture* has a
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
|
|
|
|
|
|
| 548 |
*template-parameter-list*, and the pointer to function has the same
|
| 549 |
parameter types, as the function call operator template. The return type
|
| 550 |
of the pointer to function shall behave as if it were a
|
| 551 |
*decltype-specifier* denoting the return type of the corresponding
|
| 552 |
-
function call operator template specialization.
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
|
| 560 |
``` cpp
|
| 561 |
auto glambda = [](auto a) { return a; };
|
| 562 |
int (*fp)(int) = glambda;
|
| 563 |
```
|
|
@@ -579,10 +698,14 @@ struct Closure {
|
|
| 579 |
template<class T> operator fptr_t<T>() const
|
| 580 |
{ return &lambda_call_operator_invoker; }
|
| 581 |
};
|
| 582 |
```
|
| 583 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
``` cpp
|
| 585 |
void f1(int (*)(int)) { }
|
| 586 |
void f2(char (*)(int)) { }
|
| 587 |
|
| 588 |
void g(int (*)(int)) { } // #1
|
|
@@ -597,33 +720,63 @@ f2(glambda); // error: ID is not convertible
|
|
| 597 |
g(glambda); // error: ambiguous
|
| 598 |
h(glambda); // OK: calls #3 since it is convertible from ID
|
| 599 |
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 600 |
```
|
| 601 |
|
|
|
|
|
|
|
| 602 |
The value returned by any given specialization of this conversion
|
| 603 |
-
function template
|
| 604 |
has the same effect as invoking the generic lambda’s corresponding
|
| 605 |
-
function call operator template specialization.
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
|
| 610 |
``` cpp
|
| 611 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 612 |
int (*GL_int)(int) = GL; // OK: through conversion function template
|
| 613 |
GL_int(3); // OK: same as GL(3)
|
| 614 |
```
|
| 615 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
The *lambda-expression*’s *compound-statement* yields the
|
| 617 |
*function-body* ([[dcl.fct.def]]) of the function call operator, but
|
| 618 |
for purposes of name lookup ([[basic.lookup]]), determining the type
|
| 619 |
and value of `this` ([[class.this]]) and transforming *id-expression*s
|
| 620 |
referring to non-static class members into class member access
|
| 621 |
expressions using `(*this)` ([[class.mfct.non-static]]), the
|
| 622 |
*compound-statement* is considered in the context of the
|
| 623 |
*lambda-expression*.
|
| 624 |
|
|
|
|
|
|
|
| 625 |
``` cpp
|
| 626 |
struct S1 {
|
| 627 |
int x, y;
|
| 628 |
int operator()(int);
|
| 629 |
void f() {
|
|
@@ -633,46 +786,135 @@ struct S1 {
|
|
| 633 |
};
|
| 634 |
}
|
| 635 |
};
|
| 636 |
```
|
| 637 |
|
|
|
|
|
|
|
| 638 |
Further, a variable `__func__` is implicitly defined at the beginning of
|
| 639 |
the *compound-statement* of the *lambda-expression*, with semantics as
|
| 640 |
described in [[dcl.fct.def.general]].
|
| 641 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 643 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 644 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 645 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 646 |
-
form “`&` *identifier*”
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
``` cpp
|
| 651 |
struct S2 { void f(int i); };
|
| 652 |
void S2::f(int i) {
|
| 653 |
[&, i]{ }; // OK
|
| 654 |
[&, &i]{ }; // error: i preceded by & when & is the default
|
|
|
|
| 655 |
[=, this]{ }; // error: this when = is the default
|
| 656 |
[i, i]{ }; // error: i repeated
|
|
|
|
| 657 |
}
|
| 658 |
```
|
| 659 |
|
|
|
|
|
|
|
| 660 |
A *lambda-expression* whose smallest enclosing scope is a block scope (
|
| 661 |
[[basic.scope.block]]) is a *local lambda expression*; any other
|
| 662 |
*lambda-expression* shall not have a *capture-default* or
|
| 663 |
*simple-capture* in its *lambda-introducer*. The *reaching scope* of a
|
| 664 |
local lambda expression is the set of enclosing scopes up to and
|
| 665 |
-
including the innermost enclosing function and its parameters.
|
| 666 |
-
|
|
|
|
|
|
|
| 667 |
|
| 668 |
The *identifier* in a *simple-capture* is looked up using the usual
|
| 669 |
rules for unqualified name lookup ([[basic.lookup.unqual]]); each such
|
| 670 |
lookup shall find an entity. An entity that is designated by a
|
| 671 |
*simple-capture* is said to be *explicitly captured*, and shall be
|
| 672 |
-
`this`
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
An *init-capture* behaves as if it declares and explicitly captures a
|
| 676 |
variable of the form “`auto` *init-capture* `;`” whose declarative
|
| 677 |
region is the *lambda-expression*’s *compound-statement*, except that:
|
| 678 |
|
|
@@ -682,34 +924,45 @@ region is the *lambda-expression*’s *compound-statement*, except that:
|
|
| 682 |
non-static data member, and no additional copy and destruction is
|
| 683 |
performed, and
|
| 684 |
- if the capture is by reference, the variable’s lifetime ends when the
|
| 685 |
closure object’s lifetime ends.
|
| 686 |
|
| 687 |
-
This enables an *init-capture* like “`x = std::move(x)`”;
|
| 688 |
-
“`x`” must bind to a declaration in the surrounding
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
``` cpp
|
| 691 |
int x = 4;
|
| 692 |
auto y = [&r = x, x = x+1]()->int {
|
| 693 |
r += 2;
|
| 694 |
return x+2;
|
| 695 |
}(); // Updates ::x to 6, and initializes y to 7.
|
|
|
|
|
|
|
| 696 |
```
|
| 697 |
|
|
|
|
|
|
|
| 698 |
A *lambda-expression* with an associated *capture-default* that does not
|
| 699 |
-
explicitly capture `this` or a variable with automatic storage duration
|
| 700 |
(this excludes any *id-expression* that has been found to refer to an
|
| 701 |
*init-capture*'s associated non-static data member), is said to
|
| 702 |
-
*implicitly capture* the entity (i.e., `this` or a variable) if the
|
| 703 |
*compound-statement*:
|
| 704 |
|
| 705 |
-
- odr-uses ([[basic.def.odr]]) the entity
|
|
|
|
|
|
|
| 706 |
- names the entity in a potentially-evaluated expression (
|
| 707 |
[[basic.def.odr]]) where the enclosing full-expression depends on a
|
| 708 |
generic lambda parameter declared within the reaching scope of the
|
| 709 |
*lambda-expression*.
|
| 710 |
|
|
|
|
|
|
|
| 711 |
``` cpp
|
| 712 |
void f(int, const int (&)[2] = {}) { } // #1
|
| 713 |
void f(const int&, const int (&)[1]) { } // #2
|
| 714 |
void test() {
|
| 715 |
const int x = 17;
|
|
@@ -722,63 +975,85 @@ void test() {
|
|
| 722 |
f(x, selector); // OK: is a dependent expression, so captures x
|
| 723 |
};
|
| 724 |
}
|
| 725 |
```
|
| 726 |
|
|
|
|
|
|
|
| 727 |
All such implicitly captured entities shall be declared within the
|
| 728 |
-
reaching scope of the lambda expression.
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
|
|
|
|
|
|
| 732 |
|
| 733 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 734 |
entity captured by a *lambda-expression* is odr-used (
|
| 735 |
[[basic.def.odr]]) in the scope containing the *lambda-expression*. If
|
| 736 |
-
`this` is captured by a local lambda expression, its nearest enclosing
|
| 737 |
function shall be a non-static member function. If a *lambda-expression*
|
| 738 |
or an instantiation of the function call operator template of a generic
|
| 739 |
lambda odr-uses ([[basic.def.odr]]) `this` or a variable with automatic
|
| 740 |
storage duration from its reaching scope, that entity shall be captured
|
| 741 |
by the *lambda-expression*. If a *lambda-expression* captures an entity
|
| 742 |
and that entity is not defined or captured in the immediately enclosing
|
| 743 |
lambda expression or function, the program is ill-formed.
|
| 744 |
|
|
|
|
|
|
|
| 745 |
``` cpp
|
| 746 |
void f1(int i) {
|
| 747 |
int const N = 20;
|
| 748 |
auto m1 = [=]{
|
| 749 |
int const M = 30;
|
| 750 |
auto m2 = [i]{
|
| 751 |
int x[N][M]; // OK: N and M are not odr-used
|
| 752 |
-
x[0][0] = i; // OK: i is explicitly captured by m2
|
| 753 |
-
// and implicitly captured by m1
|
| 754 |
};
|
| 755 |
};
|
| 756 |
struct s1 {
|
| 757 |
int f;
|
| 758 |
void work(int n) {
|
| 759 |
int m = n*n;
|
| 760 |
int j = 40;
|
| 761 |
auto m3 = [this,m] {
|
| 762 |
auto m4 = [&,j] { // error: j not captured by m3
|
| 763 |
-
int x = n; // error: n implicitly captured by m4
|
| 764 |
-
|
| 765 |
-
x += m; // OK: m implicitly captured by m4
|
| 766 |
-
// and explicitly captured by m3
|
| 767 |
x += i; // error: i is outside of the reaching scope
|
| 768 |
-
x += f; // OK: this captured implicitly by m4
|
| 769 |
-
// and explicitly by m3
|
| 770 |
};
|
| 771 |
};
|
| 772 |
}
|
| 773 |
};
|
| 774 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
```
|
| 776 |
|
|
|
|
|
|
|
| 777 |
A *lambda-expression* appearing in a default argument shall not
|
| 778 |
implicitly or explicitly capture any entity.
|
| 779 |
|
|
|
|
|
|
|
| 780 |
``` cpp
|
| 781 |
void f2() {
|
| 782 |
int i = 1;
|
| 783 |
void g1(int = ([i]{ return i; })()); // ill-formed
|
| 784 |
void g2(int = ([i]{ return 0; })()); // ill-formed
|
|
@@ -786,38 +1061,105 @@ void f2() {
|
|
| 786 |
void g4(int = ([=]{ return 0; })()); // OK
|
| 787 |
void g5(int = ([]{ return sizeof i; })()); // OK
|
| 788 |
}
|
| 789 |
```
|
| 790 |
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 802 |
|
| 803 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 804 |
captured but not captured by copy. It is unspecified whether additional
|
| 805 |
unnamed non-static data members are declared in the closure type for
|
| 806 |
-
entities captured by reference.
|
| 807 |
-
be
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
|
| 809 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 810 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 811 |
`m2`’s capture is transformed as follows:
|
| 812 |
|
| 813 |
- if `m1` captures the entity by copy, `m2` captures the corresponding
|
| 814 |
non-static data member of `m1`’s closure type;
|
| 815 |
- if `m1` captures the entity by reference, `m2` captures the same
|
| 816 |
entity captured by `m1`.
|
| 817 |
|
| 818 |
-
|
|
|
|
|
|
|
| 819 |
`123234`.
|
| 820 |
|
| 821 |
``` cpp
|
| 822 |
int a = 1, b = 1, c = 1;
|
| 823 |
auto m1 = [a, &b, &c]() mutable {
|
|
@@ -831,100 +1173,129 @@ auto m1 = [a, &b, &c]() mutable {
|
|
| 831 |
a = 2; b = 2; c = 2;
|
| 832 |
m1();
|
| 833 |
std::cout << a << b << c;
|
| 834 |
```
|
| 835 |
|
| 836 |
-
|
| 837 |
-
*lambda-expression* that is an odr-use ([[basic.def.odr]]) of an entity
|
| 838 |
-
captured by copy is transformed into an access to the corresponding
|
| 839 |
-
unnamed data member of the closure type. An *id-expression* that is not
|
| 840 |
-
an odr-use refers to the original entity, never to a member of the
|
| 841 |
-
closure type. Furthermore, such an *id-expression* does not cause the
|
| 842 |
-
implicit capture of the entity. If `this` is captured, each odr-use of
|
| 843 |
-
`this` is transformed into an access to the corresponding unnamed data
|
| 844 |
-
member of the closure type, cast ([[expr.cast]]) to the type of `this`.
|
| 845 |
-
The cast ensures that the transformed expression is a prvalue.
|
| 846 |
-
|
| 847 |
-
``` cpp
|
| 848 |
-
void f(const int*);
|
| 849 |
-
void g() {
|
| 850 |
-
const int N = 10;
|
| 851 |
-
[=] {
|
| 852 |
-
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 853 |
-
f(&N); // OK: causes N to be captured; &N points to the
|
| 854 |
-
// corresponding member of the closure type
|
| 855 |
-
};
|
| 856 |
-
}
|
| 857 |
-
```
|
| 858 |
|
| 859 |
Every occurrence of `decltype((x))` where `x` is a possibly
|
| 860 |
parenthesized *id-expression* that names an entity of automatic storage
|
| 861 |
duration is treated as if `x` were transformed into an access to a
|
| 862 |
corresponding data member of the closure type that would have been
|
| 863 |
declared if `x` were an odr-use of the denoted entity.
|
| 864 |
|
|
|
|
|
|
|
| 865 |
``` cpp
|
| 866 |
void f3() {
|
| 867 |
float x, &r = x;
|
| 868 |
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
|
| 869 |
decltype(x) y1; // y1 has type float
|
| 870 |
-
decltype((x)) y2 = y1; // y2 has type float const& because this lambda
|
| 871 |
-
// is not mutable and x is an lvalue
|
| 872 |
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
|
| 873 |
decltype((r)) r2 = y2; // r2 has type float const&
|
| 874 |
};
|
| 875 |
}
|
| 876 |
```
|
| 877 |
|
| 878 |
-
|
| 879 |
-
[[dcl.fct.def.delete]]) default constructor and a deleted copy
|
| 880 |
-
assignment operator. It has an implicitly-declared copy constructor (
|
| 881 |
-
[[class.copy]]) and may have an implicitly-declared move constructor (
|
| 882 |
-
[[class.copy]]). The copy/move constructor is implicitly defined in the
|
| 883 |
-
same way as any other implicitly declared copy/move constructor would be
|
| 884 |
-
implicitly defined.
|
| 885 |
-
|
| 886 |
-
The closure type associated with a *lambda-expression* has an
|
| 887 |
-
implicitly-declared destructor ([[class.dtor]]).
|
| 888 |
|
| 889 |
When the *lambda-expression* is evaluated, the entities that are
|
| 890 |
captured by copy are used to direct-initialize each corresponding
|
| 891 |
non-static data member of the resulting closure object, and the
|
| 892 |
non-static data members corresponding to the *init-capture*s are
|
| 893 |
initialized as indicated by the corresponding *initializer* (which may
|
| 894 |
be copy- or direct-initialization). (For array members, the array
|
| 895 |
elements are direct-initialized in increasing subscript order.) These
|
| 896 |
initializations are performed in the (unspecified) order in which the
|
| 897 |
-
non-static data members are declared.
|
| 898 |
-
will occur in the reverse order of the constructions.
|
| 899 |
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
|
|
|
|
|
|
|
|
|
| 904 |
|
| 905 |
A *simple-capture* followed by an ellipsis is a pack expansion (
|
| 906 |
[[temp.variadic]]). An *init-capture* followed by an ellipsis is
|
| 907 |
ill-formed.
|
| 908 |
|
|
|
|
|
|
|
| 909 |
``` cpp
|
| 910 |
template<class... Args>
|
| 911 |
void f(Args... args) {
|
| 912 |
auto lm = [&, args...] { return g(args...); };
|
| 913 |
lm();
|
| 914 |
}
|
| 915 |
```
|
| 916 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 917 |
## Postfix expressions <a id="expr.post">[[expr.post]]</a>
|
| 918 |
|
| 919 |
Postfix expressions group left-to-right.
|
| 920 |
|
| 921 |
``` bnf
|
| 922 |
postfix-expression:
|
| 923 |
primary-expression
|
| 924 |
-
postfix-expression '['
|
| 925 |
-
postfix-expression '[' braced-init-list ']'
|
| 926 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 927 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 928 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 929 |
simple-type-specifier braced-init-list
|
| 930 |
typename-specifier braced-init-list
|
|
@@ -949,213 +1320,247 @@ expression-list:
|
|
| 949 |
|
| 950 |
``` bnf
|
| 951 |
pseudo-destructor-name:
|
| 952 |
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 953 |
nested-name-specifier 'template' simple-template-id ':: ~' type-name
|
| 954 |
-
|
| 955 |
'~' decltype-specifier
|
| 956 |
```
|
| 957 |
|
| 958 |
-
The `>` token following the in a `dynamic_cast`,
|
| 959 |
-
`reinterpret_cast`, or `const_cast` may be the product of
|
| 960 |
-
`>{>}` token by two consecutive `>` tokens (
|
|
|
|
| 961 |
|
| 962 |
### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 963 |
|
| 964 |
A postfix expression followed by an expression in square brackets is a
|
| 965 |
-
postfix expression. One of the expressions shall
|
| 966 |
-
`T`” or “pointer to `T`” and the other shall
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 973 |
|
| 974 |
A *braced-init-list* shall not be used with the built-in subscript
|
| 975 |
operator.
|
| 976 |
|
| 977 |
### Function call <a id="expr.call">[[expr.call]]</a>
|
| 978 |
|
| 979 |
A function call is a postfix expression followed by parentheses
|
| 980 |
containing a possibly empty, comma-separated list of
|
| 981 |
*initializer-clause*s which constitute the arguments to the function.
|
| 982 |
-
The postfix expression shall have function type or
|
| 983 |
type. For a call to a non-member function or to a static member
|
| 984 |
function, the postfix expression shall be either an lvalue that refers
|
| 985 |
to a function (in which case the function-to-pointer standard
|
| 986 |
conversion ([[conv.func]]) is suppressed on the postfix expression), or
|
| 987 |
-
it shall have
|
| 988 |
-
expression whose function type
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
| 992 |
-
[[class.
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
function call of the form `f()` is interpreted as `(*this).f()` (see
|
| 999 |
-
[[class.mfct.non-static]]). If a function or member function name is
|
| 1000 |
-
used, the name can be overloaded (Clause [[over]]), in which case the
|
| 1001 |
-
appropriate function shall be selected according to the rules in
|
| 1002 |
-
[[over.match]]. If the selected function is non-virtual, or if the
|
| 1003 |
-
*id-expression* in the class member access expression is a
|
| 1004 |
-
*qualified-id*, that function is called. Otherwise, its final
|
| 1005 |
-
overrider ([[class.virtual]]) in the dynamic type of the object
|
| 1006 |
-
expression is called; such a call is referred to as a *virtual function
|
| 1007 |
-
call*. the dynamic type is the type of the object referred to by the
|
| 1008 |
-
current value of the object expression. [[class.cdtor]] describes the
|
| 1009 |
-
behavior of virtual function calls when the object expression refers to
|
| 1010 |
-
an object under construction or destruction.
|
| 1011 |
|
| 1012 |
-
|
| 1013 |
-
[[
|
| 1014 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1015 |
|
| 1016 |
If the *postfix-expression* designates a destructor ([[class.dtor]]),
|
| 1017 |
the type of the function call expression is `void`; otherwise, the type
|
| 1018 |
of the function call expression is the return type of the statically
|
| 1019 |
chosen function (i.e., ignoring the `virtual` keyword), even if the type
|
| 1020 |
of the function actually called is different. This return type shall be
|
| 1021 |
-
an object type, a reference type or cv
|
| 1022 |
|
| 1023 |
When a function is called, each parameter ([[dcl.fct]]) shall be
|
| 1024 |
initialized ([[dcl.init]], [[class.copy]], [[class.ctor]]) with its
|
| 1025 |
-
corresponding argument.
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
[
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
[[class.
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
-
parameter
|
| 1060 |
-
|
| 1061 |
-
|
| 1062 |
-
|
| 1063 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1064 |
[[dcl.array]], [[class.temporary]]). In addition, it is possible to
|
| 1065 |
-
modify the values of
|
|
|
|
| 1066 |
|
| 1067 |
A function can be declared to accept fewer arguments (by declaring
|
| 1068 |
default arguments ([[dcl.fct.default]])) or more arguments (by using
|
| 1069 |
the ellipsis, `...`, or a function parameter pack ([[dcl.fct]])) than
|
| 1070 |
the number of parameters in the function definition ([[dcl.fct.def]]).
|
| 1071 |
-
|
| 1072 |
-
|
|
|
|
|
|
|
| 1073 |
|
| 1074 |
When there is no parameter for a given argument, the argument is passed
|
| 1075 |
in such a way that the receiving function can obtain the value of the
|
| 1076 |
-
argument by invoking `va_arg` ([[support.runtime]]).
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
|
|
|
|
|
|
|
|
|
|
| 1081 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1082 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 1083 |
conversions are performed on the argument expression. An argument that
|
| 1084 |
-
has
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
|
| 1092 |
enumeration type that is subject to the integral promotions (
|
| 1093 |
-
[[conv.prom]]), or a floating
|
| 1094 |
-
point promotion ([[conv.fpprom]]), the value of the argument
|
| 1095 |
-
converted to the promoted type before the call. These promotions are
|
| 1096 |
referred to as the *default argument promotions*.
|
| 1097 |
|
| 1098 |
-
The evaluations of the postfix expression and of the arguments are all
|
| 1099 |
-
unsequenced relative to one another. All side effects of argument
|
| 1100 |
-
evaluations are sequenced before the function is entered (see
|
| 1101 |
-
[[intro.execution]]).
|
| 1102 |
-
|
| 1103 |
Recursive calls are permitted, except to the `main` function (
|
| 1104 |
[[basic.start.main]]).
|
| 1105 |
|
| 1106 |
A function call is an lvalue if the result type is an lvalue reference
|
| 1107 |
type or an rvalue reference to function type, an xvalue if the result
|
| 1108 |
type is an rvalue reference to object type, and a prvalue otherwise.
|
| 1109 |
|
| 1110 |
-
If a function call is a prvalue of object type:
|
| 1111 |
-
|
| 1112 |
-
- if the function call is either
|
| 1113 |
-
- the operand of a *decltype-specifier* or
|
| 1114 |
-
- the right operand of a comma operator that is the operand of a
|
| 1115 |
-
*decltype-specifier*,
|
| 1116 |
-
|
| 1117 |
-
a temporary object is not introduced for the prvalue. The type of the
|
| 1118 |
-
prvalue may be incomplete. as a result, storage is not allocated for
|
| 1119 |
-
the prvalue and it is not destroyed; thus, a class type is not
|
| 1120 |
-
instantiated as a result of being the type of a function call in this
|
| 1121 |
-
context. This is true regardless of whether the expression uses
|
| 1122 |
-
function call notation or operator notation ([[over.match.oper]]).
|
| 1123 |
-
unlike the rule for a *decltype-specifier* that considers whether an
|
| 1124 |
-
*id-expression* is parenthesized ([[dcl.type.simple]]), parentheses
|
| 1125 |
-
have no special meaning in this context.
|
| 1126 |
-
- otherwise, the type of the prvalue shall be complete.
|
| 1127 |
-
|
| 1128 |
### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 1129 |
|
| 1130 |
A *simple-type-specifier* ([[dcl.type.simple]]) or
|
| 1131 |
*typename-specifier* ([[temp.res]]) followed by a parenthesized
|
| 1132 |
-
*expression-list*
|
| 1133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1134 |
conversion expression is equivalent (in definedness, and if defined in
|
| 1135 |
meaning) to the corresponding cast expression ([[expr.cast]]). If the
|
| 1136 |
-
type
|
| 1137 |
-
|
| 1138 |
-
|
| 1139 |
-
[[
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
prvalue.
|
| 1143 |
-
|
| 1144 |
-
The expression `T()`, where `T` is a *simple-type-specifier* or
|
| 1145 |
-
*typename-specifier* for a non-array complete object type or the
|
| 1146 |
-
(possibly cv-qualified) `void` type, creates a prvalue of the specified
|
| 1147 |
-
type, whose value is that produced by value-initializing ([[dcl.init]])
|
| 1148 |
-
an object of type `T`; no initialization is done for the `void()` case.
|
| 1149 |
-
if `T` is a non-class type that is cv-qualified, the *cv-qualifier*s are
|
| 1150 |
-
discarded when determining the type of the resulting prvalue (Clause
|
| 1151 |
-
[[expr]]).
|
| 1152 |
-
|
| 1153 |
-
Similarly, a *simple-type-specifier* or *typename-specifier* followed by
|
| 1154 |
-
a *braced-init-list* creates a temporary object of the specified type
|
| 1155 |
-
direct-list-initialized ([[dcl.init.list]]) with the specified
|
| 1156 |
-
*braced-init-list*, and its value is that temporary object as a prvalue.
|
| 1157 |
|
| 1158 |
### Pseudo destructor call <a id="expr.pseudo">[[expr.pseudo]]</a>
|
| 1159 |
|
| 1160 |
The use of a *pseudo-destructor-name* after a dot `.` or arrow `->`
|
| 1161 |
operator represents the destructor for the non-class type denoted by
|
|
@@ -1173,40 +1578,45 @@ the object type and of the type designated by the
|
|
| 1173 |
|
| 1174 |
``` bnf
|
| 1175 |
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 1176 |
```
|
| 1177 |
|
| 1178 |
-
shall designate the same scalar type.
|
| 1179 |
|
| 1180 |
### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 1181 |
|
| 1182 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 1183 |
followed by the keyword `template` ([[temp.names]]), and then followed
|
| 1184 |
by an *id-expression*, is a postfix expression. The postfix expression
|
| 1185 |
before the dot or arrow is evaluated;[^6] the result of that evaluation,
|
| 1186 |
together with the *id-expression*, determines the result of the entire
|
| 1187 |
postfix expression.
|
| 1188 |
|
| 1189 |
-
For the first option (dot) the first expression shall
|
| 1190 |
-
class type. For the second option (arrow) the first
|
| 1191 |
-
|
| 1192 |
-
converted to the equivalent form `(*(E1)).E2`;
|
| 1193 |
-
[[expr.ref]] will address only the first option
|
| 1194 |
-
case, the *id-expression* shall name a member of
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
| 1198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1199 |
|
| 1200 |
Abbreviating *postfix-expression.id-expression* as `E1.E2`, `E1` is
|
| 1201 |
-
called the *object expression*.
|
| 1202 |
-
|
| 1203 |
-
|
| 1204 |
-
|
| 1205 |
-
|
|
|
|
| 1206 |
|
| 1207 |
-
If `E2` is declared to have type “reference to `T`
|
| 1208 |
lvalue; the type of `E1.E2` is `T`. Otherwise, one of the following
|
| 1209 |
rules applies.
|
| 1210 |
|
| 1211 |
- If `E2` is a static data member and the type of `E2` is `T`, then
|
| 1212 |
`E1.E2` is an lvalue; the expression designates the named member of
|
|
@@ -1231,63 +1641,75 @@ rules applies.
|
|
| 1231 |
lvalue; the expression designates the static member function. The
|
| 1232 |
type of `E1.E2` is the same type as that of `E2`, namely “function
|
| 1233 |
of parameter-type-list returning `T`”.
|
| 1234 |
- Otherwise, if `E1.E2` refers to a non-static member function and the
|
| 1235 |
type of `E2` is “function of parameter-type-list *cv*
|
| 1236 |
-
*ref-qualifierₒₚₜ
|
| 1237 |
expression designates a non-static member function. The expression
|
| 1238 |
can be used only as the left-hand operand of a member function
|
| 1239 |
-
call ([[class.mfct]]). Any redundant set of parentheses
|
| 1240 |
-
the expression is ignored (
|
| 1241 |
-
|
|
|
|
| 1242 |
- If `E2` is a nested type, the expression `E1.E2` is ill-formed.
|
| 1243 |
- If `E2` is a member enumerator and the type of `E2` is `T`, the
|
| 1244 |
expression `E1.E2` is a prvalue. The type of `E1.E2` is `T`.
|
| 1245 |
|
| 1246 |
If `E2` is a non-static data member or a non-static member function, the
|
| 1247 |
program is ill-formed if the class of which `E2` is directly a member is
|
| 1248 |
an ambiguous base ([[class.member.lookup]]) of the naming class (
|
| 1249 |
-
[[class.access.base]]) of `E2`.
|
| 1250 |
-
|
| 1251 |
-
|
|
|
|
|
|
|
| 1252 |
|
| 1253 |
### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 1254 |
|
| 1255 |
-
The value of a postfix `++` expression is the value of its operand.
|
| 1256 |
-
|
| 1257 |
-
|
| 1258 |
-
|
| 1259 |
-
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
|
|
|
|
|
|
| 1263 |
indeterminately-sequenced function call, the operation of postfix `++`
|
| 1264 |
-
is a single evaluation.
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1269 |
|
| 1270 |
The operand of postfix `\dcr` is decremented analogously to the postfix
|
| 1271 |
-
`++` operator
|
| 1272 |
-
|
|
|
|
|
|
|
| 1273 |
|
| 1274 |
### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 1275 |
|
| 1276 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 1277 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 1278 |
-
reference to a complete class type, or “pointer to *cv* `void`
|
| 1279 |
`dynamic_cast` operator shall not cast away constness (
|
| 1280 |
[[expr.const.cast]]).
|
| 1281 |
|
| 1282 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 1283 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 1284 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 1285 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 1286 |
-
is an rvalue reference type, `v` shall be
|
| 1287 |
-
|
| 1288 |
-
by `T`.
|
| 1289 |
|
| 1290 |
If the type of `v` is the same as `T`, or it is the same as `T` except
|
| 1291 |
that the class object type in `T` is more cv-qualified than the class
|
| 1292 |
object type in `v`, the result is `v` (converted if necessary).
|
| 1293 |
|
|
@@ -1297,33 +1719,35 @@ result is the null pointer value of type `T`.
|
|
| 1297 |
If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
| 1298 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 1299 |
unique `B` subobject of the `D` object pointed to by `v`. Similarly, if
|
| 1300 |
`T` is “reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B`
|
| 1301 |
is a base class of `D`, the result is the unique `B` subobject of the
|
| 1302 |
-
`D` object referred to by `v`.
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
| 1306 |
-
|
| 1307 |
|
| 1308 |
``` cpp
|
| 1309 |
struct B { };
|
| 1310 |
struct D : B { };
|
| 1311 |
void foo(D* dp) {
|
| 1312 |
B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
|
| 1313 |
}
|
| 1314 |
```
|
| 1315 |
|
|
|
|
|
|
|
| 1316 |
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
|
| 1317 |
type ([[class.virtual]]).
|
| 1318 |
|
| 1319 |
-
If `T` is “pointer to *cv* `void`
|
| 1320 |
-
most derived object pointed to by `v`. Otherwise, a
|
| 1321 |
applied to see if the object pointed or referred to by `v` can be
|
| 1322 |
converted to the type pointed or referred to by `T`.
|
| 1323 |
|
| 1324 |
-
If `C` is the class type to which `T` points or refers, the
|
| 1325 |
check logically executes as follows:
|
| 1326 |
|
| 1327 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 1328 |
points (refers) to a `public` base class subobject of a `C` object,
|
| 1329 |
and if only one object of type `C` is derived from the subobject
|
|
@@ -1332,17 +1756,19 @@ check logically executes as follows:
|
|
| 1332 |
- Otherwise, if `v` points (refers) to a `public` base class subobject
|
| 1333 |
of the most derived object, and the type of the most derived object
|
| 1334 |
has a base class, of type `C`, that is unambiguous and `public`, the
|
| 1335 |
result points (refers) to the `C` subobject of the most derived
|
| 1336 |
object.
|
| 1337 |
-
- Otherwise, the
|
| 1338 |
|
| 1339 |
The value of a failed cast to pointer type is the null pointer value of
|
| 1340 |
the required result type. A failed cast to reference type throws an
|
| 1341 |
exception ([[except.throw]]) of a type that would match a handler (
|
| 1342 |
[[except.handle]]) of type `std::bad_cast` ([[bad.cast]]).
|
| 1343 |
|
|
|
|
|
|
|
| 1344 |
``` cpp
|
| 1345 |
class A { virtual void f(); };
|
| 1346 |
class B { virtual void g(); };
|
| 1347 |
class D : public virtual A, private B { };
|
| 1348 |
void g() {
|
|
@@ -1351,27 +1777,28 @@ void g() {
|
|
| 1351 |
A* ap = &d; // public derivation, no cast needed
|
| 1352 |
D& dr = dynamic_cast<D&>(*bp); // fails
|
| 1353 |
ap = dynamic_cast<A*>(bp); // fails
|
| 1354 |
bp = dynamic_cast<B*>(ap); // fails
|
| 1355 |
ap = dynamic_cast<A*>(&d); // succeeds
|
| 1356 |
-
bp = dynamic_cast<B*>(&d); // ill-formed (not a
|
| 1357 |
}
|
| 1358 |
|
| 1359 |
class E : public D, public B { };
|
| 1360 |
class F : public E, public D { };
|
| 1361 |
void h() {
|
| 1362 |
F f;
|
| 1363 |
A* ap = &f; // succeeds: finds unique A
|
| 1364 |
-
D* dp = dynamic_cast<D*>(ap); // fails: yields
|
| 1365 |
-
// f has two D subobjects
|
| 1366 |
E* ep = (E*)ap; // ill-formed: cast from virtual base
|
| 1367 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 1368 |
}
|
| 1369 |
```
|
| 1370 |
|
| 1371 |
-
|
| 1372 |
-
|
|
|
|
|
|
|
| 1373 |
|
| 1374 |
### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 1375 |
|
| 1376 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 1377 |
`std::type_info` ([[type.info]]) and dynamic type `const`
|
|
@@ -1395,122 +1822,146 @@ value ([[conv.ptr]]), the `typeid` expression throws an exception (
|
|
| 1395 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 1396 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 1397 |
representing the static type of the expression. Lvalue-to-rvalue (
|
| 1398 |
[[conv.lval]]), array-to-pointer ([[conv.array]]), and
|
| 1399 |
function-to-pointer ([[conv.func]]) conversions are not applied to the
|
| 1400 |
-
expression. If the
|
| 1401 |
-
|
| 1402 |
-
(Clause [[expr]]).
|
| 1403 |
|
| 1404 |
When `typeid` is applied to a *type-id*, the result refers to a
|
| 1405 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 1406 |
-
type of the *type-id* is a reference to a possibly
|
| 1407 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 1408 |
-
object representing the
|
| 1409 |
the *type-id* is a class type or a reference to a class type, the class
|
| 1410 |
shall be completely-defined.
|
| 1411 |
|
| 1412 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
| 1413 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 1414 |
representing the cv-unqualified type.
|
| 1415 |
|
|
|
|
|
|
|
| 1416 |
``` cpp
|
| 1417 |
-
class D {
|
| 1418 |
D d1;
|
| 1419 |
const D d2;
|
| 1420 |
|
| 1421 |
typeid(d1) == typeid(d2); // yields true
|
| 1422 |
typeid(D) == typeid(const D); // yields true
|
| 1423 |
typeid(D) == typeid(d2); // yields true
|
| 1424 |
typeid(D) == typeid(const D&); // yields true
|
| 1425 |
```
|
| 1426 |
|
|
|
|
|
|
|
| 1427 |
If the header `<typeinfo>` ([[type.info]]) is not included prior to a
|
| 1428 |
use of `typeid`, the program is ill-formed.
|
| 1429 |
|
| 1430 |
-
[[class.cdtor]] describes the behavior of `typeid` applied
|
| 1431 |
-
under construction or destruction.
|
| 1432 |
|
| 1433 |
### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
| 1434 |
|
| 1435 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 1436 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 1437 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 1438 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 1439 |
otherwise, the result is a prvalue. The `static_cast` operator shall not
|
| 1440 |
cast away constness ([[expr.const.cast]]).
|
| 1441 |
|
| 1442 |
-
An lvalue of type “*cv1* `B`
|
| 1443 |
-
type “reference to *cv2* `D`
|
| 1444 |
-
[[class.derived]]) from `B`, if
|
| 1445 |
-
|
| 1446 |
-
|
| 1447 |
-
|
| 1448 |
-
|
| 1449 |
-
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
|
| 1453 |
-
|
|
|
|
|
|
|
| 1454 |
|
| 1455 |
``` cpp
|
| 1456 |
struct B { };
|
| 1457 |
struct D : public B { };
|
| 1458 |
D d;
|
| 1459 |
B &br = d;
|
| 1460 |
|
| 1461 |
static_cast<D&>(br); // produces lvalue to the original d object
|
| 1462 |
```
|
| 1463 |
|
| 1464 |
-
|
| 1465 |
-
cast to type “rvalue reference to *cv2* `T2`” if “*cv2* `T2`” is
|
| 1466 |
-
reference-compatible with “*cv1* `T1`” ([[dcl.init.ref]]). If the value
|
| 1467 |
-
is not a bit-field, the result refers to the object or the specified
|
| 1468 |
-
base class subobject thereof; otherwise, the lvalue-to-rvalue
|
| 1469 |
-
conversion ([[conv.lval]]) is applied to the bit-field and the
|
| 1470 |
-
resulting prvalue is used as the *expression* of the `static_cast` for
|
| 1471 |
-
the remainder of this section. If `T2` is an inaccessible (Clause
|
| 1472 |
-
[[class.access]]) or ambiguous ([[class.member.lookup]]) base class of
|
| 1473 |
-
`T1`, a program that necessitates such a cast is ill-formed.
|
| 1474 |
|
| 1475 |
-
An
|
| 1476 |
-
`
|
| 1477 |
-
|
| 1478 |
-
|
| 1479 |
-
|
| 1480 |
-
|
| 1481 |
-
|
| 1482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1483 |
|
| 1484 |
Otherwise, the `static_cast` shall perform one of the conversions listed
|
| 1485 |
below. No other conversion shall be performed explicitly using a
|
| 1486 |
`static_cast`.
|
| 1487 |
|
| 1488 |
Any expression can be explicitly converted to type cv `void`, in which
|
| 1489 |
case it becomes a discarded-value expression (Clause [[expr]]).
|
| 1490 |
-
|
| 1491 |
-
|
| 1492 |
-
the
|
| 1493 |
-
|
|
|
|
| 1494 |
|
| 1495 |
The inverse of any standard conversion sequence (Clause [[conv]]) not
|
| 1496 |
containing an lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1497 |
[[conv.array]]), function-to-pointer ([[conv.func]]), null pointer (
|
| 1498 |
-
[[conv.ptr]]), null member pointer ([[conv.mem]]),
|
| 1499 |
-
[[conv.bool]])
|
| 1500 |
-
`static_cast`. A program is ill-formed if
|
| 1501 |
-
perform the inverse of an ill-formed standard
|
|
|
|
|
|
|
|
|
|
| 1502 |
|
| 1503 |
``` cpp
|
| 1504 |
struct B { };
|
| 1505 |
struct D : private B { };
|
| 1506 |
void f() {
|
| 1507 |
-
static_cast<D*>((B*)0); //
|
| 1508 |
-
static_cast<int B::*>((int D::*)0); //
|
| 1509 |
}
|
| 1510 |
```
|
| 1511 |
|
|
|
|
|
|
|
| 1512 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1513 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) conversions
|
| 1514 |
are applied to the operand. Such a `static_cast` is subject to the
|
| 1515 |
restriction that the explicit conversion does not cast away constness (
|
| 1516 |
[[expr.const.cast]]), and the following additional rules for specific
|
|
@@ -1524,65 +1975,72 @@ unchanged if the original value can be represented by the specified
|
|
| 1524 |
type. Otherwise, the resulting value is unspecified. A value of a scoped
|
| 1525 |
enumeration type can also be explicitly converted to a floating-point
|
| 1526 |
type; the result is the same as that of converting from the original
|
| 1527 |
value to the floating-point type.
|
| 1528 |
|
| 1529 |
-
A value of integral or enumeration type can be explicitly converted to
|
| 1530 |
-
|
| 1531 |
-
within the range of the enumeration values ([[dcl.enum]]).
|
| 1532 |
-
the
|
| 1533 |
-
|
| 1534 |
-
|
| 1535 |
-
|
| 1536 |
-
|
| 1537 |
|
| 1538 |
-
A prvalue of type “pointer to *cv1* `B`
|
| 1539 |
-
be converted to a prvalue of type “pointer to *cv2* `D`
|
| 1540 |
-
class derived (Clause [[class.derived]]) from `B`, if
|
| 1541 |
-
|
| 1542 |
-
|
| 1543 |
-
|
| 1544 |
-
`
|
| 1545 |
-
value ([[conv.ptr]]) is converted to the null pointer
|
| 1546 |
-
destination type. If the prvalue of type “pointer to *cv1*
|
| 1547 |
-
to a `B` that is actually a subobject of an object of type
|
| 1548 |
-
resulting pointer points to the enclosing object of type `D`.
|
| 1549 |
-
the behavior is undefined.
|
| 1550 |
|
| 1551 |
A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
|
| 1552 |
-
converted to a prvalue of type “pointer to member of `B`
|
| 1553 |
-
`T`, where `B` is a base class (Clause [[class.derived]]) of `D`, if
|
| 1554 |
-
|
| 1555 |
-
|
| 1556 |
-
|
| 1557 |
-
|
| 1558 |
-
to the null member pointer value of
|
| 1559 |
-
contains the original member, or is a
|
| 1560 |
-
|
| 1561 |
-
|
| 1562 |
-
|
| 1563 |
-
|
| 1564 |
-
|
|
|
|
|
|
|
|
|
|
| 1565 |
|
| 1566 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
| 1567 |
-
prvalue of type “pointer to *cv2* `T`
|
| 1568 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 1569 |
-
*cv1*.
|
| 1570 |
-
|
| 1571 |
-
|
| 1572 |
-
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
|
| 1576 |
-
|
|
|
|
|
|
|
| 1577 |
|
| 1578 |
``` cpp
|
| 1579 |
T* p1 = new T;
|
| 1580 |
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
|
| 1581 |
bool b = p1 == p2; // b will have the value true.
|
| 1582 |
```
|
| 1583 |
|
|
|
|
|
|
|
| 1584 |
### Reinterpret cast <a id="expr.reinterpret.cast">[[expr.reinterpret.cast]]</a>
|
| 1585 |
|
| 1586 |
The result of the expression `reinterpret_cast<T>(v)` is the result of
|
| 1587 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 1588 |
type or an rvalue reference to function type, the result is an lvalue;
|
|
@@ -1597,61 +2055,77 @@ performed explicitly using `reinterpret_cast`.
|
|
| 1597 |
The `reinterpret_cast` operator shall not cast away constness (
|
| 1598 |
[[expr.const.cast]]). An expression of integral, enumeration, pointer,
|
| 1599 |
or pointer-to-member type can be explicitly converted to its own type;
|
| 1600 |
such a cast yields the value of its operand.
|
| 1601 |
|
| 1602 |
-
The mapping performed by `reinterpret_cast` might, or might
|
| 1603 |
-
a representation different from the original
|
|
|
|
| 1604 |
|
| 1605 |
A pointer can be explicitly converted to any integral type large enough
|
| 1606 |
-
to hold it. The mapping function is implementation-defined.
|
| 1607 |
-
|
| 1608 |
-
|
| 1609 |
-
|
| 1610 |
-
|
| 1611 |
-
`
|
| 1612 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1613 |
|
| 1614 |
A value of integral type or enumeration type can be explicitly converted
|
| 1615 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 1616 |
any such exists on the implementation) and back to the same pointer type
|
| 1617 |
will have its original value; mappings between pointers and integers are
|
| 1618 |
-
otherwise *implementation-defined*.
|
| 1619 |
-
|
| 1620 |
-
|
|
|
|
|
|
|
| 1621 |
|
| 1622 |
A function pointer can be explicitly converted to a function pointer of
|
| 1623 |
-
a different type.
|
| 1624 |
-
|
| 1625 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
|
| 1629 |
-
|
| 1630 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1631 |
|
| 1632 |
An object pointer can be explicitly converted to an object pointer of a
|
| 1633 |
different type.[^12] When a prvalue `v` of object pointer type is
|
| 1634 |
converted to the object pointer type “pointer to cv `T`”, the result is
|
| 1635 |
-
`static_cast<cv T*>(static_cast<cv
|
| 1636 |
-
|
| 1637 |
-
|
| 1638 |
-
|
| 1639 |
-
|
|
|
|
|
|
|
| 1640 |
|
| 1641 |
Converting a function pointer to an object pointer type or vice versa is
|
| 1642 |
conditionally-supported. The meaning of such a conversion is
|
| 1643 |
*implementation-defined*, except that if an implementation supports
|
| 1644 |
conversions in both directions, converting a prvalue of one type to the
|
| 1645 |
other type and back, possibly with different cv-qualification, shall
|
| 1646 |
yield the original pointer value.
|
| 1647 |
|
| 1648 |
The null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 1649 |
-
value of the destination type.
|
| 1650 |
-
|
| 1651 |
-
pointer constant of
|
| 1652 |
-
pointer
|
|
|
|
|
|
|
| 1653 |
|
| 1654 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 1655 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 1656 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 1657 |
object types.[^13] The null member pointer value ([[conv.mem]]) is
|
|
@@ -1669,16 +2143,20 @@ result of this conversion is unspecified, except in the following cases:
|
|
| 1669 |
|
| 1670 |
A glvalue expression of type `T1` can be cast to the type “reference to
|
| 1671 |
`T2`” if an expression of type “pointer to `T1`” can be explicitly
|
| 1672 |
converted to the type “pointer to `T2`” using a `reinterpret_cast`. The
|
| 1673 |
result refers to the same object as the source glvalue, but with the
|
| 1674 |
-
specified type.
|
|
|
|
|
|
|
| 1675 |
`reinterpret_cast<T&>(x)` has the same effect as the conversion
|
| 1676 |
`*reinterpret_cast<T*>(&x)` with the built-in `&` and `*` operators (and
|
| 1677 |
-
similarly for `reinterpret_cast<T&&>(x)`).
|
| 1678 |
-
|
| 1679 |
-
|
|
|
|
|
|
|
| 1680 |
|
| 1681 |
### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 1682 |
|
| 1683 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 1684 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
|
@@ -1688,22 +2166,30 @@ otherwise, the result is a prvalue and the lvalue-to-rvalue (
|
|
| 1688 |
function-to-pointer ([[conv.func]]) standard conversions are performed
|
| 1689 |
on the expression `v`. Conversions that can be performed explicitly
|
| 1690 |
using `const_cast` are listed below. No other conversion shall be
|
| 1691 |
performed explicitly using `const_cast`.
|
| 1692 |
|
| 1693 |
-
Subject to the restrictions in this section, an expression
|
| 1694 |
-
to its own type using a `const_cast`
|
|
|
|
| 1695 |
|
| 1696 |
-
For two
|
|
|
|
|
|
|
| 1697 |
|
| 1698 |
-
|
| 1699 |
|
| 1700 |
-
|
| 1701 |
-
|
| 1702 |
-
|
| 1703 |
-
|
| 1704 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1705 |
|
| 1706 |
For two object types `T1` and `T2`, if a pointer to `T1` can be
|
| 1707 |
explicitly converted to the type “pointer to `T2`” using a `const_cast`,
|
| 1708 |
then the following conversions can also be made:
|
| 1709 |
|
|
@@ -1712,64 +2198,45 @@ then the following conversions can also be made:
|
|
| 1712 |
- a glvalue of type `T1` can be explicitly converted to an xvalue of
|
| 1713 |
type `T2` using the cast `const_cast<T2&&>`; and
|
| 1714 |
- if `T1` is a class type, a prvalue of type `T1` can be explicitly
|
| 1715 |
converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
|
| 1716 |
|
| 1717 |
-
The result of a reference `const_cast` refers to the original object
|
| 1718 |
-
|
| 1719 |
-
|
| 1720 |
-
pointers to data members and multi-level mixed pointers and pointers to
|
| 1721 |
-
data members ([[conv.qual]]), the rules for `const_cast` are the same
|
| 1722 |
-
as those used for pointers; the “member” aspect of a pointer to member
|
| 1723 |
-
is ignored when determining where the cv-qualifiers are added or removed
|
| 1724 |
-
by the `const_cast`. The result of a pointer to data member `const_cast`
|
| 1725 |
-
refers to the same member as the original (uncast) pointer to data
|
| 1726 |
-
member.
|
| 1727 |
|
| 1728 |
A null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 1729 |
value of the destination type. The null member pointer value (
|
| 1730 |
[[conv.mem]]) is converted to the null member pointer value of the
|
| 1731 |
destination type.
|
| 1732 |
|
| 1733 |
-
Depending on the type of the object, a write operation
|
| 1734 |
-
pointer, lvalue or pointer to data member resulting from a
|
| 1735 |
-
that casts away a const-qualifier[^15] may produce
|
| 1736 |
-
[[dcl.type.cv]]).
|
| 1737 |
|
| 1738 |
-
|
| 1739 |
-
|
| 1740 |
-
|
|
|
|
| 1741 |
|
| 1742 |
-
|
| 1743 |
-
type `T` there does not exist an implicit conversion (Clause [[conv]])
|
| 1744 |
-
from:
|
| 1745 |
-
|
| 1746 |
-
to
|
| 1747 |
|
| 1748 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 1749 |
lvalue reference cast or casting from an expression of type `T1` to an
|
| 1750 |
xvalue of type `T2` using an rvalue reference cast casts away constness
|
| 1751 |
if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
|
| 1752 |
to `T2`” casts away constness.
|
| 1753 |
|
| 1754 |
-
|
| 1755 |
-
|
| 1756 |
-
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
|
| 1760 |
-
pointer to
|
| 1761 |
-
|
| 1762 |
-
been cast away.
|
| 1763 |
-
|
| 1764 |
-
some conversions which involve only changes in cv-qualification cannot
|
| 1765 |
-
be done using `const_cast.` For instance, conversions between pointers
|
| 1766 |
-
to functions are not covered because such conversions lead to values
|
| 1767 |
-
whose use causes undefined behavior. For the same reasons, conversions
|
| 1768 |
-
between pointers to member functions, and in particular, the conversion
|
| 1769 |
-
from a pointer to a const member function to a pointer to a non-const
|
| 1770 |
-
member function, are not covered.
|
| 1771 |
|
| 1772 |
## Unary expressions <a id="expr.unary">[[expr.unary]]</a>
|
| 1773 |
|
| 1774 |
Expressions with unary operators group right-to-left.
|
| 1775 |
|
|
@@ -1797,61 +2264,79 @@ unary-operator: one of
|
|
| 1797 |
|
| 1798 |
The unary `*` operator performs *indirection*: the expression to which
|
| 1799 |
it is applied shall be a pointer to an object type, or a pointer to a
|
| 1800 |
function type and the result is an lvalue referring to the object or
|
| 1801 |
function to which the expression points. If the type of the expression
|
| 1802 |
-
is “pointer to `T`
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
to a
|
|
|
|
| 1807 |
|
| 1808 |
The result of each of the following unary operators is a prvalue.
|
| 1809 |
|
| 1810 |
The result of the unary `&` operator is a pointer to its operand. The
|
| 1811 |
operand shall be an lvalue or a *qualified-id*. If the operand is a
|
| 1812 |
-
*qualified-id* naming a non-static member `m` of some class
|
| 1813 |
-
type `T`, the result has type “pointer to member of class `C`
|
| 1814 |
-
`T`” and is a prvalue designating `C::m`. Otherwise, if the type
|
| 1815 |
-
expression is `T`, the result has type “pointer to `T`” and is a
|
| 1816 |
-
that is the address of the designated object ([[intro.memory]])
|
| 1817 |
-
pointer to the designated function.
|
| 1818 |
-
|
| 1819 |
-
cv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1820 |
|
| 1821 |
``` cpp
|
| 1822 |
struct A { int i; };
|
| 1823 |
struct B : A { };
|
| 1824 |
... &B::i ... // has type int A::*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1825 |
```
|
| 1826 |
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
|
|
|
|
|
|
| 1830 |
|
| 1831 |
A pointer to member is only formed when an explicit `&` is used and its
|
| 1832 |
-
operand is a *qualified-id* not enclosed in parentheses.
|
| 1833 |
-
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
|
| 1837 |
-
|
| 1838 |
-
to the type “pointer to function”
|
| 1839 |
-
|
| 1840 |
-
|
|
|
|
| 1841 |
|
| 1842 |
If `&` is applied to an lvalue of incomplete class type and the complete
|
| 1843 |
type declares `operator&()`, it is unspecified whether the operator has
|
| 1844 |
the built-in meaning or the operator function is called. The operand of
|
| 1845 |
`&` shall not be a bit-field.
|
| 1846 |
|
| 1847 |
The address of an overloaded function (Clause [[over]]) can be taken
|
| 1848 |
only in a context that uniquely determines which version of the
|
| 1849 |
-
overloaded function is referred to (see [[over.over]]).
|
| 1850 |
-
|
| 1851 |
-
|
| 1852 |
-
|
|
|
|
|
|
|
| 1853 |
|
| 1854 |
The operand of the unary `+` operator shall have arithmetic, unscoped
|
| 1855 |
enumeration, or pointer type and the result is the value of the
|
| 1856 |
argument. Integral promotion is performed on integral or enumeration
|
| 1857 |
operands. The type of the result is the type of the promoted operand.
|
|
@@ -1867,51 +2352,61 @@ The operand of the logical negation operator `!` is contextually
|
|
| 1867 |
converted to `bool` (Clause [[conv]]); its value is `true` if the
|
| 1868 |
converted operand is `false` and `false` otherwise. The type of the
|
| 1869 |
result is `bool`.
|
| 1870 |
|
| 1871 |
The operand of `~` shall have integral or unscoped enumeration type; the
|
| 1872 |
-
result is the
|
| 1873 |
performed. The type of the result is the type of the promoted operand.
|
| 1874 |
-
There is an ambiguity in the
|
| 1875 |
-
*class-name* or *decltype-specifier*. The ambiguity is resolved
|
| 1876 |
-
|
| 1877 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1878 |
|
| 1879 |
### Increment and decrement <a id="expr.pre.incr">[[expr.pre.incr]]</a>
|
| 1880 |
|
| 1881 |
-
The operand of prefix `++` is modified by adding `1`
|
| 1882 |
-
|
| 1883 |
-
|
| 1884 |
-
|
| 1885 |
-
|
| 1886 |
-
|
| 1887 |
-
equivalent to `x+=1` See the discussions of addition ([[expr.add]]) and
|
| 1888 |
-
assignment operators ([[expr.ass]]) for information on conversions.
|
| 1889 |
|
| 1890 |
-
|
| 1891 |
-
|
| 1892 |
-
|
| 1893 |
-
|
| 1894 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1895 |
|
| 1896 |
### Sizeof <a id="expr.sizeof">[[expr.sizeof]]</a>
|
| 1897 |
|
| 1898 |
The `sizeof` operator yields the number of bytes in the object
|
| 1899 |
representation of its operand. The operand is either an expression,
|
| 1900 |
which is an unevaluated operand (Clause [[expr]]), or a parenthesized
|
| 1901 |
*type-id*. The `sizeof` operator shall not be applied to an expression
|
| 1902 |
-
that has function or incomplete type, to
|
| 1903 |
-
|
| 1904 |
-
|
| 1905 |
-
|
| 1906 |
-
|
| 1907 |
-
|
| 1908 |
-
*
|
| 1909 |
-
`sizeof(
|
| 1910 |
-
implementation-defined.[^16]
|
| 1911 |
-
|
| 1912 |
-
|
|
|
|
|
|
|
| 1913 |
|
| 1914 |
When applied to a reference or a reference type, the result is the size
|
| 1915 |
of the referenced type. When applied to a class, the result is the
|
| 1916 |
number of bytes in an object of that class including any padding
|
| 1917 |
required for placing objects of that type in an array. The size of a
|
|
@@ -1924,40 +2419,50 @@ number of bytes in the array. This implies that the size of an array of
|
|
| 1924 |
The `sizeof` operator can be applied to a pointer to a function, but
|
| 1925 |
shall not be applied directly to a function.
|
| 1926 |
|
| 1927 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1928 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 1929 |
-
conversions are not applied to the operand of `sizeof`.
|
|
|
|
|
|
|
| 1930 |
|
| 1931 |
The identifier in a `sizeof...` expression shall name a parameter pack.
|
| 1932 |
The `sizeof...` operator yields the number of arguments provided for the
|
| 1933 |
parameter pack *identifier*. A `sizeof...` expression is a pack
|
| 1934 |
expansion ([[temp.variadic]]).
|
| 1935 |
|
|
|
|
|
|
|
| 1936 |
``` cpp
|
| 1937 |
template<class... Types>
|
| 1938 |
struct count {
|
| 1939 |
static const std::size_t value = sizeof...(Types);
|
| 1940 |
};
|
| 1941 |
```
|
| 1942 |
|
|
|
|
|
|
|
| 1943 |
The result of `sizeof` and `sizeof...` is a constant of type
|
| 1944 |
-
`std::size_t`.
|
| 1945 |
-
|
|
|
|
|
|
|
| 1946 |
|
| 1947 |
### New <a id="expr.new">[[expr.new]]</a>
|
| 1948 |
|
| 1949 |
The *new-expression* attempts to create an object of the *type-id* (
|
| 1950 |
[[dcl.name]]) or *new-type-id* to which it is applied. The type of that
|
| 1951 |
object is the *allocated type*. This type shall be a complete object
|
| 1952 |
type, but not an abstract class type or array thereof (
|
| 1953 |
-
[[intro.object]], [[basic.types]], [[class.abstract]]).
|
| 1954 |
-
|
| 1955 |
-
[
|
| 1956 |
-
|
| 1957 |
-
|
| 1958 |
-
cv-qualified type
|
|
|
|
|
|
|
| 1959 |
|
| 1960 |
``` bnf
|
| 1961 |
new-expression:
|
| 1962 |
'::'ₒₚₜ 'new' new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 1963 |
'::'ₒₚₜ 'new' new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
|
@@ -1990,50 +2495,67 @@ new-initializer:
|
|
| 1990 |
'(' expression-listₒₚₜ ')'
|
| 1991 |
braced-init-list
|
| 1992 |
```
|
| 1993 |
|
| 1994 |
Entities created by a *new-expression* have dynamic storage duration (
|
| 1995 |
-
[[basic.stc.dynamic]]).
|
| 1996 |
-
necessarily restricted to the scope in which it is created. If the
|
| 1997 |
-
entity is a non-array object, the *new-expression* returns a pointer to
|
| 1998 |
-
the object created. If it is an array, the *new-expression* returns a
|
| 1999 |
-
pointer to the initial element of the array.
|
| 2000 |
|
| 2001 |
-
|
| 2002 |
-
|
| 2003 |
|
| 2004 |
-
|
| 2005 |
-
|
| 2006 |
-
|
| 2007 |
|
| 2008 |
-
|
| 2009 |
-
*
|
| 2010 |
-
allocated type is
|
| 2011 |
-
|
|
|
|
|
|
|
|
|
|
| 2012 |
|
| 2013 |
``` cpp
|
| 2014 |
-
T x
|
| 2015 |
```
|
| 2016 |
|
|
|
|
|
|
|
| 2017 |
``` cpp
|
| 2018 |
new auto(1); // allocated type is int
|
| 2019 |
auto x = new auto('a'); // allocated type is char, x is of type char*
|
|
|
|
|
|
|
|
|
|
| 2020 |
```
|
| 2021 |
|
|
|
|
|
|
|
| 2022 |
The *new-type-id* in a *new-expression* is the longest possible sequence
|
| 2023 |
-
of *new-declarator*s.
|
| 2024 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2025 |
|
| 2026 |
``` cpp
|
| 2027 |
new int * i; // syntax error: parsed as (new int*) i, not as (new int)*i
|
| 2028 |
```
|
| 2029 |
|
| 2030 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 2031 |
|
| 2032 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2033 |
effects.
|
| 2034 |
|
|
|
|
|
|
|
| 2035 |
``` cpp
|
| 2036 |
new int(*[10])(); // error
|
| 2037 |
```
|
| 2038 |
|
| 2039 |
is ill-formed because the binding is
|
|
@@ -2048,66 +2570,85 @@ be used to create objects of compound types ([[basic.compound]]):
|
|
| 2048 |
``` cpp
|
| 2049 |
new (int (*[10])());
|
| 2050 |
```
|
| 2051 |
|
| 2052 |
allocates an array of `10` pointers to functions (taking no argument and
|
| 2053 |
-
returning `int`.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2054 |
|
| 2055 |
When the allocated object is an array (that is, the
|
| 2056 |
*noptr-new-declarator* syntax is used or the *new-type-id* or *type-id*
|
| 2057 |
denotes an array type), the *new-expression* yields a pointer to the
|
| 2058 |
-
initial element (if any) of the array.
|
| 2059 |
-
|
| 2060 |
-
*
|
| 2061 |
-
|
|
|
|
|
|
|
|
|
|
| 2062 |
|
| 2063 |
Every *constant-expression* in a *noptr-new-declarator* shall be a
|
| 2064 |
converted constant expression ([[expr.const]]) of type `std::size_t`
|
| 2065 |
and shall evaluate to a strictly positive value. The *expression* in a
|
| 2066 |
-
*noptr-new-declarator*is implicitly converted to `std::size_t`.
|
| 2067 |
-
|
| 2068 |
-
|
| 2069 |
-
|
| 2070 |
-
|
|
|
|
| 2071 |
|
| 2072 |
The *expression* in a *noptr-new-declarator* is erroneous if:
|
| 2073 |
|
| 2074 |
- the expression is of non-class type and its value before converting to
|
| 2075 |
`std::size_t` is less than zero;
|
| 2076 |
- the expression is of class type and its value before application of
|
| 2077 |
the second standard conversion ([[over.ics.user]])[^18] is less than
|
| 2078 |
zero;
|
| 2079 |
- its value is such that the size of the allocated object would exceed
|
| 2080 |
-
the implementation-defined limit (
|
| 2081 |
- the *new-initializer* is a *braced-init-list* and the number of array
|
| 2082 |
elements for which initializers are provided (including the
|
| 2083 |
terminating `'\0'` in a string literal ([[lex.string]])) exceeds the
|
| 2084 |
number of elements to initialize.
|
| 2085 |
|
| 2086 |
-
If the *expression*
|
| 2087 |
-
|
| 2088 |
-
|
| 2089 |
-
|
| 2090 |
-
|
| 2091 |
-
|
| 2092 |
-
|
| 2093 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2094 |
|
| 2095 |
A *new-expression* may obtain storage for the object by calling an
|
| 2096 |
-
|
| 2097 |
*new-expression* terminates by throwing an exception, it may release
|
| 2098 |
storage by calling a deallocation function (
|
| 2099 |
[[basic.stc.dynamic.deallocation]]). If the allocated type is a
|
| 2100 |
non-array type, the allocation function’s name is `operator new` and the
|
| 2101 |
deallocation function’s name is `operator delete`. If the allocated type
|
| 2102 |
is an array type, the allocation function’s name is `operator new[]` and
|
| 2103 |
-
the deallocation function’s name is `operator delete[]`.
|
| 2104 |
-
|
| 2105 |
-
|
| 2106 |
-
[[
|
| 2107 |
-
|
| 2108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2109 |
|
| 2110 |
If the *new-expression* begins with a unary `::` operator, the
|
| 2111 |
allocation function’s name is looked up in the global scope. Otherwise,
|
| 2112 |
if the allocated type is a class type `T` or array thereof, the
|
| 2113 |
allocation function’s name is looked up in the scope of `T`. If this
|
|
@@ -2133,10 +2674,12 @@ true were the allocation not extended:
|
|
| 2133 |
*delete-expression*s, and
|
| 2134 |
- the evaluation of `e2` is sequenced before the evaluation of the
|
| 2135 |
*delete-expression* whose operand is the pointer value produced by
|
| 2136 |
`e1`.
|
| 2137 |
|
|
|
|
|
|
|
| 2138 |
``` cpp
|
| 2139 |
void mergeable(int x) {
|
| 2140 |
// These allocations are safe for merging:
|
| 2141 |
std::unique_ptr<char[]> a{new (std::nothrow) char[8]};
|
| 2142 |
std::unique_ptr<char[]> b{new (std::nothrow) char[8]};
|
|
@@ -2155,110 +2698,140 @@ void mergeable(int x) {
|
|
| 2155 |
throw;
|
| 2156 |
}
|
| 2157 |
}
|
| 2158 |
```
|
| 2159 |
|
|
|
|
|
|
|
| 2160 |
When a *new-expression* calls an allocation function and that allocation
|
| 2161 |
has not been extended, the *new-expression* passes the amount of space
|
| 2162 |
requested to the allocation function as the first argument of type
|
| 2163 |
`std::size_t`. That argument shall be no less than the size of the
|
| 2164 |
object being created; it may be greater than the size of the object
|
| 2165 |
-
being created only if the object is an array. For arrays of `char`
|
| 2166 |
-
`unsigned char`, the difference between the result of
|
| 2167 |
-
*new-expression* and the address returned by the allocation function
|
| 2168 |
shall be an integral multiple of the strictest fundamental alignment
|
| 2169 |
requirement ([[basic.align]]) of any object type whose size is no
|
| 2170 |
-
greater than the size of the array being created.
|
| 2171 |
-
|
| 2172 |
-
|
| 2173 |
-
|
| 2174 |
-
|
| 2175 |
-
|
|
|
|
| 2176 |
|
| 2177 |
When a *new-expression* calls an allocation function and that allocation
|
| 2178 |
has been extended, the size argument to the allocation call shall be no
|
| 2179 |
greater than the sum of the sizes for the omitted calls as specified
|
| 2180 |
above, plus the size for the extended call had it not been extended,
|
| 2181 |
plus any padding necessary to align the allocated objects within the
|
| 2182 |
allocated memory.
|
| 2183 |
|
| 2184 |
The *new-placement* syntax is used to supply additional arguments to an
|
| 2185 |
-
allocation function
|
| 2186 |
-
|
| 2187 |
-
|
| 2188 |
-
|
| 2189 |
-
|
| 2190 |
-
`std::size_t`
|
| 2191 |
-
|
| 2192 |
-
|
| 2193 |
-
|
| 2194 |
-
|
| 2195 |
-
|
| 2196 |
-
|
| 2197 |
-
|
| 2198 |
-
|
| 2199 |
-
|
| 2200 |
-
|
| 2201 |
-
|
| 2202 |
-
|
| 2203 |
-
|
| 2204 |
-
|
| 2205 |
-
|
| 2206 |
-
|
| 2207 |
-
|
| 2208 |
-
|
| 2209 |
-
|
| 2210 |
-
|
| 2211 |
-
|
| 2212 |
-
|
| 2213 |
-
[
|
| 2214 |
-
|
| 2215 |
-
|
| 2216 |
-
|
| 2217 |
-
|
| 2218 |
-
|
| 2219 |
-
|
| 2220 |
-
|
| 2221 |
-
|
| 2222 |
-
|
| 2223 |
-
|
| 2224 |
-
|
| 2225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2226 |
|
| 2227 |
A *new-expression* that creates an object of type `T` initializes that
|
| 2228 |
object as follows:
|
| 2229 |
|
| 2230 |
- If the *new-initializer* is omitted, the object is
|
| 2231 |
-
default-initialized ([[dcl.init]]). If no initialization
|
| 2232 |
-
performed, the object has an indeterminate value.
|
| 2233 |
- Otherwise, the *new-initializer* is interpreted according to the
|
| 2234 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 2235 |
|
| 2236 |
-
The invocation of the allocation function is
|
| 2237 |
-
|
| 2238 |
-
|
| 2239 |
-
|
| 2240 |
-
expressions in the *new-initializer* are evaluated if the allocation
|
| 2241 |
-
function returns the null pointer or exits using an exception.
|
| 2242 |
|
| 2243 |
If the *new-expression* creates an object or an array of objects of
|
| 2244 |
class type, access and ambiguity control are done for the allocation
|
| 2245 |
function, the deallocation function ([[class.free]]), and the
|
| 2246 |
constructor ([[class.ctor]]). If the *new-expression* creates an array
|
| 2247 |
of objects of class type, the destructor is potentially invoked (
|
| 2248 |
[[class.dtor]]).
|
| 2249 |
|
| 2250 |
If any part of the object initialization described above[^19] terminates
|
| 2251 |
-
by throwing an exception
|
| 2252 |
-
|
| 2253 |
-
|
| 2254 |
-
|
| 2255 |
-
|
| 2256 |
-
|
| 2257 |
-
|
|
|
|
| 2258 |
does not allocate memory; otherwise, it is likely to result in a memory
|
| 2259 |
-
leak.
|
| 2260 |
|
| 2261 |
If the *new-expression* begins with a unary `::` operator, the
|
| 2262 |
deallocation function’s name is looked up in the global scope.
|
| 2263 |
Otherwise, if the allocated type is a class type `T` or an array
|
| 2264 |
thereof, the deallocation function’s name is looked up in the scope of
|
|
@@ -2270,17 +2843,19 @@ A declaration of a placement deallocation function matches the
|
|
| 2270 |
declaration of a placement allocation function if it has the same number
|
| 2271 |
of parameters and, after parameter transformations ([[dcl.fct]]), all
|
| 2272 |
parameter types except the first are identical. If the lookup finds a
|
| 2273 |
single matching deallocation function, that function will be called;
|
| 2274 |
otherwise, no deallocation function will be called. If the lookup finds
|
| 2275 |
-
|
| 2276 |
[[basic.stc.dynamic.deallocation]]) and that function, considered as a
|
| 2277 |
placement deallocation function, would have been selected as a match for
|
| 2278 |
the allocation function, the program is ill-formed. For a non-placement
|
| 2279 |
allocation function, the normal deallocation function lookup is used to
|
| 2280 |
find the matching deallocation function ([[expr.delete]])
|
| 2281 |
|
|
|
|
|
|
|
| 2282 |
``` cpp
|
| 2283 |
struct S {
|
| 2284 |
// Placement allocation function:
|
| 2285 |
static void* operator new(std::size_t, std::size_t);
|
| 2286 |
|
|
@@ -2290,10 +2865,12 @@ struct S {
|
|
| 2290 |
|
| 2291 |
S* p = new (0) S; // ill-formed: non-placement deallocation function matches
|
| 2292 |
// placement allocation function
|
| 2293 |
```
|
| 2294 |
|
|
|
|
|
|
|
| 2295 |
If a *new-expression* calls a deallocation function, it passes the value
|
| 2296 |
returned from the allocation function call as the first argument of type
|
| 2297 |
`void*`. If a placement deallocation function is called, it is passed
|
| 2298 |
the same additional arguments as were passed to the placement allocation
|
| 2299 |
function, that is, the same arguments as those specified with the
|
|
@@ -2332,16 +2909,20 @@ pointer to a non-array object created by a previous *new-expression*, or
|
|
| 2332 |
a pointer to a subobject ([[intro.object]]) representing a base class
|
| 2333 |
of such an object (Clause [[class.derived]]). If not, the behavior is
|
| 2334 |
undefined. In the second alternative (*delete array*), the value of the
|
| 2335 |
operand of `delete` may be a null pointer value or a pointer value that
|
| 2336 |
resulted from a previous array *new-expression*.[^22] If not, the
|
| 2337 |
-
behavior is undefined.
|
| 2338 |
-
|
| 2339 |
-
|
| 2340 |
-
|
| 2341 |
-
|
| 2342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2343 |
|
| 2344 |
In the first alternative (*delete object*), if the static type of the
|
| 2345 |
object to be deleted is different from its dynamic type, the static type
|
| 2346 |
shall be a base class of the dynamic type of the object to be deleted
|
| 2347 |
and the static type shall have a virtual destructor or the behavior is
|
|
@@ -2378,43 +2959,70 @@ pointer value, then:
|
|
| 2378 |
*new-expression* that had storage provided by the extended
|
| 2379 |
*new-expression* has been evaluated, the *delete-expression* shall
|
| 2380 |
call a deallocation function. The value returned from the allocation
|
| 2381 |
call of the extended *new-expression* shall be passed as the first
|
| 2382 |
argument to the deallocation function.
|
| 2383 |
-
- Otherwise, the *delete-expression* will not call a
|
| 2384 |
-
function
|
| 2385 |
|
| 2386 |
-
|
| 2387 |
-
|
| 2388 |
-
|
| 2389 |
-
exception.
|
| 2390 |
|
| 2391 |
-
|
| 2392 |
-
|
| 2393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2394 |
[[new.delete.array]]). A C++ program can provide alternative definitions
|
| 2395 |
of these functions ([[replacement.functions]]), and/or class-specific
|
| 2396 |
-
versions ([[class.free]]).
|
| 2397 |
|
| 2398 |
When the keyword `delete` in a *delete-expression* is preceded by the
|
| 2399 |
unary `::` operator, the deallocation function’s name is looked up in
|
| 2400 |
global scope. Otherwise, the lookup considers class-specific
|
| 2401 |
deallocation functions ([[class.free]]). If no class-specific
|
| 2402 |
deallocation function is found, the deallocation function’s name is
|
| 2403 |
looked up in global scope.
|
| 2404 |
|
| 2405 |
-
If
|
| 2406 |
-
|
| 2407 |
-
|
| 2408 |
-
|
| 2409 |
-
|
| 2410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2411 |
|
| 2412 |
When a *delete-expression* is executed, the selected deallocation
|
| 2413 |
-
function shall be called with the address of the
|
| 2414 |
-
|
| 2415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2416 |
|
| 2417 |
Access and ambiguity control are done for both the deallocation function
|
| 2418 |
and the destructor ([[class.dtor]], [[class.free]]).
|
| 2419 |
|
| 2420 |
### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
|
@@ -2441,35 +3049,23 @@ noexcept-expression:
|
|
| 2441 |
```
|
| 2442 |
|
| 2443 |
The result of the `noexcept` operator is a constant of type `bool` and
|
| 2444 |
is a prvalue.
|
| 2445 |
|
| 2446 |
-
The result of the `noexcept` operator is `
|
| 2447 |
-
potentially-
|
| 2448 |
-
|
| 2449 |
-
- a potentially-evaluated call[^24] to a function, member function,
|
| 2450 |
-
function pointer, or member function pointer that does not have a
|
| 2451 |
-
non-throwing *exception-specification* ([[except.spec]]), unless the
|
| 2452 |
-
call is a constant expression ([[expr.const]]),
|
| 2453 |
-
- a potentially-evaluated *throw-expression* ([[except.throw]]),
|
| 2454 |
-
- a potentially-evaluated `dynamic_cast` expression
|
| 2455 |
-
`dynamic_cast<T>(v)`, where `T` is a reference type, that requires a
|
| 2456 |
-
run-time check ([[expr.dynamic.cast]]), or
|
| 2457 |
-
- a potentially-evaluated `typeid` expression ([[expr.typeid]]) applied
|
| 2458 |
-
to a glvalue expression whose type is a polymorphic class type (
|
| 2459 |
-
[[class.virtual]]).
|
| 2460 |
-
|
| 2461 |
-
Otherwise, the result is `true`.
|
| 2462 |
|
| 2463 |
## Explicit type conversion (cast notation) <a id="expr.cast">[[expr.cast]]</a>
|
| 2464 |
|
| 2465 |
The result of the expression `(T)` *cast-expression* is of type `T`. The
|
| 2466 |
result is an lvalue if `T` is an lvalue reference type or an rvalue
|
| 2467 |
reference to function type and an xvalue if `T` is an rvalue reference
|
| 2468 |
-
to object type; otherwise the result is a prvalue.
|
| 2469 |
-
|
| 2470 |
-
|
|
|
|
|
|
|
| 2471 |
|
| 2472 |
An explicit type conversion can be expressed using functional notation (
|
| 2473 |
[[expr.type.conv]]), a type conversion operator (`dynamic_cast`,
|
| 2474 |
`static_cast`, `reinterpret_cast`, `const_cast`), or the *cast*
|
| 2475 |
notation.
|
|
@@ -2511,30 +3107,36 @@ If a conversion can be interpreted in more than one of the ways listed
|
|
| 2511 |
above, the interpretation that appears first in the list is used, even
|
| 2512 |
if a cast resulting from that interpretation is ill-formed. If a
|
| 2513 |
conversion can be interpreted in more than one way as a `static_cast`
|
| 2514 |
followed by a `const_cast`, the conversion is ill-formed.
|
| 2515 |
|
|
|
|
|
|
|
| 2516 |
``` cpp
|
| 2517 |
struct A { };
|
| 2518 |
struct I1 : A { };
|
| 2519 |
struct I2 : A { };
|
| 2520 |
struct D : I1, I2 { };
|
| 2521 |
A* foo( D* p ) {
|
| 2522 |
return (A*)( p ); // ill-formed static_cast interpretation
|
| 2523 |
}
|
| 2524 |
```
|
| 2525 |
|
|
|
|
|
|
|
| 2526 |
The operand of a cast using the cast notation can be a prvalue of type
|
| 2527 |
“pointer to incomplete class type”. The destination type of a cast using
|
| 2528 |
the cast notation can be “pointer to incomplete class type”. If both the
|
| 2529 |
operand and destination types are class types and one or both are
|
| 2530 |
incomplete, it is unspecified whether the `static_cast` or the
|
| 2531 |
`reinterpret_cast` interpretation is used, even if there is an
|
| 2532 |
-
inheritance relationship between the two classes.
|
| 2533 |
-
|
| 2534 |
-
|
| 2535 |
-
|
|
|
|
|
|
|
| 2536 |
|
| 2537 |
## Pointer-to-member operators <a id="expr.mptr.oper">[[expr.mptr.oper]]</a>
|
| 2538 |
|
| 2539 |
The pointer-to-member operators `->*` and `.*` group left-to-right.
|
| 2540 |
|
|
@@ -2544,31 +3146,35 @@ pm-expression:
|
|
| 2544 |
pm-expression '.*' cast-expression
|
| 2545 |
pm-expression '->*' cast-expression
|
| 2546 |
```
|
| 2547 |
|
| 2548 |
The binary operator `.*` binds its second operand, which shall be of
|
| 2549 |
-
type “pointer to member of `T`” to its first operand, which shall be
|
| 2550 |
-
class `T` or of a class of which `T` is an unambiguous and
|
| 2551 |
-
base class. The result is an object or a function of the type
|
| 2552 |
-
by the second operand.
|
| 2553 |
|
| 2554 |
The binary operator `->*` binds its second operand, which shall be of
|
| 2555 |
type “pointer to member of `T`” to its first operand, which shall be of
|
| 2556 |
-
type “pointer to `
|
| 2557 |
-
unambiguous and accessible base class.
|
| 2558 |
converted into the equivalent form `(*(E1)).*E2`.
|
| 2559 |
|
| 2560 |
Abbreviating *pm-expression*`.*`*cast-expression* as `E1.*E2`, `E1` is
|
| 2561 |
called the *object expression*. If the dynamic type of `E1` does not
|
| 2562 |
contain the member to which `E2` refers, the behavior is undefined.
|
|
|
|
| 2563 |
|
| 2564 |
The restrictions on *cv-*qualification, and the manner in which the
|
| 2565 |
*cv-*qualifiers of the operands are combined to produce the
|
| 2566 |
*cv-*qualifiers of the result, are the same as the rules for `E1.E2`
|
| 2567 |
-
given in [[expr.ref]].
|
| 2568 |
-
|
| 2569 |
-
|
|
|
|
|
|
|
|
|
|
| 2570 |
|
| 2571 |
``` cpp
|
| 2572 |
struct S {
|
| 2573 |
S() : i(0) { }
|
| 2574 |
mutable int i;
|
|
@@ -2579,29 +3185,36 @@ const S cs;
|
|
| 2579 |
int S::* pm = &S::i; // pm refers to mutable member S::i
|
| 2580 |
cs.*pm = 88; // ill-formed: cs is a const object
|
| 2581 |
}
|
| 2582 |
```
|
| 2583 |
|
|
|
|
|
|
|
| 2584 |
If the result of `.*` or `->*` is a function, then that result can be
|
| 2585 |
used only as the operand for the function call operator `()`.
|
| 2586 |
|
|
|
|
|
|
|
| 2587 |
``` cpp
|
| 2588 |
(ptr_to_obj->*ptr_to_mfct)(10);
|
| 2589 |
```
|
| 2590 |
|
| 2591 |
calls the member function denoted by `ptr_to_mfct` for the object
|
| 2592 |
-
pointed to by `ptr_to_obj`.
|
| 2593 |
-
|
| 2594 |
-
|
| 2595 |
-
|
| 2596 |
-
|
| 2597 |
-
|
| 2598 |
-
|
| 2599 |
-
|
| 2600 |
-
|
| 2601 |
-
|
| 2602 |
-
the
|
|
|
|
|
|
|
|
|
|
| 2603 |
|
| 2604 |
## Multiplicative operators <a id="expr.mul">[[expr.mul]]</a>
|
| 2605 |
|
| 2606 |
The multiplicative operators `*`, `/`, and `%` group left-to-right.
|
| 2607 |
|
|
@@ -2622,11 +3235,11 @@ The binary `*` operator indicates multiplication.
|
|
| 2622 |
|
| 2623 |
The binary `/` operator yields the quotient, and the binary `%` operator
|
| 2624 |
yields the remainder from the division of the first expression by the
|
| 2625 |
second. If the second operand of `/` or `%` is zero the behavior is
|
| 2626 |
undefined. For integral operands the `/` operator yields the algebraic
|
| 2627 |
-
quotient with any fractional part discarded;[^
|
| 2628 |
is representable in the type of the result, `(a/b)*b + a%b` is equal to
|
| 2629 |
`a`; otherwise, the behavior of both `a/b` and `a%b` is undefined.
|
| 2630 |
|
| 2631 |
## Additive operators <a id="expr.add">[[expr.add]]</a>
|
| 2632 |
|
|
@@ -2656,65 +3269,43 @@ For subtraction, one of the following shall hold:
|
|
| 2656 |
|
| 2657 |
The result of the binary `+` operator is the sum of the operands. The
|
| 2658 |
result of the binary `-` operator is the difference resulting from the
|
| 2659 |
subtraction of the second operand from the first.
|
| 2660 |
|
| 2661 |
-
For the purposes of these operators, a pointer to a nonarray object
|
| 2662 |
-
behaves the same as a pointer to the first element of an array of length
|
| 2663 |
-
one with the type of the object as its element type.
|
| 2664 |
-
|
| 2665 |
When an expression that has integral type is added to or subtracted from
|
| 2666 |
a pointer, the result has the type of the pointer operand. If the
|
| 2667 |
-
|
| 2668 |
-
|
| 2669 |
-
|
| 2670 |
-
|
| 2671 |
-
|
| 2672 |
-
|
| 2673 |
-
`(P)-N` (where `N` has the value n) point to, respectively, the i+n-th
|
| 2674 |
-
and i-n-th elements of the array object, provided they exist. Moreover,
|
| 2675 |
-
if the expression `P` points to the last element of an array object, the
|
| 2676 |
-
expression `(P)+1` points one past the last element of the array object,
|
| 2677 |
-
and if the expression `Q` points one past the last element of an array
|
| 2678 |
-
object, the expression `(Q)-1` points to the last element of the array
|
| 2679 |
-
object. If both the pointer operand and the result point to elements of
|
| 2680 |
-
the same array object, or one past the last element of the array object,
|
| 2681 |
-
the evaluation shall not produce an overflow; otherwise, the behavior is
|
| 2682 |
-
undefined.
|
| 2683 |
|
| 2684 |
When two pointers to elements of the same array object are subtracted,
|
| 2685 |
-
the
|
| 2686 |
-
|
| 2687 |
-
|
| 2688 |
-
`
|
| 2689 |
-
|
| 2690 |
-
|
| 2691 |
-
|
| 2692 |
-
|
| 2693 |
-
|
| 2694 |
-
|
| 2695 |
-
past the last element of an array object, and the expression `Q` points
|
| 2696 |
-
to the last element of the same array object, the expression
|
| 2697 |
-
`((Q)+1)-(P)` has the same value as `((Q)-(P))+1` and as
|
| 2698 |
-
`-((P)-((Q)+1))`, and has the value zero if the expression `P` points
|
| 2699 |
-
one past the last element of the array object, even though the
|
| 2700 |
-
expression `(Q)+1` does not point to an element of the array object.
|
| 2701 |
-
Unless both pointers point to elements of the same array object, or one
|
| 2702 |
-
past the last element of the array object, the behavior is
|
| 2703 |
-
undefined.[^26]
|
| 2704 |
|
| 2705 |
For addition or subtraction, if the expressions `P` or `Q` have type
|
| 2706 |
-
“pointer to cv `T`”, where `T`
|
| 2707 |
-
|
| 2708 |
-
to a base class cannot be used for pointer arithmetic when the array
|
| 2709 |
-
contains objects of a derived class type.
|
| 2710 |
|
| 2711 |
-
|
| 2712 |
-
|
| 2713 |
-
|
| 2714 |
-
|
| 2715 |
-
|
|
|
|
|
|
|
|
|
|
| 2716 |
|
| 2717 |
## Shift operators <a id="expr.shift">[[expr.shift]]</a>
|
| 2718 |
|
| 2719 |
The shift operators `<<` and `>>` group left-to-right.
|
| 2720 |
|
|
@@ -2745,14 +3336,18 @@ The value of `E1 >> E2` is `E1` right-shifted `E2` bit positions. If
|
|
| 2745 |
`E1` has an unsigned type or if `E1` has a signed type and a
|
| 2746 |
non-negative value, the value of the result is the integral part of the
|
| 2747 |
quotient of $\mathrm{E1}/2^\mathrm{E2}$. If `E1` has a signed type and a
|
| 2748 |
negative value, the resulting value is *implementation-defined*.
|
| 2749 |
|
|
|
|
|
|
|
| 2750 |
## Relational operators <a id="expr.rel">[[expr.rel]]</a>
|
| 2751 |
|
| 2752 |
-
The relational operators group left-to-right.
|
| 2753 |
-
|
|
|
|
|
|
|
| 2754 |
|
| 2755 |
``` bnf
|
| 2756 |
relational-expression:
|
| 2757 |
shift-expression
|
| 2758 |
relational-expression '<' shift-expression
|
|
@@ -2771,23 +3366,21 @@ or enumeration type. If both operands are pointers, pointer
|
|
| 2771 |
conversions ([[conv.ptr]]) and qualification conversions (
|
| 2772 |
[[conv.qual]]) are performed to bring them to their composite pointer
|
| 2773 |
type (Clause [[expr]]). After conversions, the operands shall have the
|
| 2774 |
same type.
|
| 2775 |
|
| 2776 |
-
Comparing pointers to objects is defined as follows:
|
| 2777 |
|
| 2778 |
- If two pointers point to different elements of the same array, or to
|
| 2779 |
subobjects thereof, the pointer to the element with the higher
|
| 2780 |
subscript compares greater.
|
| 2781 |
-
- If one pointer points to an element of an array, or to a subobject
|
| 2782 |
-
thereof, and another pointer points one past the last element of the
|
| 2783 |
-
array, the latter pointer compares greater.
|
| 2784 |
- If two pointers point to different non-static data members of the same
|
| 2785 |
object, or to subobjects of such members, recursively, the pointer to
|
| 2786 |
the later declared member compares greater provided the two members
|
| 2787 |
have the same access control (Clause [[class.access]]) and provided
|
| 2788 |
their class is not a union.
|
|
|
|
| 2789 |
|
| 2790 |
If two operands `p` and `q` compare equal ([[expr.eq]]), `p<=q` and
|
| 2791 |
`p>=q` both yield `true` and `p<q` and `p>q` both yield `false`.
|
| 2792 |
Otherwise, if a pointer `p` compares greater than a pointer `q`, `p>=q`,
|
| 2793 |
`p>q`, `q<=p`, and `q<p` all yield `true` and `p<=q`, `p<q`, `q>=p`, and
|
|
@@ -2813,16 +3406,23 @@ or pointer to member type, or type `std::nullptr_t`. The operators `==`
|
|
| 2813 |
and `!=` both yield `true` or `false`, i.e., a result of type `bool`. In
|
| 2814 |
each case below, the operands shall have the same type after the
|
| 2815 |
specified conversions have been applied.
|
| 2816 |
|
| 2817 |
If at least one of the operands is a pointer, pointer conversions (
|
| 2818 |
-
[[conv.ptr]])
|
| 2819 |
-
|
| 2820 |
-
|
| 2821 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2822 |
function, or both represent the same address ([[basic.compound]]),
|
| 2823 |
-
|
|
|
|
| 2824 |
|
| 2825 |
If at least one of the operands is a pointer to member, pointer to
|
| 2826 |
member conversions ([[conv.mem]]) and qualification conversions (
|
| 2827 |
[[conv.qual]]) are performed on both operands to bring them to their
|
| 2828 |
composite pointer type (Clause [[expr]]). Comparing pointers to members
|
|
@@ -2835,24 +3435,30 @@ is defined as follows:
|
|
| 2835 |
- If either is a pointer to a virtual member function, the result is
|
| 2836 |
unspecified.
|
| 2837 |
- If one refers to a member of class `C1` and the other refers to a
|
| 2838 |
member of a different class `C2`, where neither is a base class of the
|
| 2839 |
other, the result is unspecified.
|
|
|
|
| 2840 |
``` cpp
|
| 2841 |
struct A {};
|
| 2842 |
struct B : A { int x; };
|
| 2843 |
struct C : A { int x; };
|
| 2844 |
|
| 2845 |
int A::*bx = (int(A::*))&B::x;
|
| 2846 |
int A::*cx = (int(A::*))&C::x;
|
| 2847 |
|
| 2848 |
bool b1 = (bx == cx); // unspecified
|
| 2849 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2850 |
- Otherwise, two pointers to members compare equal if they would refer
|
| 2851 |
to the same member of the same most derived object ([[intro.object]])
|
| 2852 |
or the same subobject if indirection with a hypothetical object of the
|
| 2853 |
associated class type were performed, otherwise they compare unequal.
|
|
|
|
| 2854 |
``` cpp
|
| 2855 |
struct B {
|
| 2856 |
int f();
|
| 2857 |
};
|
| 2858 |
struct L : B { };
|
|
@@ -2866,10 +3472,12 @@ is defined as follows:
|
|
| 2866 |
int (D::*pdr)() = pr;
|
| 2867 |
bool x = (pdl == pdr); // false
|
| 2868 |
bool y = (pb == pl); // true
|
| 2869 |
```
|
| 2870 |
|
|
|
|
|
|
|
| 2871 |
Two operands of type `std::nullptr_t` or one operand of type
|
| 2872 |
`std::nullptr_t` and the other a null pointer constant compare equal.
|
| 2873 |
|
| 2874 |
If two operands compare equal, the result is `true` for the `==`
|
| 2875 |
operator and `false` for the `!=` operator. If two operands compare
|
|
@@ -2975,60 +3583,67 @@ the second or third expression.
|
|
| 2975 |
|
| 2976 |
If either the second or the third operand has type `void`, one of the
|
| 2977 |
following shall hold:
|
| 2978 |
|
| 2979 |
- The second or the third operand (but not both) is a (possibly
|
| 2980 |
-
parenthesized) *throw-expression* ([[
|
| 2981 |
-
|
|
|
|
| 2982 |
- Both the second and the third operands have type `void`; the result is
|
| 2983 |
-
of type `void` and is a prvalue. This includes the case
|
| 2984 |
-
operands are *throw-expression*s.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2985 |
|
| 2986 |
Otherwise, if the second and third operand have different types and
|
| 2987 |
either has (possibly cv-qualified) class type, or if both are glvalues
|
| 2988 |
of the same value category and the same type except for
|
| 2989 |
-
cv-qualification, an attempt is made to
|
| 2990 |
-
|
| 2991 |
-
|
| 2992 |
-
expression `E2` of type `T2` is defined as follows:
|
| 2993 |
|
| 2994 |
-
|
| 2995 |
-
|
| 2996 |
-
|
| 2997 |
-
|
| 2998 |
-
|
| 2999 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3000 |
subject to the constraint that the reference must bind directly.
|
| 3001 |
-
- If `E2` is a prvalue or if neither of the
|
| 3002 |
-
|
| 3003 |
-
class type:
|
| 3004 |
-
- if `
|
| 3005 |
-
|
| 3006 |
-
|
| 3007 |
-
|
| 3008 |
-
|
| 3009 |
-
|
| 3010 |
-
|
| 3011 |
-
of type `T2` from `E1` and using that temporary as the converted
|
| 3012 |
-
operand.
|
| 3013 |
-
- Otherwise (i.e., if `E1` or `E2` has a nonclass type, or if they
|
| 3014 |
-
both have class types but the underlying classes are not either the
|
| 3015 |
-
same or one a base class of the other): `E1` can be converted to
|
| 3016 |
-
match `E2` if `E1` can be implicitly converted to the type that
|
| 3017 |
-
expression `E2` would have if `E2` were converted to a prvalue (or
|
| 3018 |
-
the type it has, if `E2` is a prvalue).
|
| 3019 |
|
| 3020 |
-
Using this process, it is determined whether
|
| 3021 |
-
|
| 3022 |
-
|
| 3023 |
-
one can be
|
| 3024 |
-
ill-formed. If
|
| 3025 |
-
|
| 3026 |
-
|
|
|
|
| 3027 |
and the converted operand is used in place of the original operand for
|
| 3028 |
the remainder of this section.
|
| 3029 |
|
|
|
|
|
|
|
|
|
|
| 3030 |
If the second and third operands are glvalues of the same value category
|
| 3031 |
and have the same type, the result is of that type and value category
|
| 3032 |
and it is a bit-field if the second or the third operand is a bit-field,
|
| 3033 |
or if both are bit-fields.
|
| 3034 |
|
|
@@ -3045,43 +3660,84 @@ Lvalue-to-rvalue ([[conv.lval]]), array-to-pointer ([[conv.array]]),
|
|
| 3045 |
and function-to-pointer ([[conv.func]]) standard conversions are
|
| 3046 |
performed on the second and third operands. After those conversions, one
|
| 3047 |
of the following shall hold:
|
| 3048 |
|
| 3049 |
- The second and third operands have the same type; the result is of
|
| 3050 |
-
that type
|
| 3051 |
-
|
| 3052 |
-
the second operand or the third operand depending on the value of the
|
| 3053 |
-
first operand.
|
| 3054 |
- The second and third operands have arithmetic or enumeration type; the
|
| 3055 |
usual arithmetic conversions are performed to bring them to a common
|
| 3056 |
type, and the result is of that type.
|
| 3057 |
- One or both of the second and third operands have pointer type;
|
| 3058 |
-
pointer conversions ([[conv.ptr]])
|
| 3059 |
-
[[conv.
|
| 3060 |
-
|
|
|
|
| 3061 |
- One or both of the second and third operands have pointer to member
|
| 3062 |
type; pointer to member conversions ([[conv.mem]]) and qualification
|
| 3063 |
conversions ([[conv.qual]]) are performed to bring them to their
|
| 3064 |
composite pointer type (Clause [[expr]]). The result is of the
|
| 3065 |
composite pointer type.
|
| 3066 |
- Both the second and third operands have type `std::nullptr_t` or one
|
| 3067 |
has that type and the other is a null pointer constant. The result is
|
| 3068 |
of type `std::nullptr_t`.
|
| 3069 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3070 |
## Assignment and compound assignment operators <a id="expr.ass">[[expr.ass]]</a>
|
| 3071 |
|
| 3072 |
The assignment operator (`=`) and the compound assignment operators all
|
| 3073 |
group right-to-left. All require a modifiable lvalue as their left
|
| 3074 |
operand and return an lvalue referring to the left operand. The result
|
| 3075 |
in all cases is a bit-field if the left operand is a bit-field. In all
|
| 3076 |
cases, the assignment is sequenced after the value computation of the
|
| 3077 |
right and left operands, and before the value computation of the
|
| 3078 |
-
assignment expression.
|
| 3079 |
-
|
| 3080 |
-
|
|
|
|
|
|
|
| 3081 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 3082 |
-
single compound assignment operator.
|
| 3083 |
|
| 3084 |
``` bnf
|
| 3085 |
assignment-expression:
|
| 3086 |
conditional-expression
|
| 3087 |
logical-or-expression assignment-operator initializer-clause
|
|
@@ -3102,31 +3758,33 @@ operand.
|
|
| 3102 |
|
| 3103 |
If the left operand is of class type, the class shall be complete.
|
| 3104 |
Assignment to objects of a class is defined by the copy/move assignment
|
| 3105 |
operator ([[class.copy]], [[over.ass]]).
|
| 3106 |
|
| 3107 |
-
For class objects, assignment is not in general the same as
|
| 3108 |
initialization ([[dcl.init]], [[class.ctor]], [[class.init]],
|
| 3109 |
-
[[class.copy]]).
|
| 3110 |
|
| 3111 |
-
When the left operand of an assignment operator
|
| 3112 |
-
|
| 3113 |
-
|
| 3114 |
|
| 3115 |
The behavior of an expression of the form `E1` *op*`=` `E2` is
|
| 3116 |
equivalent to `E1 = E1` *op* `E2` except that `E1` is evaluated only
|
| 3117 |
once. In `+=` and `-=`, `E1` shall either have arithmetic type or be a
|
| 3118 |
pointer to a possibly cv-qualified completely-defined object type. In
|
| 3119 |
all other cases, `E1` shall have arithmetic type.
|
| 3120 |
|
| 3121 |
-
If the value being stored in an object is
|
| 3122 |
-
|
| 3123 |
-
|
| 3124 |
-
|
| 3125 |
-
|
| 3126 |
-
|
| 3127 |
-
|
|
|
|
|
|
|
| 3128 |
|
| 3129 |
A *braced-init-list* may appear on the right-hand side of
|
| 3130 |
|
| 3131 |
- an assignment to a scalar, in which case the initializer list shall
|
| 3132 |
have at most a single element. The meaning of `x = {v}`, where `T` is
|
|
@@ -3135,19 +3793,23 @@ A *braced-init-list* may appear on the right-hand side of
|
|
| 3135 |
- an assignment to an object of class type, in which case the
|
| 3136 |
initializer list is passed as the argument to the assignment operator
|
| 3137 |
function selected by overload resolution ([[over.ass]],
|
| 3138 |
[[over.match]]).
|
| 3139 |
|
|
|
|
|
|
|
| 3140 |
``` cpp
|
| 3141 |
complex<double> z;
|
| 3142 |
z = { 1,2 }; // meaning z.operator=({1,2\)}
|
| 3143 |
z += { 1, 2 }; // meaning z.operator+=({1,2\)}
|
| 3144 |
int a, b;
|
| 3145 |
a = b = { 1 }; // meaning a=b=1;
|
| 3146 |
a = { 1 } = b; // syntax error
|
| 3147 |
```
|
| 3148 |
|
|
|
|
|
|
|
| 3149 |
## Comma operator <a id="expr.comma">[[expr.comma]]</a>
|
| 3150 |
|
| 3151 |
The comma operator groups left-to-right.
|
| 3152 |
|
| 3153 |
``` bnf
|
|
@@ -3155,90 +3817,140 @@ expression:
|
|
| 3155 |
assignment-expression
|
| 3156 |
expression ',' assignment-expression
|
| 3157 |
```
|
| 3158 |
|
| 3159 |
A pair of expressions separated by a comma is evaluated left-to-right;
|
| 3160 |
-
the left expression is a discarded-value expression (Clause
|
| 3161 |
-
|
| 3162 |
-
|
| 3163 |
-
|
| 3164 |
-
|
| 3165 |
-
|
| 3166 |
-
|
| 3167 |
-
|
| 3168 |
-
|
| 3169 |
-
|
| 3170 |
-
|
| 3171 |
-
arguments to functions ([[expr.call]]) and
|
| 3172 |
-
[[dcl.init]])
|
| 3173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3174 |
|
| 3175 |
``` cpp
|
| 3176 |
f(a, (t=3, t+2), c);
|
| 3177 |
```
|
| 3178 |
|
| 3179 |
has three arguments, the second of which has the value `5`.
|
| 3180 |
|
|
|
|
|
|
|
| 3181 |
## Constant expressions <a id="expr.const">[[expr.const]]</a>
|
| 3182 |
|
| 3183 |
Certain contexts require expressions that satisfy additional
|
| 3184 |
-
requirements as detailed in this
|
| 3185 |
different semantics depending on whether or not an expression satisfies
|
| 3186 |
-
these requirements. Expressions that satisfy these requirements
|
| 3187 |
-
|
| 3188 |
-
|
|
|
|
|
|
|
|
|
|
| 3189 |
|
| 3190 |
``` bnf
|
| 3191 |
constant-expression:
|
| 3192 |
conditional-expression
|
| 3193 |
```
|
| 3194 |
|
| 3195 |
-
|
| 3196 |
-
|
| 3197 |
[[intro.execution]]), would evaluate one of the following expressions:
|
| 3198 |
|
| 3199 |
-
- `this` ([[expr.prim.
|
| 3200 |
-
|
| 3201 |
-
- an invocation of a function other than a
|
| 3202 |
-
literal class, a
|
| 3203 |
-
trivial destructor ([[class.dtor]]) Overload resolution (
|
| 3204 |
-
[[over.match]]) is applied as usual ;
|
| 3205 |
-
- an invocation of an undefined
|
| 3206 |
-
|
|
|
|
|
|
|
|
|
|
| 3207 |
- an expression that would exceed the implementation-defined limits (see
|
| 3208 |
Annex [[implimits]]);
|
| 3209 |
-
- an operation that would have undefined behavior
|
| 3210 |
-
|
| 3211 |
-
|
| 3212 |
-
certain
|
| 3213 |
-
|
|
|
|
| 3214 |
- an lvalue-to-rvalue conversion ([[conv.lval]]) unless it is applied
|
| 3215 |
to
|
| 3216 |
- a non-volatile glvalue of integral or enumeration type that refers
|
| 3217 |
-
to a non-volatile const object with a preceding
|
| 3218 |
-
initialized with a constant expression
|
| 3219 |
-
|
|
|
|
| 3220 |
- a non-volatile glvalue that refers to a non-volatile object defined
|
| 3221 |
-
with `constexpr`, or that refers to a non-mutable
|
| 3222 |
an object, or
|
| 3223 |
- a non-volatile glvalue of literal type that refers to a non-volatile
|
| 3224 |
object whose lifetime began within the evaluation of `e`;
|
| 3225 |
-
- an lvalue-to-rvalue conversion ([[conv.lval]])
|
| 3226 |
-
|
| 3227 |
-
|
| 3228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3229 |
- an *id-expression* that refers to a variable or data member of
|
| 3230 |
reference type unless the reference has a preceding initialization and
|
| 3231 |
either
|
| 3232 |
- it is initialized with a constant expression or
|
| 3233 |
-
-
|
| 3234 |
-
within the evaluation of `e`;
|
| 3235 |
- in a *lambda-expression*, a reference to `this` or to a variable with
|
| 3236 |
automatic storage duration defined outside that *lambda-expression*,
|
| 3237 |
where the reference would be an odr-use ([[basic.def.odr]],
|
| 3238 |
[[expr.prim.lambda]]);
|
| 3239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3240 |
- a dynamic cast ([[expr.dynamic.cast]]);
|
| 3241 |
- a `reinterpret_cast` ([[expr.reinterpret.cast]]);
|
| 3242 |
- a pseudo-destructor call ([[expr.pseudo]]);
|
| 3243 |
- modification of an object ([[expr.ass]], [[expr.post.incr]],
|
| 3244 |
[[expr.pre.incr]]) unless it is applied to a non-volatile lvalue of
|
|
@@ -3248,27 +3960,33 @@ the evaluation of `e`, following the rules of the abstract machine (
|
|
| 3248 |
polymorphic class type;
|
| 3249 |
- a *new-expression* ([[expr.new]]);
|
| 3250 |
- a *delete-expression* ([[expr.delete]]);
|
| 3251 |
- a relational ([[expr.rel]]) or equality ([[expr.eq]]) operator where
|
| 3252 |
the result is unspecified; or
|
| 3253 |
-
- a *throw-expression* ([[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3254 |
|
| 3255 |
``` cpp
|
| 3256 |
int x; // not constant
|
| 3257 |
struct A {
|
| 3258 |
constexpr A(bool b) : m(b?42:x) { }
|
| 3259 |
int m;
|
| 3260 |
};
|
| 3261 |
-
constexpr int v = A(true).m; // OK: constructor call initializes
|
| 3262 |
-
|
| 3263 |
-
constexpr int w = A(false).m; // error: initializer for m is
|
| 3264 |
-
// x, which is non-constant
|
| 3265 |
|
| 3266 |
constexpr int f1(int k) {
|
| 3267 |
-
constexpr int x = k; // error: x is not initialized by a
|
| 3268 |
-
//
|
| 3269 |
-
// began outside the initializer of x
|
| 3270 |
return x;
|
| 3271 |
}
|
| 3272 |
constexpr int f2(int k) {
|
| 3273 |
int x = k; // OK: not required to be a constant expression
|
| 3274 |
// because x is not constexpr
|
|
@@ -3277,77 +3995,116 @@ constexpr int f2(int k) {
|
|
| 3277 |
|
| 3278 |
constexpr int incr(int &n) {
|
| 3279 |
return ++n;
|
| 3280 |
}
|
| 3281 |
constexpr int g(int k) {
|
| 3282 |
-
constexpr int x = incr(k); // error: incr(k) is not a core constant
|
| 3283 |
-
//
|
| 3284 |
-
// began outside the expression incr(k)
|
| 3285 |
return x;
|
| 3286 |
}
|
| 3287 |
constexpr int h(int k) {
|
| 3288 |
-
int x = incr(k); // OK: incr(k) is not required to be a core
|
| 3289 |
-
// constant expression
|
| 3290 |
return x;
|
| 3291 |
}
|
| 3292 |
constexpr int y = h(1); // OK: initializes y with the value 2
|
| 3293 |
// h(1) is a core constant expression because
|
| 3294 |
// the lifetime of k begins inside h(1)
|
| 3295 |
```
|
| 3296 |
|
|
|
|
|
|
|
| 3297 |
An *integral constant expression* is an expression of integral or
|
| 3298 |
unscoped enumeration type, implicitly converted to a prvalue, where the
|
| 3299 |
-
converted expression is a core constant expression.
|
| 3300 |
-
|
| 3301 |
-
|
| 3302 |
-
|
| 3303 |
-
|
| 3304 |
-
|
| 3305 |
-
|
| 3306 |
-
|
| 3307 |
-
|
| 3308 |
-
|
| 3309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3310 |
[[expr.new]]), as case expressions ([[stmt.switch]]), as enumerator
|
| 3311 |
initializers if the underlying type is fixed ([[dcl.enum]]), as array
|
| 3312 |
-
bounds ([[dcl.array]]), and as
|
| 3313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3314 |
|
| 3315 |
A *constant expression* is either a glvalue core constant expression
|
| 3316 |
-
|
| 3317 |
-
|
| 3318 |
-
|
| 3319 |
|
| 3320 |
-
-
|
| 3321 |
-
|
| 3322 |
-
|
| 3323 |
-
|
| 3324 |
-
|
| 3325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3326 |
|
| 3327 |
Since this International Standard imposes no restrictions on the
|
| 3328 |
accuracy of floating-point operations, it is unspecified whether the
|
| 3329 |
evaluation of a floating-point expression during translation yields the
|
| 3330 |
same result as the evaluation of the same expression (or the same
|
| 3331 |
operations on the same values) during program execution.[^28]
|
| 3332 |
|
|
|
|
|
|
|
| 3333 |
``` cpp
|
| 3334 |
bool f() {
|
| 3335 |
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
|
| 3336 |
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
|
| 3337 |
return sizeof(array) == size;
|
| 3338 |
}
|
| 3339 |
```
|
| 3340 |
|
| 3341 |
It is unspecified whether the value of `f()` will be `true` or `false`.
|
| 3342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3343 |
If an expression of literal class type is used in a context where an
|
| 3344 |
integral constant expression is required, then that expression is
|
| 3345 |
contextually implicitly converted (Clause [[conv]]) to an integral or
|
| 3346 |
unscoped enumeration type and the selected conversion function shall be
|
| 3347 |
`constexpr`.
|
| 3348 |
|
|
|
|
|
|
|
| 3349 |
``` cpp
|
| 3350 |
struct A {
|
| 3351 |
constexpr A(int i) : val(i) { }
|
| 3352 |
constexpr operator int() const { return val; }
|
| 3353 |
constexpr operator long() const { return 43; }
|
|
@@ -3358,18 +4115,21 @@ template<int> struct X { };
|
|
| 3358 |
constexpr A a = 42;
|
| 3359 |
X<a> x; // OK: unique conversion to int
|
| 3360 |
int ary[a]; // error: ambiguous conversion
|
| 3361 |
```
|
| 3362 |
|
|
|
|
|
|
|
| 3363 |
<!-- Link reference definitions -->
|
| 3364 |
[bad.alloc]: language.md#bad.alloc
|
| 3365 |
[bad.cast]: language.md#bad.cast
|
| 3366 |
[bad.typeid]: language.md#bad.typeid
|
| 3367 |
[basic.align]: basic.md#basic.align
|
| 3368 |
[basic.compound]: basic.md#basic.compound
|
| 3369 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 3370 |
[basic.fundamental]: basic.md#basic.fundamental
|
|
|
|
| 3371 |
[basic.lookup]: basic.md#basic.lookup
|
| 3372 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 3373 |
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 3374 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 3375 |
[basic.lval]: basic.md#basic.lval
|
|
@@ -3395,40 +4155,46 @@ int ary[a]; // error: ambiguous conversion
|
|
| 3395 |
[class.copy]: special.md#class.copy
|
| 3396 |
[class.ctor]: special.md#class.ctor
|
| 3397 |
[class.derived]: class.md#class.derived
|
| 3398 |
[class.dtor]: special.md#class.dtor
|
| 3399 |
[class.free]: special.md#class.free
|
|
|
|
| 3400 |
[class.init]: special.md#class.init
|
| 3401 |
[class.mem]: class.md#class.mem
|
| 3402 |
[class.member.lookup]: class.md#class.member.lookup
|
| 3403 |
[class.mfct]: class.md#class.mfct
|
| 3404 |
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 3405 |
[class.name]: class.md#class.name
|
| 3406 |
[class.qual]: basic.md#class.qual
|
| 3407 |
[class.static]: class.md#class.static
|
| 3408 |
[class.temporary]: special.md#class.temporary
|
| 3409 |
[class.this]: class.md#class.this
|
|
|
|
| 3410 |
[class.virtual]: class.md#class.virtual
|
| 3411 |
[conv]: conv.md#conv
|
| 3412 |
[conv.array]: conv.md#conv.array
|
| 3413 |
[conv.bool]: conv.md#conv.bool
|
|
|
|
| 3414 |
[conv.fpint]: conv.md#conv.fpint
|
| 3415 |
[conv.fpprom]: conv.md#conv.fpprom
|
| 3416 |
[conv.func]: conv.md#conv.func
|
| 3417 |
[conv.integral]: conv.md#conv.integral
|
| 3418 |
[conv.lval]: conv.md#conv.lval
|
| 3419 |
[conv.mem]: conv.md#conv.mem
|
| 3420 |
[conv.prom]: conv.md#conv.prom
|
| 3421 |
[conv.ptr]: conv.md#conv.ptr
|
| 3422 |
[conv.qual]: conv.md#conv.qual
|
|
|
|
|
|
|
|
|
|
| 3423 |
[dcl.align]: dcl.md#dcl.align
|
| 3424 |
[dcl.array]: dcl.md#dcl.array
|
|
|
|
| 3425 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 3426 |
[dcl.enum]: dcl.md#dcl.enum
|
| 3427 |
[dcl.fct]: dcl.md#dcl.fct
|
| 3428 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 3429 |
-
[dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
|
| 3430 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 3431 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 3432 |
[dcl.init]: dcl.md#dcl.init
|
| 3433 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 3434 |
[dcl.init.list]: dcl.md#dcl.init.list
|
|
@@ -3436,17 +4202,18 @@ int ary[a]; // error: ambiguous conversion
|
|
| 3436 |
[dcl.link]: dcl.md#dcl.link
|
| 3437 |
[dcl.name]: dcl.md#dcl.name
|
| 3438 |
[dcl.ref]: dcl.md#dcl.ref
|
| 3439 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 3440 |
[dcl.stc]: dcl.md#dcl.stc
|
|
|
|
| 3441 |
[dcl.type]: dcl.md#dcl.type
|
| 3442 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 3443 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 3444 |
-
[depr]: future.md#depr
|
| 3445 |
[except]: except.md#except
|
| 3446 |
[except.handle]: except.md#except.handle
|
| 3447 |
[except.spec]: except.md#except.spec
|
|
|
|
| 3448 |
[except.throw]: except.md#except.throw
|
| 3449 |
[expr]: #expr
|
| 3450 |
[expr.add]: #expr.add
|
| 3451 |
[expr.alignof]: #expr.alignof
|
| 3452 |
[expr.ass]: #expr.ass
|
|
@@ -3468,56 +4235,76 @@ int ary[a]; // error: ambiguous conversion
|
|
| 3468 |
[expr.or]: #expr.or
|
| 3469 |
[expr.post]: #expr.post
|
| 3470 |
[expr.post.incr]: #expr.post.incr
|
| 3471 |
[expr.pre.incr]: #expr.pre.incr
|
| 3472 |
[expr.prim]: #expr.prim
|
| 3473 |
-
[expr.prim.
|
|
|
|
|
|
|
|
|
|
| 3474 |
[expr.prim.lambda]: #expr.prim.lambda
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3475 |
[expr.pseudo]: #expr.pseudo
|
| 3476 |
[expr.ref]: #expr.ref
|
| 3477 |
[expr.reinterpret.cast]: #expr.reinterpret.cast
|
| 3478 |
[expr.rel]: #expr.rel
|
| 3479 |
[expr.shift]: #expr.shift
|
| 3480 |
[expr.sizeof]: #expr.sizeof
|
| 3481 |
[expr.static.cast]: #expr.static.cast
|
| 3482 |
[expr.sub]: #expr.sub
|
|
|
|
| 3483 |
[expr.type.conv]: #expr.type.conv
|
| 3484 |
[expr.typeid]: #expr.typeid
|
| 3485 |
[expr.unary]: #expr.unary
|
| 3486 |
[expr.unary.noexcept]: #expr.unary.noexcept
|
| 3487 |
[expr.unary.op]: #expr.unary.op
|
| 3488 |
[expr.xor]: #expr.xor
|
| 3489 |
[function.objects]: utilities.md#function.objects
|
| 3490 |
[implimits]: limits.md#implimits
|
|
|
|
| 3491 |
[intro.execution]: intro.md#intro.execution
|
| 3492 |
[intro.memory]: intro.md#intro.memory
|
| 3493 |
[intro.object]: intro.md#intro.object
|
| 3494 |
[lex.literal]: lex.md#lex.literal
|
| 3495 |
[lex.string]: lex.md#lex.string
|
|
|
|
| 3496 |
[namespace.qual]: basic.md#namespace.qual
|
| 3497 |
[new.badlength]: language.md#new.badlength
|
| 3498 |
[new.delete.array]: language.md#new.delete.array
|
|
|
|
| 3499 |
[new.delete.single]: language.md#new.delete.single
|
| 3500 |
[over]: over.md#over
|
| 3501 |
[over.ass]: over.md#over.ass
|
|
|
|
| 3502 |
[over.built]: over.md#over.built
|
| 3503 |
[over.call]: over.md#over.call
|
| 3504 |
[over.ics.user]: over.md#over.ics.user
|
| 3505 |
[over.literal]: over.md#over.literal
|
| 3506 |
[over.match]: over.md#over.match
|
|
|
|
| 3507 |
[over.match.oper]: over.md#over.match.oper
|
|
|
|
| 3508 |
[over.oper]: over.md#over.oper
|
| 3509 |
[over.over]: over.md#over.over
|
| 3510 |
[replacement.functions]: library.md#replacement.functions
|
|
|
|
| 3511 |
[stmt.switch]: stmt.md#stmt.switch
|
| 3512 |
[support.runtime]: language.md#support.runtime
|
| 3513 |
[support.types]: language.md#support.types
|
|
|
|
| 3514 |
[temp.arg]: temp.md#temp.arg
|
|
|
|
|
|
|
| 3515 |
[temp.mem]: temp.md#temp.mem
|
| 3516 |
[temp.names]: temp.md#temp.names
|
| 3517 |
[temp.res]: temp.md#temp.res
|
| 3518 |
[temp.variadic]: temp.md#temp.variadic
|
|
|
|
| 3519 |
[type.info]: language.md#type.info
|
| 3520 |
|
| 3521 |
[^1]: The precedence of operators is not directly specified, but it can
|
| 3522 |
be derived from the syntax.
|
| 3523 |
|
|
@@ -3588,41 +4375,34 @@ int ary[a]; // error: ambiguous conversion
|
|
| 3588 |
expression is enclosed in parentheses.
|
| 3589 |
|
| 3590 |
[^21]: This implies that an object cannot be deleted using a pointer of
|
| 3591 |
type `void*` because `void` is not an object type.
|
| 3592 |
|
| 3593 |
-
[^22]: For
|
| 3594 |
first element of the array created by that *new-expression*.
|
| 3595 |
Zero-length arrays do not have a first element.
|
| 3596 |
|
| 3597 |
[^23]: If the static type of the object to be deleted is complete and is
|
| 3598 |
different from the dynamic type, and the destructor is not virtual,
|
| 3599 |
the size might be incorrect, but that case is already undefined, as
|
| 3600 |
stated above.
|
| 3601 |
|
| 3602 |
-
[^24]: This
|
| 3603 |
-
function in a *new-expression*.
|
| 3604 |
|
| 3605 |
-
[^25]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3606 |
|
| 3607 |
-
[^26]:
|
| 3608 |
-
|
| 3609 |
-
|
| 3610 |
-
|
| 3611 |
-
|
| 3612 |
-
original type. For pointer subtraction, the result of the difference
|
| 3613 |
-
between the character pointers is similarly divided by the size of
|
| 3614 |
-
the object originally pointed to.
|
| 3615 |
|
| 3616 |
-
|
| 3617 |
-
|
| 3618 |
-
after the end of the object in order to satisfy the “one past the
|
| 3619 |
-
last element” requirements.
|
| 3620 |
-
|
| 3621 |
-
[^27]: However, an invocation of an overloaded comma operator is an
|
| 3622 |
-
ordinary function call; hence, the evaluations of its argument
|
| 3623 |
-
expressions are unsequenced relative to one another (see
|
| 3624 |
-
[[intro.execution]]).
|
| 3625 |
|
| 3626 |
[^28]: Nonetheless, implementations are encouraged to provide consistent
|
| 3627 |
results, irrespective of whether the evaluation was performed during
|
| 3628 |
translation and/or during program execution.
|
|
|
|
| 1 |
# Expressions <a id="expr">[[expr]]</a>
|
| 2 |
|
| 3 |
+
[*Note 1*: Clause [[expr]] defines the syntax, order of evaluation,
|
| 4 |
+
and meaning of expressions.[^1] An expression is a sequence of operators
|
| 5 |
+
and operands that specifies a computation. An expression can result in a
|
| 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 (Clause [[class]]) or enumeration
|
| 10 |
+
type ([[dcl.enum]]). Uses of overloaded operators are transformed into
|
| 11 |
function calls as described in [[over.oper]]. Overloaded operators obey
|
| 12 |
+
the rules for syntax and evaluation order specified in Clause [[expr]],
|
| 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]]). — *end note*]
|
| 17 |
|
| 18 |
Clause [[expr]] defines the effects of operators when applied to types
|
| 19 |
for which they have not been overloaded. Operator overloading shall not
|
| 20 |
modify the rules for the *built-in operators*, that is, for operators
|
| 21 |
applied to types for which they are defined by this Standard. However,
|
|
|
|
| 27 |
according to the rules in Clause [[expr]]; see [[over.match.oper]],
|
| 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 vary among machines, and
|
| 36 |
+
is sometimes adjustable by a library function. — *end note*]
|
| 37 |
|
| 38 |
If an expression initially has the type “reference to `T`” (
|
| 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 |
+
[*Note 4*: Before the lifetime of the reference has started or after it
|
| 45 |
+
has ended, the behavior is undefined (see
|
| 46 |
+
[[basic.life]]). — *end note*]
|
| 47 |
+
|
| 48 |
+
If a prvalue initially has the type “cv `T`”, where `T` is a
|
| 49 |
cv-unqualified non-class, non-array type, the type of the expression is
|
| 50 |
adjusted to `T` prior to any further analysis.
|
| 51 |
|
| 52 |
+
[*Note 5*:
|
| 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,
|
|
|
|
| 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.
|
| 68 |
|
| 69 |
+
— *end note*]
|
| 70 |
+
|
| 71 |
+
[*Example 1*:
|
| 72 |
+
|
| 73 |
``` cpp
|
| 74 |
struct A {
|
| 75 |
int m;
|
| 76 |
};
|
| 77 |
A&& operator+(A, A);
|
|
|
|
| 82 |
```
|
| 83 |
|
| 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 |
In some contexts, *unevaluated operands* appear ([[expr.typeid]],
|
| 90 |
[[expr.sizeof]], [[expr.unary.noexcept]], [[dcl.type.simple]]). An
|
| 91 |
+
unevaluated operand is not evaluated.
|
| 92 |
+
|
| 93 |
+
[*Note 6*: In an unevaluated operand, a non-static class member may be
|
| 94 |
+
named ([[expr.prim]]) and naming of objects or functions does not, by
|
| 95 |
+
itself, require that a definition be provided ([[basic.def.odr]]). An
|
| 96 |
+
unevaluated operand is considered a full-expression (
|
| 97 |
+
[[intro.execution]]). — *end note*]
|
| 98 |
|
| 99 |
Whenever a glvalue expression appears as an operand of an operator that
|
| 100 |
expects a prvalue for that operand, the lvalue-to-rvalue (
|
| 101 |
[[conv.lval]]), array-to-pointer ([[conv.array]]), or
|
| 102 |
function-to-pointer ([[conv.func]]) standard conversions are applied to
|
| 103 |
+
convert the expression to a prvalue.
|
| 104 |
+
|
| 105 |
+
[*Note 7*: Because cv-qualifiers are removed from the type of an
|
| 106 |
+
expression of non-class type when the expression is converted to a
|
| 107 |
+
prvalue, an lvalue expression of type `const int` can, for example, be
|
| 108 |
+
used where a prvalue expression of type `int` is
|
| 109 |
+
required. — *end note*]
|
| 110 |
+
|
| 111 |
+
Whenever a prvalue expression appears as an operand of an operator that
|
| 112 |
+
expects a glvalue for that operand, the temporary materialization
|
| 113 |
+
conversion ([[conv.rval]]) is applied to convert the expression to an
|
| 114 |
+
xvalue.
|
| 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 ([[dcl.enum]]), no
|
| 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 ([[conv.prom]]) shall be performed
|
|
|
|
| 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 |
In some contexts, an expression only appears for its side effects. Such
|
| 153 |
+
an expression is called a *discarded-value expression*. The
|
| 154 |
+
array-to-pointer ([[conv.array]]) and function-to-pointer (
|
| 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;
|
|
|
|
| 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 |
'this'
|
| 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. Its type depends on its form (
|
| 254 |
[[lex.literal]]). A string literal is an lvalue; all other literals are
|
| 255 |
prvalues.
|
| 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 ([[class.this]]) is invoked or a non-static data
|
| 261 |
member’s initializer ([[class.mem]]) is evaluated.
|
| 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*.
|
| 267 |
It shall not appear before the optional *cv-qualifier-seq* and it shall
|
| 268 |
not appear within the declaration of a static member function (although
|
| 269 |
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 |
+
Unlike the object expression in other contexts, `*this` is not required
|
| 276 |
+
to be of complete type for purposes of class member access (
|
| 277 |
+
[[expr.ref]]) outside the member function body.
|
| 278 |
+
|
| 279 |
+
[*Note 2*: Only class members declared prior to the declaration are
|
| 280 |
+
visible. — *end note*]
|
| 281 |
+
|
| 282 |
+
[*Example 1*:
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
struct A {
|
| 286 |
char g();
|
| 287 |
template<class T> auto f(T t) -> decltype(t + g())
|
| 288 |
{ return t + g(); }
|
| 289 |
};
|
| 290 |
template auto A::f(int t) -> decltype(t + g());
|
| 291 |
```
|
| 292 |
|
| 293 |
+
— *end example*]
|
| 294 |
+
|
| 295 |
Otherwise, if a *member-declarator* declares a non-static data member (
|
| 296 |
[[class.mem]]) of a class `X`, the expression `this` is a prvalue of
|
| 297 |
+
type “pointer to `X`” within the optional default member initializer (
|
| 298 |
+
[[class.mem]]). It shall not appear elsewhere in the
|
| 299 |
+
*member-declarator*.
|
| 300 |
|
| 301 |
The expression `this` shall not appear in any other context.
|
| 302 |
|
| 303 |
+
[*Example 2*:
|
| 304 |
+
|
| 305 |
``` cpp
|
| 306 |
class Outer {
|
| 307 |
int a[sizeof(*this)]; // error: not inside a member function
|
| 308 |
+
unsigned int sz = sizeof(*this); // OK: in default member initializer
|
| 309 |
|
| 310 |
void f() {
|
| 311 |
int b[sizeof(*this)]; // OK
|
| 312 |
|
| 313 |
struct Inner {
|
|
|
|
| 315 |
};
|
| 316 |
}
|
| 317 |
};
|
| 318 |
```
|
| 319 |
|
| 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 `E`. The
|
| 326 |
parenthesized expression can be used in exactly the same contexts as
|
| 327 |
+
those where `E` can be used, and with the same meaning, except as
|
| 328 |
+
otherwise indicated.
|
| 329 |
|
| 330 |
+
### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
|
| 331 |
+
|
| 332 |
+
``` bnf
|
| 333 |
+
id-expression:
|
| 334 |
+
unqualified-id
|
| 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]]). — *end note*]
|
| 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 ([[expr.ref]]) in which the object
|
| 347 |
+
expression refers to the member’s class[^4] or a class derived from
|
| 348 |
+
that class, or
|
| 349 |
+
- to form a pointer to member ([[expr.unary.op]]), or
|
| 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 {
|
| 355 |
+
int m;
|
| 356 |
+
};
|
| 357 |
+
int i = sizeof(S::m); // OK
|
| 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 |
+
'~' class-name
|
| 372 |
+
'~' decltype-specifier
|
| 373 |
+
template-id
|
| 374 |
+
```
|
| 375 |
|
| 376 |
An *identifier* is an *id-expression* provided it has been suitably
|
| 377 |
+
declared (Clause [[dcl.dcl]]).
|
| 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 *class-name* or *decltype-specifier* prefixed by `~`
|
| 383 |
+
denotes a destructor; see [[class.dtor]]. Within the definition of a
|
| 384 |
+
non-static member function, an *identifier* that names a non-static
|
| 385 |
member is transformed to a class member access expression (
|
| 386 |
+
[[class.mfct.non-static]]). — *end note*]
|
| 387 |
+
|
| 388 |
+
The type of the expression is the type of the *identifier*. The result
|
| 389 |
+
is the entity denoted by the identifier. The expression is an lvalue if
|
| 390 |
+
the entity is a function, variable, or data member and a prvalue
|
| 391 |
+
otherwise; it is a bit-field if the identifier designates a bit-field (
|
| 392 |
+
[[dcl.struct.bind]]).
|
| 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 'template'ₒₚₜ unqualified-id
|
| 399 |
```
|
|
|
|
| 414 |
A *nested-name-specifier* that denotes a class, optionally followed by
|
| 415 |
the keyword `template` ([[temp.names]]), and then followed by the name
|
| 416 |
of a member of either that class ([[class.mem]]) or one of its base
|
| 417 |
classes (Clause [[class.derived]]), is a *qualified-id*;
|
| 418 |
[[class.qual]] describes name lookup for class members that appear in
|
| 419 |
+
*qualified-id*s. The result is the member. The type of the result is the
|
| 420 |
type of the member. The result is an lvalue if the member is a static
|
| 421 |
+
member function or a data member and a prvalue otherwise.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 *class-name* `::~` *class-name* is used, the two *class-name*s
|
| 428 |
+
shall refer to the same class; this notation names the destructor (
|
| 429 |
+
[[class.dtor]]). The form `~` *decltype-specifier* also denotes the
|
| 430 |
+
destructor, but it shall not be used as the *unqualified-id* in a
|
| 431 |
+
*qualified-id*.
|
| 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 ([[basic.namespace]]),
|
| 438 |
+
optionally followed by the keyword `template` ([[temp.names]]), and
|
| 439 |
+
then followed by the name of a member of that namespace (or the name of
|
| 440 |
+
a member of a namespace made visible by a *using-directive*), is a
|
| 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 or a variable and a prvalue otherwise.
|
| 445 |
|
| 446 |
A *nested-name-specifier* that denotes an enumeration ([[dcl.enum]]),
|
| 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
|
|
|
|
| 452 |
In a *qualified-id*, if the *unqualified-id* is a
|
| 453 |
*conversion-function-id*, its *conversion-type-id* shall denote the same
|
| 454 |
type in both the context in which the entire *qualified-id* occurs and
|
| 455 |
in the context of the class denoted by the *nested-name-specifier*.
|
| 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 |
+
Lambda expressions provide a concise way to create simple function
|
| 476 |
+
objects.
|
| 477 |
+
|
| 478 |
+
[*Example 1*:
|
| 479 |
+
|
| 480 |
+
``` cpp
|
| 481 |
+
#include <algorithm>
|
| 482 |
+
#include <cmath>
|
| 483 |
+
void abssort(float* x, unsigned N) {
|
| 484 |
+
std::sort(x, x + N, [](float a, float b) { return std::abs(a) < std::abs(b); });
|
| 485 |
+
}
|
| 486 |
+
```
|
| 487 |
+
|
| 488 |
+
— *end example*]
|
| 489 |
+
|
| 490 |
+
A *lambda-expression* is a prvalue whose result object is called the
|
| 491 |
+
*closure object*. A *lambda-expression* shall not appear in an
|
| 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*: The intention is to prevent lambdas from appearing in a
|
| 498 |
+
signature. — *end note*]
|
| 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 either be `mutable` or `constexpr`.
|
| 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`
|
| 510 |
+
statements as described in [[dcl.spec.auto]].
|
| 511 |
+
|
| 512 |
+
[*Example 2*:
|
| 513 |
+
|
| 514 |
+
``` cpp
|
| 515 |
+
auto x1 = [](int i){ return i; }; // OK: return type is int
|
| 516 |
+
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 517 |
+
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 ([[basic.lookup.argdep]]). The parameter types of
|
| 534 |
+
a *lambda-declarator* do not affect these associated namespaces and
|
| 535 |
+
classes. — *end note*]
|
| 536 |
+
|
| 537 |
+
The closure type is not an aggregate type ([[dcl.init.aggr]]). An
|
| 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 (Clause [[class]]),
|
| 544 |
- whether the closure type is a standard-layout class (Clause
|
| 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 non-generic *lambda-expression* has a public
|
| 552 |
inline function call operator ([[over.call]]) whose parameters and
|
| 553 |
return type are described by the *lambda-expression*’s
|
| 554 |
*parameter-declaration-clause* and *trailing-return-type* respectively.
|
| 555 |
For a generic lambda, the closure type has a public inline function call
|
|
|
|
| 563 |
*lambda-expression*'s *trailing-return-type* and
|
| 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; };
|
| 572 |
bool b = glambda(3, 3.14); // OK
|
| 573 |
+
|
| 574 |
auto vglambda = [](auto printer) {
|
| 575 |
return [=](auto&& ... ts) { // OK: ts is a function parameter pack
|
| 576 |
printer(std::forward<decltype(ts)>(ts)...);
|
| 577 |
|
| 578 |
return [=]() {
|
|
|
|
| 584 |
{ std::cout << v1 << v2 << v3; } );
|
| 585 |
auto q = p(1, 'a', 3.14); // OK: outputs 1a3.14
|
| 586 |
q(); // OK: outputs 1a3.14
|
| 587 |
```
|
| 588 |
|
| 589 |
+
— *end example*]
|
| 590 |
+
|
| 591 |
+
The function call operator or operator template is declared `const` (
|
| 592 |
[[class.mfct.non-static]]) if and only if the *lambda-expression*’s
|
| 593 |
*parameter-declaration-clause* is not followed by `mutable`. It is
|
| 594 |
+
neither virtual nor declared `volatile`. Any *noexcept-specifier*
|
| 595 |
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 requirements for a
|
| 602 |
+
constexpr function ([[dcl.constexpr]]).
|
| 603 |
+
|
| 604 |
+
[*Note 2*: Names referenced in the *lambda-declarator* are looked up in
|
| 605 |
+
the context in which the *lambda-expression* appears. — *end note*]
|
| 606 |
+
|
| 607 |
+
[*Example 2*:
|
| 608 |
+
|
| 609 |
+
``` cpp
|
| 610 |
+
auto ID = [](auto a) { return a; };
|
| 611 |
+
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); // ill-formed
|
| 618 |
+
```
|
| 619 |
+
|
| 620 |
+
— *end example*]
|
| 621 |
+
|
| 622 |
+
[*Example 3*:
|
| 623 |
+
|
| 624 |
+
``` cpp
|
| 625 |
+
auto monoid = [](auto v) { return [=] { return v; }; };
|
| 626 |
+
auto add = [](auto m1) constexpr {
|
| 627 |
+
auto ret = m1();
|
| 628 |
+
return [=](auto m2) mutable {
|
| 629 |
+
auto m1val = m1();
|
| 630 |
+
auto plus = [=](auto m2val) mutable constexpr
|
| 631 |
+
{ return m1val += m2val; };
|
| 632 |
+
ret = plus(m2());
|
| 633 |
+
return monoid(ret);
|
| 634 |
+
};
|
| 635 |
+
};
|
| 636 |
+
constexpr auto zero = monoid(0);
|
| 637 |
+
constexpr auto one = monoid(1);
|
| 638 |
+
static_assert(add(one)(zero)() == one()); // OK
|
| 639 |
+
|
| 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()); // ill-formed: two() is not a constant expression
|
| 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* has a conversion function to pointer to function with
|
| 653 |
+
C++language linkage ([[dcl.link]]) having the same parameter and return
|
| 654 |
+
types as the closure type’s function call operator. The conversion is to
|
| 655 |
+
“pointer to `noexcept` function” if the function call operator has a
|
| 656 |
+
non-throwing exception specification. The value returned by this
|
| 657 |
+
conversion function is the address of a function `F` that, when invoked,
|
| 658 |
+
has the same effect as invoking the closure type’s function call
|
| 659 |
+
operator. `F` is a constexpr function if the function call operator is a
|
| 660 |
+
constexpr function. For a generic lambda with no *lambda-capture*, the
|
| 661 |
+
closure type has a conversion function template to pointer to function.
|
| 662 |
+
The conversion function template has the same invented
|
| 663 |
*template-parameter-list*, and the pointer to function has the same
|
| 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 |
+
[*Note 3*:
|
| 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
|
| 675 |
+
instantiation of the function call operator template with the same
|
| 676 |
+
template arguments as those deduced for the conversion function
|
| 677 |
+
template. Consider the following:
|
| 678 |
|
| 679 |
``` cpp
|
| 680 |
auto glambda = [](auto a) { return a; };
|
| 681 |
int (*fp)(int) = glambda;
|
| 682 |
```
|
|
|
|
| 698 |
template<class T> operator fptr_t<T>() const
|
| 699 |
{ return &lambda_call_operator_invoker; }
|
| 700 |
};
|
| 701 |
```
|
| 702 |
|
| 703 |
+
— *end note*]
|
| 704 |
+
|
| 705 |
+
[*Example 4*:
|
| 706 |
+
|
| 707 |
``` cpp
|
| 708 |
void f1(int (*)(int)) { }
|
| 709 |
void f2(char (*)(int)) { }
|
| 710 |
|
| 711 |
void g(int (*)(int)) { } // #1
|
|
|
|
| 720 |
g(glambda); // error: ambiguous
|
| 721 |
h(glambda); // OK: calls #3 since it is convertible from ID
|
| 722 |
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 723 |
```
|
| 724 |
|
| 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. `F` is a constexpr
|
| 731 |
+
function if the corresponding specialization is a constexpr function.
|
| 732 |
+
|
| 733 |
+
[*Note 4*: This will result in the implicit instantiation of the
|
| 734 |
+
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 735 |
+
parameter types shall match the return type and parameter types of the
|
| 736 |
+
pointer to function. — *end note*]
|
| 737 |
+
|
| 738 |
+
[*Example 5*:
|
| 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)
|
| 744 |
```
|
| 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 ([[except.spec]]).
|
| 751 |
+
|
| 752 |
+
[*Example 6*:
|
| 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); // ill-formed
|
| 763 |
+
```
|
| 764 |
+
|
| 765 |
+
— *end example*]
|
| 766 |
+
|
| 767 |
The *lambda-expression*’s *compound-statement* yields the
|
| 768 |
*function-body* ([[dcl.fct.def]]) of the function call operator, but
|
| 769 |
for purposes of name lookup ([[basic.lookup]]), determining the type
|
| 770 |
and value of `this` ([[class.this]]) and transforming *id-expression*s
|
| 771 |
referring to non-static class members into class member access
|
| 772 |
expressions using `(*this)` ([[class.mfct.non-static]]), the
|
| 773 |
*compound-statement* is considered in the context of the
|
| 774 |
*lambda-expression*.
|
| 775 |
|
| 776 |
+
[*Example 7*:
|
| 777 |
+
|
| 778 |
``` cpp
|
| 779 |
struct S1 {
|
| 780 |
int x, y;
|
| 781 |
int operator()(int);
|
| 782 |
void f() {
|
|
|
|
| 786 |
};
|
| 787 |
}
|
| 788 |
};
|
| 789 |
```
|
| 790 |
|
| 791 |
+
— *end example*]
|
| 792 |
+
|
| 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 and a deleted copy assignment operator. It has a defaulted
|
| 799 |
+
copy constructor and a defaulted move constructor ([[class.copy]]).
|
| 800 |
+
|
| 801 |
+
[*Note 5*: These special member functions are implicitly defined as
|
| 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 ([[class.dtor]]).
|
| 806 |
+
|
| 807 |
+
A member of a closure type shall not be explicitly instantiated (
|
| 808 |
+
[[temp.explicit]]), explicitly specialized ([[temp.expl.spec]]), or
|
| 809 |
+
named in a `friend` declaration ([[class.friend]]).
|
| 810 |
+
|
| 811 |
+
#### Captures <a id="expr.prim.lambda.capture">[[expr.prim.lambda.capture]]</a>
|
| 812 |
+
|
| 813 |
+
``` bnf
|
| 814 |
+
lambda-capture:
|
| 815 |
+
capture-default
|
| 816 |
+
capture-list
|
| 817 |
+
capture-default ',' capture-list
|
| 818 |
+
```
|
| 819 |
+
|
| 820 |
+
``` bnf
|
| 821 |
+
capture-default:
|
| 822 |
+
'&'
|
| 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 |
+
'this'
|
| 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
|
| 866 |
+
or `this` shall not appear more than once in a *lambda-capture*.
|
| 867 |
+
|
| 868 |
+
[*Example 1*:
|
| 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]{ }; // error: this when = is the default
|
| 877 |
[i, i]{ }; // error: i repeated
|
| 878 |
+
[this, *this]{ }; // error: this appears twice
|
| 879 |
}
|
| 880 |
```
|
| 881 |
|
| 882 |
+
— *end example*]
|
| 883 |
+
|
| 884 |
A *lambda-expression* whose smallest enclosing scope is a block scope (
|
| 885 |
[[basic.scope.block]]) is a *local lambda expression*; any other
|
| 886 |
*lambda-expression* shall not have a *capture-default* or
|
| 887 |
*simple-capture* in its *lambda-introducer*. The *reaching scope* of a
|
| 888 |
local lambda expression is the set of enclosing scopes up to and
|
| 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 ([[basic.lookup.unqual]]); each such
|
| 896 |
lookup shall find an entity. An entity that is designated by a
|
| 897 |
*simple-capture* is said to be *explicitly captured*, and shall be
|
| 898 |
+
`*this` (when the *simple-capture* is “`this`” or “`* this`”) or a
|
| 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 explicitly captures a
|
| 918 |
variable of the form “`auto` *init-capture* `;`” whose declarative
|
| 919 |
region is the *lambda-expression*’s *compound-statement*, except that:
|
| 920 |
|
|
|
|
| 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 3*: This enables an *init-capture* like “`x = std::move(x)`”;
|
| 930 |
+
the second “`x`” must bind to a declaration in the surrounding
|
| 931 |
+
context. — *end note*]
|
| 932 |
+
|
| 933 |
+
[*Example 3*:
|
| 934 |
|
| 935 |
``` cpp
|
| 936 |
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; } // error: parameter and local variable have the same name
|
| 943 |
```
|
| 944 |
|
| 945 |
+
— *end example*]
|
| 946 |
+
|
| 947 |
A *lambda-expression* with an associated *capture-default* that does not
|
| 948 |
+
explicitly capture `*this` or a variable with automatic storage duration
|
| 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 |
+
- odr-uses ([[basic.def.odr]]) the entity (in the case of a variable),
|
| 955 |
+
- odr-uses ([[basic.def.odr]]) `this` (in the case of the object
|
| 956 |
+
designated by `*this`), or
|
| 957 |
- names the entity in a potentially-evaluated expression (
|
| 958 |
[[basic.def.odr]]) where the enclosing full-expression depends on a
|
| 959 |
generic lambda parameter declared within the reaching scope of the
|
| 960 |
*lambda-expression*.
|
| 961 |
|
| 962 |
+
[*Example 4*:
|
| 963 |
+
|
| 964 |
``` cpp
|
| 965 |
void f(int, const int (&)[2] = {}) { } // #1
|
| 966 |
void f(const int&, const int (&)[1]) { } // #2
|
| 967 |
void test() {
|
| 968 |
const int x = 17;
|
|
|
|
| 975 |
f(x, selector); // OK: is a dependent expression, so captures x
|
| 976 |
};
|
| 977 |
}
|
| 978 |
```
|
| 979 |
|
| 980 |
+
— *end example*]
|
| 981 |
+
|
| 982 |
All such implicitly captured entities shall be declared within the
|
| 983 |
+
reaching scope of the lambda expression.
|
| 984 |
+
|
| 985 |
+
[*Note 4*: The implicit capture of an entity by a nested
|
| 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 |
[[basic.def.odr]]) in the scope containing the *lambda-expression*. If
|
| 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 |
+
[*Example 5*:
|
| 1003 |
+
|
| 1004 |
``` cpp
|
| 1005 |
void f1(int i) {
|
| 1006 |
int const N = 20;
|
| 1007 |
auto m1 = [=]{
|
| 1008 |
int const M = 30;
|
| 1009 |
auto m2 = [i]{
|
| 1010 |
int x[N][M]; // OK: N and M are not odr-used
|
| 1011 |
+
x[0][0] = i; // OK: i is explicitly captured by m2 and implicitly captured by m1
|
|
|
|
| 1012 |
};
|
| 1013 |
};
|
| 1014 |
struct s1 {
|
| 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 captured by m3
|
| 1021 |
+
int x = n; // error: n implicitly captured by m4 but not captured by m3
|
| 1022 |
+
x += m; // OK: m implicitly captured by m4 and explicitly captured by m3
|
|
|
|
|
|
|
| 1023 |
x += i; // error: i is outside of the reaching scope
|
| 1024 |
+
x += f; // OK: this captured implicitly by m4 and explicitly by m3
|
|
|
|
| 1025 |
};
|
| 1026 |
};
|
| 1027 |
}
|
| 1028 |
};
|
| 1029 |
}
|
| 1030 |
+
|
| 1031 |
+
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
|
| 1043 |
+
}();
|
| 1044 |
+
}
|
| 1045 |
+
};
|
| 1046 |
```
|
| 1047 |
|
| 1048 |
+
— *end example*]
|
| 1049 |
+
|
| 1050 |
A *lambda-expression* appearing in a default argument shall not
|
| 1051 |
implicitly or explicitly capture any entity.
|
| 1052 |
|
| 1053 |
+
[*Example 6*:
|
| 1054 |
+
|
| 1055 |
``` cpp
|
| 1056 |
void f2() {
|
| 1057 |
int i = 1;
|
| 1058 |
void g1(int = ([i]{ return i; })()); // ill-formed
|
| 1059 |
void g2(int = ([i]{ return 0; })()); // ill-formed
|
|
|
|
| 1061 |
void g4(int = ([=]{ return 0; })()); // OK
|
| 1062 |
void g5(int = ([]{ return sizeof i; })()); // OK
|
| 1063 |
}
|
| 1064 |
```
|
| 1065 |
|
| 1066 |
+
— *end example*]
|
| 1067 |
+
|
| 1068 |
+
An entity is *captured by copy* if
|
| 1069 |
+
|
| 1070 |
+
- it is implicitly captured, the *capture-default* is `=`, and the
|
| 1071 |
+
captured entity is not `*this`, or
|
| 1072 |
+
- it is explicitly captured with a capture that is not of the form
|
| 1073 |
+
`this`, `&` *identifier*, or `&` *identifier* *initializer*.
|
| 1074 |
+
|
| 1075 |
+
For each entity captured by copy, an unnamed non-static data member is
|
| 1076 |
+
declared in the closure type. The declaration order of these members is
|
| 1077 |
+
unspecified. The type of such a data member is the referenced type if
|
| 1078 |
+
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 ([[basic.def.odr]]) of an entity
|
| 1085 |
+
captured by copy is transformed into an access to the corresponding
|
| 1086 |
+
unnamed data member of the closure type.
|
| 1087 |
+
|
| 1088 |
+
[*Note 5*: An *id-expression* that is not an odr-use refers to the
|
| 1089 |
+
original entity, never to a member of the closure type. Furthermore,
|
| 1090 |
+
such an *id-expression* does not cause the implicit capture of the
|
| 1091 |
+
entity. — *end note*]
|
| 1092 |
+
|
| 1093 |
+
If `*this` is captured by copy, each odr-use of `this` is transformed
|
| 1094 |
+
into a pointer to the corresponding unnamed data member of the closure
|
| 1095 |
+
type, cast ([[expr.cast]]) to the type of `this`.
|
| 1096 |
+
|
| 1097 |
+
[*Note 6*: The cast ensures that the transformed expression is a
|
| 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;
|
| 1115 |
+
[=] {
|
| 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 8*:
|
| 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 |
+
```
|
| 1143 |
+
|
| 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 9*:
|
| 1159 |
+
|
| 1160 |
+
The nested lambda expressions and invocations below will output
|
| 1161 |
`123234`.
|
| 1162 |
|
| 1163 |
``` cpp
|
| 1164 |
int a = 1, b = 1, c = 1;
|
| 1165 |
auto m1 = [a, &b, &c]() mutable {
|
|
|
|
| 1173 |
a = 2; b = 2; c = 2;
|
| 1174 |
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 8*: This ensures that the destructions will occur in the reverse
|
| 1213 |
+
order of the constructions. — *end note*]
|
| 1214 |
+
|
| 1215 |
+
[*Note 9*: If a non-reference entity is implicitly or explicitly
|
| 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* followed by an ellipsis is a pack expansion (
|
| 1221 |
[[temp.variadic]]). An *init-capture* followed by an ellipsis is
|
| 1222 |
ill-formed.
|
| 1223 |
|
| 1224 |
+
[*Example 11*:
|
| 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 template parameter pack (
|
| 1239 |
+
[[temp.variadic]]) over a binary operator.
|
| 1240 |
+
|
| 1241 |
+
``` bnf
|
| 1242 |
+
fold-expression:
|
| 1243 |
+
'(' cast-expression fold-operator '...' ')'
|
| 1244 |
+
'(' '...' fold-operator cast-expression ')'
|
| 1245 |
+
'(' cast-expression fold-operator '...' fold-operator cast-expression ')'
|
| 1246 |
+
```
|
| 1247 |
+
|
| 1248 |
+
``` bnf
|
| 1249 |
+
%% Ed. note: character protrusion would misalign operators with leading `-`.
|
| 1250 |
+
|
| 1251 |
+
fold-operator: one of
|
| 1252 |
+
'+ ' '- ' '* ' '/ ' '% ' '^ ' '& ' '| ' '<< ' '>> '
|
| 1253 |
+
'+=' '-=' '*=' '/=' '%=' '^=' '&=' '|=' '<<=' '>>=' '='
|
| 1254 |
+
'==' '!=' '< ' '> ' '<=' '>=' '&&' '||' ', ' '.* ' '->*'
|
| 1255 |
+
```
|
| 1256 |
+
|
| 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 parameter pack ([[temp.variadic]]).
|
| 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 parameter pack or `e2` shall contain an unexpanded
|
| 1268 |
+
parameter pack, but not both. If `e2` contains an unexpanded parameter
|
| 1269 |
+
pack, the expression is called a *binary left fold*. If `e1` contains an
|
| 1270 |
+
unexpanded parameter pack, the expression is called a *binary right
|
| 1271 |
+
fold*.
|
| 1272 |
+
|
| 1273 |
+
[*Example 1*:
|
| 1274 |
+
|
| 1275 |
+
``` cpp
|
| 1276 |
+
template<typename ...Args>
|
| 1277 |
+
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 parameter packs
|
| 1284 |
+
}
|
| 1285 |
+
```
|
| 1286 |
+
|
| 1287 |
+
— *end example*]
|
| 1288 |
+
|
| 1289 |
## Postfix expressions <a id="expr.post">[[expr.post]]</a>
|
| 1290 |
|
| 1291 |
Postfix expressions group left-to-right.
|
| 1292 |
|
| 1293 |
``` bnf
|
| 1294 |
postfix-expression:
|
| 1295 |
primary-expression
|
| 1296 |
+
postfix-expression '[' expr-or-braced-init-list ']'
|
|
|
|
| 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
|
|
|
|
| 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]]). — *end note*]
|
| 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.[^5] The expression `E1[E2]` is identical (by definition) to
|
| 1342 |
+
`*((E1)+(E2))`
|
| 1343 |
+
|
| 1344 |
+
[*Note 1*: see [[expr.unary]] and [[expr.add]] for details of `*` and
|
| 1345 |
+
`+` and [[dcl.array]] for details of arrays. — *end note*]
|
| 1346 |
+
|
| 1347 |
+
, except that in the case of an array operand, the result is an lvalue
|
| 1348 |
+
if that operand is an lvalue and an xvalue otherwise. The expression
|
| 1349 |
+
`E1` is sequenced before the expression `E2`.
|
| 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 be either an lvalue that refers
|
| 1362 |
to a function (in which case the function-to-pointer standard
|
| 1363 |
conversion ([[conv.func]]) is suppressed on the postfix expression), or
|
| 1364 |
+
it shall have function pointer type. Calling a function through an
|
| 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 |
+
[*Note 1*: A member function call of the form `f()` is interpreted as
|
| 1377 |
+
`(*this).f()` (see [[class.mfct.non-static]]). — *end note*]
|
| 1378 |
+
|
| 1379 |
+
If a function or member function name is used, the name can be
|
| 1380 |
+
overloaded (Clause [[over]]), in which case the appropriate function
|
| 1381 |
+
shall be selected according to the rules in [[over.match]]. If the
|
| 1382 |
+
selected function is non-virtual, or if the *id-expression* in the class
|
| 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 2*: The dynamic type is the type of the object referred to by
|
| 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 3*: If a function or member function name is used, and name
|
| 1394 |
+
lookup ([[basic.lookup]]) does not find a declaration of that name, the
|
| 1395 |
+
program is ill-formed. No function is implicitly declared by such a
|
| 1396 |
+
call. — *end note*]
|
| 1397 |
|
| 1398 |
If the *postfix-expression* designates a destructor ([[class.dtor]]),
|
| 1399 |
the type of the function call expression is `void`; otherwise, the type
|
| 1400 |
of the function call expression is the return type of the statically
|
| 1401 |
chosen function (i.e., ignoring the `virtual` keyword), even if the type
|
| 1402 |
of the function actually called is different. This return type shall be
|
| 1403 |
+
an object type, a reference type or cv `void`.
|
| 1404 |
|
| 1405 |
When a function is called, each parameter ([[dcl.fct]]) shall be
|
| 1406 |
initialized ([[dcl.init]], [[class.copy]], [[class.ctor]]) with its
|
| 1407 |
+
corresponding argument. If the function is a non-static member function,
|
| 1408 |
+
the `this` parameter of the function ([[class.this]]) shall be
|
| 1409 |
+
initialized with a pointer to the object of the call, converted as if by
|
| 1410 |
+
an explicit type conversion ([[expr.cast]]).
|
| 1411 |
+
|
| 1412 |
+
[*Note 4*: There is no access or ambiguity checking on this conversion;
|
| 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 parameters that have object type shall
|
| 1418 |
+
have completely-defined object type.
|
| 1419 |
+
|
| 1420 |
+
[*Note 5*: this still allows a parameter to be a pointer or reference
|
| 1421 |
+
to an incomplete class type. However, it prevents a passed-by-value
|
| 1422 |
+
parameter to have an incomplete class type. — *end note*]
|
| 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 1*: The access of the constructor, conversion functions or
|
| 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* (Clause
|
| 1434 |
+
[[except]]) with a handler that could handle the exception, this handler
|
| 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 6*: All side effects of argument evaluations are sequenced
|
| 1444 |
+
before the function is entered (see
|
| 1445 |
+
[[intro.execution]]). — *end note*]
|
| 1446 |
+
|
| 1447 |
+
[*Example 2*:
|
| 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, "");
|
| 1453 |
+
assert(s == "I have heard it works only if you believe in it"); // OK
|
| 1454 |
+
}
|
| 1455 |
+
```
|
| 1456 |
+
|
| 1457 |
+
— *end example*]
|
| 1458 |
+
|
| 1459 |
+
[*Note 7*: If an operator function is invoked using operator notation,
|
| 1460 |
+
argument evaluation is sequenced as specified for the built-in operator;
|
| 1461 |
+
see [[over.match.oper]]. — *end note*]
|
| 1462 |
+
|
| 1463 |
+
[*Example 3*:
|
| 1464 |
+
|
| 1465 |
+
``` cpp
|
| 1466 |
+
struct S {
|
| 1467 |
+
S(int);
|
| 1468 |
+
};
|
| 1469 |
+
int operator<<(S, int);
|
| 1470 |
+
int i, j;
|
| 1471 |
+
int x = S(i=1) << (i=2);
|
| 1472 |
+
int y = operator<<(S(j=1), j=2);
|
| 1473 |
+
```
|
| 1474 |
+
|
| 1475 |
+
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 operand of the
|
| 1482 |
+
evaluated `return` statement ([[stmt.return]]) in the called function
|
| 1483 |
+
(if any), except in a virtual function call if the return type of the
|
| 1484 |
+
final overrider is different from the return type of the statically
|
| 1485 |
+
chosen function, the value returned from the final overrider is
|
| 1486 |
+
converted to the return type of the statically chosen function.
|
| 1487 |
+
|
| 1488 |
+
[*Note 8*: A function can change the values of its non-const
|
| 1489 |
+
parameters, but these changes cannot affect the values of the arguments
|
| 1490 |
+
except where a parameter is of a reference type ([[dcl.ref]]); if the
|
| 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]], [[lex.literal]], [[lex.string]],
|
| 1495 |
[[dcl.array]], [[class.temporary]]). In addition, it is possible to
|
| 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 ([[dcl.fct.default]])) or more arguments (by using
|
| 1501 |
the ellipsis, `...`, or a function parameter pack ([[dcl.fct]])) than
|
| 1502 |
the number of parameters in the function definition ([[dcl.fct.def]]).
|
| 1503 |
+
|
| 1504 |
+
[*Note 9*: This implies that, except where the ellipsis (`...`) or a
|
| 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` ([[support.runtime]]).
|
| 1511 |
+
|
| 1512 |
+
[*Note 10*: This paragraph does not apply to arguments passed to a
|
| 1513 |
+
function parameter pack. Function parameter packs are expanded during
|
| 1514 |
+
template instantiation ([[temp.variadic]]), thus each such argument has
|
| 1515 |
+
a corresponding parameter when a function template specialization is
|
| 1516 |
+
actually called. — *end note*]
|
| 1517 |
+
|
| 1518 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1519 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 1520 |
conversions are performed on the argument expression. An argument that
|
| 1521 |
+
has type cv `std::nullptr_t` is converted to type `void*` (
|
| 1522 |
+
[[conv.ptr]]). After these conversions, if the argument does not have
|
| 1523 |
+
arithmetic, enumeration, pointer, pointer to member, or class type, the
|
| 1524 |
+
program is ill-formed. Passing a potentially-evaluated argument of class
|
| 1525 |
+
type (Clause [[class]]) having a non-trivial copy constructor, a
|
| 1526 |
+
non-trivial move constructor, or a non-trivial destructor, with no
|
| 1527 |
+
corresponding parameter, is conditionally-supported with
|
| 1528 |
+
*implementation-defined* semantics. If the argument has integral or
|
| 1529 |
enumeration type that is subject to the integral promotions (
|
| 1530 |
+
[[conv.prom]]), or a floating-point type that is subject to the
|
| 1531 |
+
floating-point promotion ([[conv.fpprom]]), the value of the argument
|
| 1532 |
+
is converted to the promoted type before the call. These promotions are
|
| 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* ([[dcl.type.simple]]) or
|
| 1545 |
*typename-specifier* ([[temp.res]]) followed by a parenthesized
|
| 1546 |
+
optional *expression-list* or by a *braced-init-list* (the initializer)
|
| 1547 |
+
constructs a value of the specified type given the initializer. If the
|
| 1548 |
+
type is a placeholder for a deduced class type, it is replaced by the
|
| 1549 |
+
return type of the function selected by overload resolution for class
|
| 1550 |
+
template deduction ([[over.match.class.deduct]]) for the remainder of
|
| 1551 |
+
this section.
|
| 1552 |
+
|
| 1553 |
+
If the initializer is a parenthesized single expression, the type
|
| 1554 |
conversion expression is equivalent (in definedness, and if defined in
|
| 1555 |
meaning) to the corresponding cast expression ([[expr.cast]]). If the
|
| 1556 |
+
type is cv `void` and the initializer is `()`, the expression is a
|
| 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 ([[dcl.init]]) with the
|
| 1560 |
+
initializer. For an expression of the form `T()`, `T` shall not be an
|
| 1561 |
+
array type.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1562 |
|
| 1563 |
### Pseudo destructor call <a id="expr.pseudo">[[expr.pseudo]]</a>
|
| 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
|
|
|
|
| 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` ([[temp.names]]), and then followed
|
| 1589 |
by an *id-expression*, is a postfix expression. The postfix expression
|
| 1590 |
before the dot or arrow is evaluated;[^6] the result of that evaluation,
|
| 1591 |
together with the *id-expression*, determines the result of the entire
|
| 1592 |
postfix expression.
|
| 1593 |
|
| 1594 |
+
For the first option (dot) the first expression shall be a glvalue
|
| 1595 |
+
having complete class type. For the second option (arrow) the first
|
| 1596 |
+
expression shall be a prvalue having pointer to complete class type. The
|
| 1597 |
+
expression `E1->E2` is converted to the equivalent form `(*(E1)).E2`;
|
| 1598 |
+
the remainder of [[expr.ref]] will address only the first option
|
| 1599 |
+
(dot).[^7] In either case, the *id-expression* shall name a member of
|
| 1600 |
+
the class or of one of its base classes.
|
| 1601 |
+
|
| 1602 |
+
[*Note 1*: Because the name of a class is inserted in its class scope
|
| 1603 |
+
(Clause [[class]]), the name of a class is also considered a nested
|
| 1604 |
+
member of that class. — *end note*]
|
| 1605 |
+
|
| 1606 |
+
[*Note 2*: [[basic.lookup.classref]] describes how names are looked up
|
| 1607 |
+
after the `.` and `->` operators. — *end note*]
|
| 1608 |
|
| 1609 |
Abbreviating *postfix-expression.id-expression* as `E1.E2`, `E1` is
|
| 1610 |
+
called the *object expression*. If `E2` is a bit-field, `E1.E2` is a
|
| 1611 |
+
bit-field. The type and value category of `E1.E2` are determined as
|
| 1612 |
+
follows. In the remainder of [[expr.ref]], *cq* represents either
|
| 1613 |
+
`const` or the absence of `const` and *vq* represents either `volatile`
|
| 1614 |
+
or the absence of `volatile`. *cv* represents an arbitrary set of
|
| 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
|
|
|
|
| 1641 |
lvalue; the expression designates the static member function. The
|
| 1642 |
type of `E1.E2` is the same type as that of `E2`, namely “function
|
| 1643 |
of parameter-type-list returning `T`”.
|
| 1644 |
- Otherwise, if `E1.E2` refers to a non-static member function and the
|
| 1645 |
type of `E2` is “function of parameter-type-list *cv*
|
| 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 ([[class.member.lookup]]) of the naming class (
|
| 1660 |
+
[[class.access.base]]) of `E2`.
|
| 1661 |
+
|
| 1662 |
+
[*Note 4*: The program is also ill-formed if the naming class is an
|
| 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. The value of the operand object is modified by adding `1`
|
| 1676 |
+
to it. The value computation of the `++` expression is sequenced before
|
| 1677 |
+
the modification of the operand object. With respect to an
|
| 1678 |
indeterminately-sequenced function call, the operation of postfix `++`
|
| 1679 |
+
is a single evaluation.
|
| 1680 |
+
|
| 1681 |
+
[*Note 2*: Therefore, a function call shall not intervene between the
|
| 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
|
| 1689 |
+
[[expr.ass]].
|
| 1690 |
|
| 1691 |
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 *cv* `void`”. The
|
| 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`, or it is the same as `T` except
|
| 1713 |
that the class object type in `T` is more cv-qualified than the class
|
| 1714 |
object type in `v`, the result is `v` (converted if necessary).
|
| 1715 |
|
|
|
|
| 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`. Similarly, if
|
| 1722 |
`T` is “reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B`
|
| 1723 |
is a base class of `D`, the result is the unique `B` subobject of the
|
| 1724 |
+
`D` object referred to by `v`.[^8] In both the pointer and reference
|
| 1725 |
+
cases, the program is ill-formed if *cv2* has greater cv-qualification
|
| 1726 |
+
than *cv1* or if `B` is an inaccessible or ambiguous base class of `D`.
|
| 1727 |
+
|
| 1728 |
+
[*Example 1*:
|
| 1729 |
|
| 1730 |
``` cpp
|
| 1731 |
struct B { };
|
| 1732 |
struct D : B { };
|
| 1733 |
void foo(D* dp) {
|
| 1734 |
B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
|
| 1735 |
}
|
| 1736 |
```
|
| 1737 |
|
| 1738 |
+
— *end example*]
|
| 1739 |
+
|
| 1740 |
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
|
| 1741 |
type ([[class.virtual]]).
|
| 1742 |
|
| 1743 |
+
If `T` is “pointer to *cv* `void`”, then the result is a pointer to the
|
| 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 `public` base class subobject of a `C` object,
|
| 1753 |
and if only one object of type `C` is derived from the subobject
|
|
|
|
| 1756 |
- Otherwise, if `v` points (refers) to a `public` base class subobject
|
| 1757 |
of the most derived object, and the type of the most derived object
|
| 1758 |
has a base class, of type `C`, that is unambiguous and `public`, the
|
| 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 ([[except.throw]]) of a type that would match a handler (
|
| 1766 |
[[except.handle]]) of type `std::bad_cast` ([[bad.cast]]).
|
| 1767 |
|
| 1768 |
+
[*Example 2*:
|
| 1769 |
+
|
| 1770 |
``` cpp
|
| 1771 |
class A { virtual void f(); };
|
| 1772 |
class B { virtual void g(); };
|
| 1773 |
class D : public virtual A, private B { };
|
| 1774 |
void g() {
|
|
|
|
| 1777 |
A* ap = &d; // public derivation, no cast needed
|
| 1778 |
D& dr = dynamic_cast<D&>(*bp); // fails
|
| 1779 |
ap = dynamic_cast<A*>(bp); // fails
|
| 1780 |
bp = dynamic_cast<B*>(ap); // fails
|
| 1781 |
ap = dynamic_cast<A*>(&d); // succeeds
|
| 1782 |
+
bp = dynamic_cast<B*>(&d); // ill-formed (not a runtime check)
|
| 1783 |
}
|
| 1784 |
|
| 1785 |
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; // ill-formed: cast from virtual base
|
| 1792 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 1793 |
}
|
| 1794 |
```
|
| 1795 |
|
| 1796 |
+
— *end example*]
|
| 1797 |
+
|
| 1798 |
+
[*Note 1*: [[class.cdtor]] describes the behavior of a `dynamic_cast`
|
| 1799 |
+
applied to an object under construction or destruction. — *end note*]
|
| 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` ([[type.info]]) and dynamic type `const`
|
|
|
|
| 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]]), array-to-pointer ([[conv.array]]), and
|
| 1826 |
function-to-pointer ([[conv.func]]) conversions are not applied to the
|
| 1827 |
+
expression. If the expression is a prvalue, the temporary
|
| 1828 |
+
materialization conversion ([[conv.rval]]) is applied. The expression
|
| 1829 |
+
is an unevaluated operand (Clause [[expr]]).
|
| 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*:
|
| 1844 |
+
|
| 1845 |
``` cpp
|
| 1846 |
+
class D { ... };
|
| 1847 |
D d1;
|
| 1848 |
const D d2;
|
| 1849 |
|
| 1850 |
typeid(d1) == typeid(d2); // yields true
|
| 1851 |
typeid(D) == typeid(const D); // yields true
|
| 1852 |
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>` ([[type.info]]) is not included prior to a
|
| 1859 |
use of `typeid`, the program is ill-formed.
|
| 1860 |
|
| 1861 |
+
[*Note 1*: [[class.cdtor]] describes the behavior of `typeid` applied
|
| 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 ([[expr.const.cast]]).
|
| 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 (Clause
|
| 1875 |
+
[[class.derived]]) from `B`, if *cv2* is the same cv-qualification as,
|
| 1876 |
+
or greater cv-qualification than, *cv1*. If `B` is a virtual base class
|
| 1877 |
+
of `D` or a base class of a virtual base class of `D`, or if no valid
|
| 1878 |
+
standard conversion from “pointer to `D`” to “pointer to `B`” exists (
|
| 1879 |
+
[[conv.ptr]]), the program is ill-formed. An xvalue of type “*cv1* `B`”
|
| 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.
|
| 1885 |
+
|
| 1886 |
+
[*Example 1*:
|
| 1887 |
|
| 1888 |
``` cpp
|
| 1889 |
struct B { };
|
| 1890 |
struct D : public B { };
|
| 1891 |
D d;
|
| 1892 |
B &br = d;
|
| 1893 |
|
| 1894 |
static_cast<D&>(br); // produces lvalue to the original d object
|
| 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]]). If the value is not a bit-field, the result refers to
|
| 1902 |
+
the object or the specified base class subobject thereof; otherwise, the
|
| 1903 |
+
lvalue-to-rvalue conversion ([[conv.lval]]) is applied to the bit-field
|
| 1904 |
+
and the resulting prvalue is used as the *expression* of the
|
| 1905 |
+
`static_cast` for the remainder of this section. If `T2` is an
|
| 1906 |
+
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 1907 |
+
[[class.member.lookup]]) base class of `T1`, a program that necessitates
|
| 1908 |
+
such a cast is ill-formed.
|
| 1909 |
+
|
| 1910 |
+
An expression `e` can be explicitly converted to a type `T` if there is
|
| 1911 |
+
an implicit conversion sequence ([[over.best.ics]]) from `e` to `T`, or
|
| 1912 |
+
if overload resolution for a direct-initialization ([[dcl.init]]) of an
|
| 1913 |
+
object or reference of type `T` from `e` would find at least one viable
|
| 1914 |
+
function ([[over.match.viable]]). If `T` is a reference type, the
|
| 1915 |
+
effect is the same as performing the declaration and initialization
|
| 1916 |
+
|
| 1917 |
+
``` cpp
|
| 1918 |
+
T t(e);
|
| 1919 |
+
```
|
| 1920 |
+
|
| 1921 |
+
for some invented temporary variable `t` ([[dcl.init]]) and then using
|
| 1922 |
+
the temporary variable as the result of the conversion. Otherwise, the
|
| 1923 |
+
result object is direct-initialized from `e`.
|
| 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 (Clause [[expr]]).
|
| 1935 |
+
|
| 1936 |
+
[*Note 2*: However, if the value is in a temporary object (
|
| 1937 |
+
[[class.temporary]]), the destructor for that object is not executed
|
| 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 (Clause [[conv]]) not
|
| 1942 |
containing an lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1943 |
[[conv.array]]), function-to-pointer ([[conv.func]]), null pointer (
|
| 1944 |
+
[[conv.ptr]]), null member pointer ([[conv.mem]]), boolean (
|
| 1945 |
+
[[conv.bool]]), or function pointer ([[conv.fctptr]]) conversion, can
|
| 1946 |
+
be performed explicitly using `static_cast`. A program is ill-formed if
|
| 1947 |
+
it uses `static_cast` to perform the inverse of an ill-formed standard
|
| 1948 |
+
conversion sequence.
|
| 1949 |
+
|
| 1950 |
+
[*Example 2*:
|
| 1951 |
|
| 1952 |
``` cpp
|
| 1953 |
struct B { };
|
| 1954 |
struct D : private B { };
|
| 1955 |
void f() {
|
| 1956 |
+
static_cast<D*>((B*)0); // error: B is a private base of D
|
| 1957 |
+
static_cast<int B::*>((int D::*)0); // error: B is a private base of D
|
| 1958 |
}
|
| 1959 |
```
|
| 1960 |
|
| 1961 |
+
— *end example*]
|
| 1962 |
+
|
| 1963 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 1964 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) conversions
|
| 1965 |
are applied to the operand. Such a `static_cast` is subject to the
|
| 1966 |
restriction that the explicit conversion does not cast away constness (
|
| 1967 |
[[expr.const.cast]]), and the following additional rules for specific
|
|
|
|
| 1975 |
type. Otherwise, the resulting value is unspecified. A value of a scoped
|
| 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. The value is unchanged if the original value
|
| 1982 |
+
is within the range of the enumeration values ([[dcl.enum]]).
|
| 1983 |
+
Otherwise, the behavior is undefined. A value of floating-point type can
|
| 1984 |
+
also be explicitly converted to an enumeration type. The resulting value
|
| 1985 |
+
is the same as converting the original value to the underlying type of
|
| 1986 |
+
the enumeration ([[conv.fpint]]), and subsequently to the enumeration
|
| 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 (Clause [[class.derived]]) from `B`, if *cv2* is the same
|
| 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 ([[conv.ptr]]), the program is ill-formed. The
|
| 1996 |
+
null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 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 `B` is a base class (Clause [[class.derived]]) of `D`, if
|
| 2005 |
+
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 2006 |
+
*cv1*.[^11] If no valid standard conversion from “pointer to member of
|
| 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 3*: Although class `B` need not contain the original member, the
|
| 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
|
| 2021 |
+
prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
|
| 2022 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 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 ([[basic.compound]]) with *a*, the result is a
|
| 2029 |
+
pointer to *b*. Otherwise, the pointer value is unchanged by the
|
| 2030 |
+
conversion.
|
| 2031 |
+
|
| 2032 |
+
[*Example 3*:
|
| 2033 |
|
| 2034 |
``` cpp
|
| 2035 |
T* p1 = new T;
|
| 2036 |
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;
|
|
|
|
| 2055 |
The `reinterpret_cast` operator shall not cast away constness (
|
| 2056 |
[[expr.const.cast]]). An expression of integral, enumeration, pointer,
|
| 2057 |
or pointer-to-member type can be explicitly converted to its own type;
|
| 2058 |
such a cast yields the value of its operand.
|
| 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 it. The mapping function is *implementation-defined*.
|
| 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;
|
| 2071 |
+
the conversion has the same meaning and validity as a conversion of
|
| 2072 |
+
`(void*)0` to the integral type.
|
| 2073 |
+
|
| 2074 |
+
[*Note 3*: A `reinterpret_cast` cannot be used to convert a value of
|
| 2075 |
+
any type to the type `std::nullptr_t`. — *end note*]
|
| 2076 |
|
| 2077 |
A value of integral type or enumeration type can be explicitly converted
|
| 2078 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 2079 |
any such exists on the implementation) and back to the same pointer type
|
| 2080 |
will have its original value; mappings between pointers and integers are
|
| 2081 |
+
otherwise *implementation-defined*.
|
| 2082 |
+
|
| 2083 |
+
[*Note 4*: Except as described in [[basic.stc.dynamic.safety]], the
|
| 2084 |
+
result of such a conversion will not be a safely-derived pointer
|
| 2085 |
+
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 ([[dcl.fct]]) that is not the same as the type used in
|
| 2092 |
+
the definition of the function is undefined. — *end note*]
|
| 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.[^12] When a prvalue `v` of object pointer type is
|
| 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
|
| 2109 |
+
alignment requirements of `T2` are no stricter than those of `T1`) and
|
| 2110 |
+
back to its original type yields the original pointer
|
| 2111 |
+
value. — *end note*]
|
| 2112 |
|
| 2113 |
Converting a function pointer to an object pointer type or vice versa is
|
| 2114 |
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 ([[conv.ptr]]) is converted to the null pointer
|
| 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.[^13] The null member pointer value ([[conv.mem]]) is
|
|
|
|
| 2143 |
|
| 2144 |
A glvalue expression of type `T1` can be cast to the type “reference to
|
| 2145 |
`T2`” if an expression of type “pointer to `T1`” can be explicitly
|
| 2146 |
converted to the type “pointer to `T2`” using a `reinterpret_cast`. The
|
| 2147 |
result refers to the same object as the source glvalue, but with the
|
| 2148 |
+
specified type.
|
| 2149 |
+
|
| 2150 |
+
[*Note 9*: That is, for lvalues, a reference cast
|
| 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`
|
|
|
|
| 2166 |
function-to-pointer ([[conv.func]]) standard conversions are performed
|
| 2167 |
on the expression `v`. Conversions that can be performed explicitly
|
| 2168 |
using `const_cast` are listed below. No other conversion shall be
|
| 2169 |
performed explicitly using `const_cast`.
|
| 2170 |
|
| 2171 |
+
[*Note 1*: Subject to the restrictions in this section, an expression
|
| 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` ([[conv.qual]]), a prvalue of type
|
| 2176 |
+
`T1` may be explicitly converted to the type `T2` using a `const_cast`.
|
| 2177 |
+
The result of a `const_cast` refers to the original entity.
|
| 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{}; // OK, reference binds to temporary array object after qualification conversion to type CA
|
| 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*]
|
| 2191 |
|
| 2192 |
For two object types `T1` and `T2`, if a pointer to `T1` can be
|
| 2193 |
explicitly converted to the type “pointer to `T2`” using a `const_cast`,
|
| 2194 |
then the following conversions can also be made:
|
| 2195 |
|
|
|
|
| 2198 |
- a glvalue of type `T1` can be explicitly converted to an xvalue of
|
| 2199 |
type `T2` using the cast `const_cast<T2&&>`; and
|
| 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 ([[conv.rval]]) otherwise.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2206 |
|
| 2207 |
A null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 2208 |
value of the destination type. The null member pointer value (
|
| 2209 |
[[conv.mem]]) is converted to the null member pointer value of the
|
| 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[^15] may produce
|
| 2215 |
+
undefined behavior ([[dcl.type.cv]]). — *end note*]
|
| 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 |
+
[[conv.qual]]) of `T1` yielding *n* such that `T2` has a
|
| 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
|
| 2226 |
xvalue of type `T2` using an rvalue reference cast casts away constness
|
| 2227 |
if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
|
| 2228 |
to `T2`” casts away constness.
|
| 2229 |
|
| 2230 |
+
[*Note 3*: Some conversions which involve only changes in
|
| 2231 |
+
cv-qualification cannot be done using `const_cast.` For instance,
|
| 2232 |
+
conversions between pointers to functions are not covered because such
|
| 2233 |
+
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 |
|
|
|
|
| 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 *cv* `void`) is valid. The lvalue thus obtained can be used in
|
| 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. The
|
| 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 |
+
[*Note 2*: In particular, the address of an object of type “cv `T`” is
|
| 2288 |
+
“pointer to cv `T`”, with the same cv-qualification. — *end note*]
|
| 2289 |
+
|
| 2290 |
+
For purposes of pointer arithmetic ([[expr.add]]) and comparison (
|
| 2291 |
+
[[expr.rel]], [[expr.eq]]), an object that is not an array element whose
|
| 2292 |
+
address is taken in this way is considered to belong to an array with
|
| 2293 |
+
one element of type `T`.
|
| 2294 |
+
|
| 2295 |
+
[*Example 1*:
|
| 2296 |
|
| 2297 |
``` cpp
|
| 2298 |
struct A { int i; };
|
| 2299 |
struct B : A { };
|
| 2300 |
... &B::i ... // has type int A::*
|
| 2301 |
+
int a;
|
| 2302 |
+
int* p1 = &a;
|
| 2303 |
+
int* p2 = p1 + 1; // defined behavior
|
| 2304 |
+
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 ([[dcl.stc]]) does not reflect the `mutable` specifier
|
| 2311 |
+
associated with the non-static data member. — *end note*]
|
| 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 |
+
[[conv.func]]). Nor is `&unqualified-id` a pointer to member, even
|
| 2323 |
+
within the scope of the *unqualified-id*’s class. — *end note*]
|
| 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 (Clause [[over]]) can be taken
|
| 2331 |
only in a context that uniquely determines which version of the
|
| 2332 |
+
overloaded function is referred to (see [[over.over]]).
|
| 2333 |
+
|
| 2334 |
+
[*Note 5*: Since the context might determine whether the operand is a
|
| 2335 |
+
static or non-static member function, the context can also affect
|
| 2336 |
+
whether the expression has type “pointer to function” or “pointer to
|
| 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.
|
|
|
|
| 2352 |
converted to `bool` (Clause [[conv]]); its value is `true` if the
|
| 2353 |
converted operand is `false` and `false` otherwise. The type of the
|
| 2354 |
result is `bool`.
|
| 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 |
+
*class-name* or *decltype-specifier*. The ambiguity is resolved by
|
| 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 *class-name* or
|
| 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`. The operand shall
|
| 2372 |
+
be a modifiable lvalue. The type of the operand shall be an arithmetic
|
| 2373 |
+
type other than cv `bool`, or a pointer to a completely-defined object
|
| 2374 |
+
type. The result is the updated operand; it is an lvalue, and it is a
|
| 2375 |
+
bit-field if the operand is a bit-field. The expression `++x` is
|
| 2376 |
+
equivalent to `x+=1`.
|
|
|
|
|
|
|
| 2377 |
|
| 2378 |
+
[*Note 1*: See the discussions of addition ([[expr.add]]) and
|
| 2379 |
+
assignment operators ([[expr.ass]]) for information on
|
| 2380 |
+
conversions. — *end note*]
|
| 2381 |
+
|
| 2382 |
+
The operand of prefix `\dcr` is modified by subtracting `1`. The
|
| 2383 |
+
requirements on the operand of prefix `\dcr` and the properties of its
|
| 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 |
### Sizeof <a id="expr.sizeof">[[expr.sizeof]]</a>
|
| 2390 |
|
| 2391 |
The `sizeof` operator yields the number of bytes in the object
|
| 2392 |
representation of its operand. The operand is either an expression,
|
| 2393 |
which is an unevaluated operand (Clause [[expr]]), or a parenthesized
|
| 2394 |
*type-id*. The `sizeof` operator shall not be applied to an expression
|
| 2395 |
+
that has function or incomplete type, to the parenthesized name of such
|
| 2396 |
+
types, or to a glvalue that designates a bit-field. `sizeof(char)`,
|
| 2397 |
+
`sizeof(signed char)` and `sizeof(unsigned char)` are `1`. The result of
|
| 2398 |
+
`sizeof` applied to any other fundamental type ([[basic.fundamental]])
|
| 2399 |
+
is *implementation-defined*.
|
| 2400 |
+
|
| 2401 |
+
[*Note 1*: In particular, `sizeof(bool)`, `sizeof(char16_t)`,
|
| 2402 |
+
`sizeof(char32_t)`, and `sizeof(wchar_t)` are
|
| 2403 |
+
implementation-defined.[^16] — *end note*]
|
| 2404 |
+
|
| 2405 |
+
[*Note 2*: See [[intro.memory]] for the definition of *byte* and
|
| 2406 |
+
[[basic.types]] for the definition of *object
|
| 2407 |
+
representation*. — *end note*]
|
| 2408 |
|
| 2409 |
When applied to a reference or a reference type, the result is the size
|
| 2410 |
of the referenced type. When applied to a class, the result is the
|
| 2411 |
number of bytes in an object of that class including any padding
|
| 2412 |
required for placing objects of that type in an array. The size of a
|
|
|
|
| 2419 |
The `sizeof` operator can be applied to a pointer to a function, but
|
| 2420 |
shall not be applied directly to a function.
|
| 2421 |
|
| 2422 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 2423 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 2424 |
+
conversions are not applied to the operand of `sizeof`. If the operand
|
| 2425 |
+
is a prvalue, the temporary materialization conversion ([[conv.rval]])
|
| 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>
|
| 2437 |
struct count {
|
| 2438 |
static const std::size_t value = sizeof...(Types);
|
| 2439 |
};
|
| 2440 |
```
|
| 2441 |
|
| 2442 |
+
— *end example*]
|
| 2443 |
+
|
| 2444 |
The result of `sizeof` and `sizeof...` is a constant of type
|
| 2445 |
+
`std::size_t`.
|
| 2446 |
+
|
| 2447 |
+
[*Note 3*: `std::size_t` is defined in the standard header
|
| 2448 |
+
`<cstddef>` ([[cstddef.syn]], [[support.types.layout]]). — *end note*]
|
| 2449 |
|
| 2450 |
### New <a id="expr.new">[[expr.new]]</a>
|
| 2451 |
|
| 2452 |
The *new-expression* attempts to create an object of the *type-id* (
|
| 2453 |
[[dcl.name]]) or *new-type-id* to which it is applied. The type of that
|
| 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]], [[basic.types]], [[class.abstract]]).
|
| 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 |
'::'ₒₚₜ 'new' new-placementₒₚₜ new-type-id new-initializerₒₚₜ
|
| 2468 |
'::'ₒₚₜ 'new' new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
|
|
|
|
| 2495 |
'(' expression-listₒₚₜ ')'
|
| 2496 |
braced-init-list
|
| 2497 |
```
|
| 2498 |
|
| 2499 |
Entities created by a *new-expression* have dynamic storage duration (
|
| 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 |
|
| 2521 |
+
[*Example 1*:
|
| 2522 |
+
|
| 2523 |
``` cpp
|
| 2524 |
new auto(1); // allocated type is int
|
| 2525 |
auto x = new auto('a'); // allocated type is char, x is of type char*
|
| 2526 |
+
|
| 2527 |
+
template<class T> struct A { A(T, T); };
|
| 2528 |
+
auto y = new A{1, 2}; // allocated type is A<int>
|
| 2529 |
```
|
| 2530 |
|
| 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 4*: This prevents ambiguities between the declarator operators
|
| 2537 |
+
`&`, `&&`, `*`, and `[]` and their expression
|
| 2538 |
+
counterparts. — *end note*]
|
| 2539 |
+
|
| 2540 |
+
[*Example 2*:
|
| 2541 |
|
| 2542 |
``` cpp
|
| 2543 |
new int * i; // syntax error: parsed as (new int*) i, not as (new int)*i
|
| 2544 |
```
|
| 2545 |
|
| 2546 |
The `*` is the pointer declarator and not the multiplication operator.
|
| 2547 |
|
| 2548 |
+
— *end example*]
|
| 2549 |
+
|
| 2550 |
+
[*Note 5*:
|
| 2551 |
+
|
| 2552 |
+
Parentheses in a *new-type-id* of a *new-expression* can have surprising
|
| 2553 |
effects.
|
| 2554 |
|
| 2555 |
+
[*Example 3*:
|
| 2556 |
+
|
| 2557 |
``` cpp
|
| 2558 |
new int(*[10])(); // error
|
| 2559 |
```
|
| 2560 |
|
| 2561 |
is ill-formed because the binding is
|
|
|
|
| 2570 |
``` cpp
|
| 2571 |
new (int (*[10])());
|
| 2572 |
```
|
| 2573 |
|
| 2574 |
allocates an array of `10` pointers to functions (taking no argument and
|
| 2575 |
+
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 |
+
|
| 2586 |
+
[*Note 6*: Both `new int` and `new int[10]` have type `int*` and the
|
| 2587 |
+
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 ([[expr.const]]) of type `std::size_t`
|
| 2594 |
and shall evaluate to a strictly positive value. The *expression* in a
|
| 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 |
The *expression* in a *noptr-new-declarator* is erroneous if:
|
| 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 ([[over.ics.user]])[^18] is less than
|
| 2608 |
zero;
|
| 2609 |
- its value is such that the size of the allocated object would exceed
|
| 2610 |
+
the *implementation-defined* limit (Annex [[implimits]]); or
|
| 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 literal ([[lex.string]])) exceeds the
|
| 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 ([[except.spec]]), the value
|
| 2623 |
+
of the *new-expression* is the null pointer value of the required
|
| 2624 |
+
result type;
|
| 2625 |
+
- otherwise, the *new-expression* terminates by throwing an exception
|
| 2626 |
+
of a type that would match a handler ([[except.handle]]) of type
|
| 2627 |
+
`std::bad_array_new_length` ([[new.badlength]]).
|
| 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 ([[basic.stc.dynamic.allocation]]). If the
|
| 2634 |
*new-expression* terminates by throwing an exception, it may release
|
| 2635 |
storage by calling a deallocation function (
|
| 2636 |
[[basic.stc.dynamic.deallocation]]). If the allocated type is a
|
| 2637 |
non-array type, the allocation function’s name is `operator new` and the
|
| 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 shall provide default definitions for the
|
| 2643 |
+
global allocation functions ([[basic.stc.dynamic]],
|
| 2644 |
+
[[new.delete.single]], [[new.delete.array]]). A C++program can provide
|
| 2645 |
+
alternative definitions of these functions ([[replacement.functions]])
|
| 2646 |
+
and/or class-specific versions ([[class.free]]). The set of allocation
|
| 2647 |
+
and deallocation functions that may be called by a *new-expression* may
|
| 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,
|
| 2653 |
if the allocated type is a class type `T` or array thereof, the
|
| 2654 |
allocation function’s name is looked up in the scope of `T`. If this
|
|
|
|
| 2674 |
*delete-expression*s, and
|
| 2675 |
- the evaluation of `e2` is sequenced before the evaluation of the
|
| 2676 |
*delete-expression* whose operand is the pointer value produced by
|
| 2677 |
`e1`.
|
| 2678 |
|
| 2679 |
+
[*Example 5*:
|
| 2680 |
+
|
| 2681 |
``` cpp
|
| 2682 |
void mergeable(int x) {
|
| 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]};
|
|
|
|
| 2698 |
throw;
|
| 2699 |
}
|
| 2700 |
}
|
| 2701 |
```
|
| 2702 |
|
| 2703 |
+
— *end example*]
|
| 2704 |
+
|
| 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. For arrays of `char`,
|
| 2711 |
+
`unsigned char`, and `std::byte`, the difference between the result of
|
| 2712 |
+
the *new-expression* and the address returned by the allocation function
|
| 2713 |
shall be an integral multiple of the strictest fundamental alignment
|
| 2714 |
requirement ([[basic.align]]) of any object type whose size is no
|
| 2715 |
+
greater than the size of the array being created.
|
| 2716 |
+
|
| 2717 |
+
[*Note 8*: Because allocation functions are assumed to return pointers
|
| 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 |
|
| 2723 |
When a *new-expression* calls an allocation function and that allocation
|
| 2724 |
has been extended, the size argument to the allocation call shall be no
|
| 2725 |
greater than the sum of the sizes for the omitted calls as specified
|
| 2726 |
above, plus the size for the extended call had it not been extended,
|
| 2727 |
plus any padding necessary to align the allocated objects within the
|
| 2728 |
allocated memory.
|
| 2729 |
|
| 2730 |
The *new-placement* syntax is used to supply additional arguments to an
|
| 2731 |
+
allocation function; such an expression is called a *placement
|
| 2732 |
+
*new-expression**.
|
| 2733 |
+
|
| 2734 |
+
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 and the
|
| 2741 |
+
allocated object type has new-extended alignment, the alignment argument
|
| 2742 |
+
is removed from the argument list, and overload resolution is performed
|
| 2743 |
+
again.
|
| 2744 |
+
|
| 2745 |
+
[*Example 6*:
|
| 2746 |
+
|
| 2747 |
+
- `new T` results in one of the following calls:
|
| 2748 |
+
``` cpp
|
| 2749 |
+
operator new(sizeof(T))
|
| 2750 |
+
operator new(sizeof(T), std::align_val_t(alignof(T)))
|
| 2751 |
+
```
|
| 2752 |
+
- `new(2,f) T` results in one of the following calls:
|
| 2753 |
+
``` cpp
|
| 2754 |
+
operator new(sizeof(T), 2, f)
|
| 2755 |
+
operator new(sizeof(T), std::align_val_t(alignof(T)), 2, f)
|
| 2756 |
+
```
|
| 2757 |
+
- `new T[5]` results in one of the following calls:
|
| 2758 |
+
``` cpp
|
| 2759 |
+
operator new[](sizeof(T) * 5 + x)
|
| 2760 |
+
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)))
|
| 2761 |
+
```
|
| 2762 |
+
- `new(2,f) T[5]` results in one of the following calls:
|
| 2763 |
+
``` cpp
|
| 2764 |
+
operator new[](sizeof(T) * 5 + x, 2, f)
|
| 2765 |
+
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)), 2, f)
|
| 2766 |
+
```
|
| 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 the library function
|
| 2773 |
+
`operator new[](std::size_t, void*)` and other placement allocation
|
| 2774 |
+
functions. The amount of overhead may vary from one invocation of `new`
|
| 2775 |
+
to another.
|
| 2776 |
+
|
| 2777 |
+
— *end example*]
|
| 2778 |
+
|
| 2779 |
+
[*Note 9*: Unless an allocation function has a non-throwing exception
|
| 2780 |
+
specification ([[except.spec]]), it indicates failure to allocate
|
| 2781 |
+
storage by throwing a `std::bad_alloc` exception (
|
| 2782 |
+
[[basic.stc.dynamic.allocation]], Clause [[except]], [[bad.alloc]]);
|
| 2783 |
+
it returns a non-null pointer otherwise. If the allocation function has
|
| 2784 |
+
a non-throwing exception specification, it returns null to indicate
|
| 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]]) that returns null, the behavior is undefined.
|
| 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 10*: When the allocation function returns a value other than
|
| 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 |
+
default-initialized ([[dcl.init]]). \[*Note 11*: If no initialization
|
| 2806 |
+
is performed, the object has an indeterminate value. — *end note*]
|
| 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 ([[class.free]]), and the
|
| 2818 |
constructor ([[class.ctor]]). If the *new-expression* creates an array
|
| 2819 |
of objects of class type, the destructor is potentially invoked (
|
| 2820 |
[[class.dtor]]).
|
| 2821 |
|
| 2822 |
If any part of the object initialization described above[^19] terminates
|
| 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 12*: This is appropriate when the called allocation function
|
| 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.
|
| 2836 |
Otherwise, if the allocated type is a class type `T` or an array
|
| 2837 |
thereof, the deallocation function’s name is looked up in the scope of
|
|
|
|
| 2843 |
declaration of a placement allocation function if it has the same number
|
| 2844 |
of parameters and, after parameter transformations ([[dcl.fct]]), all
|
| 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 with a parameter of type `std::size_t` (
|
| 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 ([[expr.delete]])
|
| 2854 |
|
| 2855 |
+
[*Example 7*:
|
| 2856 |
+
|
| 2857 |
``` cpp
|
| 2858 |
struct S {
|
| 2859 |
// Placement allocation function:
|
| 2860 |
static void* operator new(std::size_t, std::size_t);
|
| 2861 |
|
|
|
|
| 2865 |
|
| 2866 |
S* p = new (0) S; // ill-formed: non-placement deallocation function matches
|
| 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
|
|
|
|
| 2909 |
a pointer to a subobject ([[intro.object]]) representing a base class
|
| 2910 |
of such an object (Clause [[class.derived]]). If not, the behavior is
|
| 2911 |
undefined. In the second alternative (*delete array*), the value of the
|
| 2912 |
operand of `delete` may be a null pointer value or a pointer value that
|
| 2913 |
resulted from a previous array *new-expression*.[^22] If not, the
|
| 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]]) of the pointer expression before it is used as the
|
| 2923 |
+
operand of the *delete-expression*. — *end note*]
|
| 2924 |
|
| 2925 |
In the first alternative (*delete object*), if the static type of the
|
| 2926 |
object to be deleted is different from its dynamic type, the static type
|
| 2927 |
shall be a base class of the dynamic type of the object to be deleted
|
| 2928 |
and the static type shall have a virtual destructor or the behavior is
|
|
|
|
| 2959 |
*new-expression* that had storage provided by the extended
|
| 2960 |
*new-expression* has been evaluated, the *delete-expression* shall
|
| 2961 |
call a deallocation function. The value returned from the allocation
|
| 2962 |
call of the extended *new-expression* shall be passed as the first
|
| 2963 |
argument to the deallocation function.
|
| 2964 |
+
- Otherwise, the *delete-expression* will not call a deallocation
|
| 2965 |
+
function.
|
| 2966 |
|
| 2967 |
+
[*Note 3*: The deallocation function is called regardless of whether
|
| 2968 |
+
the destructor for the object or some element of the array throws an
|
| 2969 |
+
exception. — *end note*]
|
|
|
|
| 2970 |
|
| 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]]) and `operator delete[]` for arrays (
|
| 2978 |
[[new.delete.array]]). A C++ program can provide alternative definitions
|
| 2979 |
of these functions ([[replacement.functions]]), and/or class-specific
|
| 2980 |
+
versions ([[class.free]]). — *end note*]
|
| 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 ([[class.free]]). If no class-specific
|
| 2986 |
deallocation function is found, the deallocation function’s name is
|
| 2987 |
looked up in global scope.
|
| 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 exactly one preferred function is
|
| 2995 |
+
found, that function is selected and the selection process terminates.
|
| 2996 |
+
If more than one preferred function is found, all non-preferred
|
| 2997 |
+
functions are eliminated from further consideration.
|
| 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 the second alternative (delete
|
| 3001 |
+
array) only, the operand is a pointer to a class type with a
|
| 3002 |
+
non-trivial destructor or a (possibly multi-dimensional) array
|
| 3003 |
+
thereof, the function with a parameter of type `std::size_t` is
|
| 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 most-derived object in
|
| 3010 |
+
the *delete object* case, or the address of the object suitably adjusted
|
| 3011 |
+
for the array allocation overhead ([[expr.new]]) in the *delete array*
|
| 3012 |
+
case, as its first argument. If a deallocation function with a parameter
|
| 3013 |
+
of type `std::align_val_t` is used, the alignment of the type of the
|
| 3014 |
+
object to be deleted is passed as the corresponding argument. If a
|
| 3015 |
+
deallocation function with a parameter of type `std::size_t` is used,
|
| 3016 |
+
the size of the most-derived type, or of the array plus allocation
|
| 3017 |
+
overhead, respectively, is passed as the corresponding argument. [^23]
|
| 3018 |
+
|
| 3019 |
+
[*Note 5*: If this results in a call to a usual deallocation function,
|
| 3020 |
+
and either the first argument was not the result of a prior call to a
|
| 3021 |
+
usual allocation function or the second argument was not the
|
| 3022 |
+
corresponding argument in said call, the behavior is undefined (
|
| 3023 |
+
[[new.delete.single]], [[new.delete.array]]). — *end note*]
|
| 3024 |
|
| 3025 |
Access and ambiguity control are done for both the deallocation function
|
| 3026 |
and the destructor ([[class.dtor]], [[class.free]]).
|
| 3027 |
|
| 3028 |
### Alignof <a id="expr.alignof">[[expr.alignof]]</a>
|
|
|
|
| 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 Clause [[expr]]. — *end note*]
|
| 3067 |
|
| 3068 |
An explicit type conversion can be expressed using functional notation (
|
| 3069 |
[[expr.type.conv]]), a type conversion operator (`dynamic_cast`,
|
| 3070 |
`static_cast`, `reinterpret_cast`, `const_cast`), or the *cast*
|
| 3071 |
notation.
|
|
|
|
| 3107 |
above, the interpretation that appears first in the list is used, even
|
| 3108 |
if a cast resulting from that interpretation is ill-formed. If a
|
| 3109 |
conversion can be interpreted in more than one way as a `static_cast`
|
| 3110 |
followed by a `const_cast`, the conversion is ill-formed.
|
| 3111 |
|
| 3112 |
+
[*Example 1*:
|
| 3113 |
+
|
| 3114 |
``` cpp
|
| 3115 |
struct A { };
|
| 3116 |
struct I1 : A { };
|
| 3117 |
struct I2 : A { };
|
| 3118 |
struct D : I1, I2 { };
|
| 3119 |
A* foo( D* p ) {
|
| 3120 |
return (A*)( p ); // ill-formed static_cast interpretation
|
| 3121 |
}
|
| 3122 |
```
|
| 3123 |
|
| 3124 |
+
— *end example*]
|
| 3125 |
+
|
| 3126 |
The operand of a cast using the cast notation can be a prvalue of type
|
| 3127 |
“pointer to incomplete class type”. The destination type of a cast using
|
| 3128 |
the cast notation can be “pointer to incomplete class type”. If both the
|
| 3129 |
operand and destination types are class types and one or both are
|
| 3130 |
incomplete, it is unspecified whether the `static_cast` or the
|
| 3131 |
`reinterpret_cast` interpretation is used, even if there is an
|
| 3132 |
+
inheritance relationship between the two classes.
|
| 3133 |
+
|
| 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 |
|
|
|
|
| 3146 |
pm-expression '.*' cast-expression
|
| 3147 |
pm-expression '->*' cast-expression
|
| 3148 |
```
|
| 3149 |
|
| 3150 |
The binary operator `.*` binds its second operand, which shall be of
|
| 3151 |
+
type “pointer to member of `T`” to its first operand, which shall be a
|
| 3152 |
+
glvalue of class `T` or of a class of which `T` is an unambiguous and
|
| 3153 |
+
accessible base class. The result is an object or a function of the type
|
| 3154 |
+
specified by the second operand.
|
| 3155 |
|
| 3156 |
The binary operator `->*` binds its second operand, which shall be of
|
| 3157 |
type “pointer to member of `T`” to its first operand, which shall be of
|
| 3158 |
+
type “pointer to `U`” where `U` is either `T` or a class of which `T` is
|
| 3159 |
+
an unambiguous and accessible base class. The expression `E1->*E2` is
|
| 3160 |
converted into the equivalent form `(*(E1)).*E2`.
|
| 3161 |
|
| 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 *cv-*qualification, and the manner in which the
|
| 3168 |
*cv-*qualifiers of the operands are combined to produce the
|
| 3169 |
*cv-*qualifiers of the result, are the same as the rules for `E1.E2`
|
| 3170 |
+
given in [[expr.ref]].
|
| 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 `const` class object. For example,
|
| 3176 |
|
| 3177 |
``` cpp
|
| 3178 |
struct S {
|
| 3179 |
S() : i(0) { }
|
| 3180 |
mutable int i;
|
|
|
|
| 3185 |
int S::* pm = &S::i; // pm refers to mutable member S::i
|
| 3186 |
cs.*pm = 88; // ill-formed: cs is a const object
|
| 3187 |
}
|
| 3188 |
```
|
| 3189 |
|
| 3190 |
+
— *end note*]
|
| 3191 |
+
|
| 3192 |
If the result of `.*` or `->*` is a function, then that result can be
|
| 3193 |
used only as the operand for the function call operator `()`.
|
| 3194 |
|
| 3195 |
+
[*Example 1*:
|
| 3196 |
+
|
| 3197 |
``` cpp
|
| 3198 |
(ptr_to_obj->*ptr_to_mfct)(10);
|
| 3199 |
```
|
| 3200 |
|
| 3201 |
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 with
|
| 3208 |
+
*ref-qualifier* `&`. In a `.*` expression whose object expression is an
|
| 3209 |
+
lvalue, the program is ill-formed if the second operand is a pointer to
|
| 3210 |
+
member function with *ref-qualifier* `&&`. The result of a `.*`
|
| 3211 |
+
expression whose second operand is a pointer to a data member is an
|
| 3212 |
+
lvalue if the first operand is an lvalue and an xvalue otherwise. The
|
| 3213 |
+
result of a `.*` expression whose second operand is a pointer to a
|
| 3214 |
+
member function is a prvalue. If the second operand is the null member
|
| 3215 |
+
pointer value ([[conv.mem]]), the behavior is undefined.
|
| 3216 |
|
| 3217 |
## Multiplicative operators <a id="expr.mul">[[expr.mul]]</a>
|
| 3218 |
|
| 3219 |
The multiplicative operators `*`, `/`, and `%` group left-to-right.
|
| 3220 |
|
|
|
|
| 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;[^24] if the quotient `a/b`
|
| 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 |
|
|
|
|
| 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 from
|
| 3275 |
a pointer, the result has the type of the pointer operand. If the
|
| 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 |
When two pointers to elements of the same array object are subtracted,
|
| 3284 |
+
the type of the result is an *implementation-defined* signed integral
|
| 3285 |
+
type; this type shall be the same type that is defined as
|
| 3286 |
+
`std::ptrdiff_t` in the `<cstddef>` header ([[support.types]]). If the
|
| 3287 |
+
expressions `P` and `Q` point to, respectively, elements x[i] and x[j]
|
| 3288 |
+
of the same array object `x`, the expression `P - Q` has the value
|
| 3289 |
+
i - j; otherwise, the behavior is undefined.
|
| 3290 |
+
|
| 3291 |
+
[*Note 1*: If the value i - j is not in the range of representable
|
| 3292 |
+
values of type `std::ptrdiff_t`, the behavior is
|
| 3293 |
+
undefined. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ([[conv.qual]]), the behavior is undefined.
|
|
|
|
|
|
|
| 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 |
+
If the value 0 is added to or subtracted from a null pointer value, the
|
| 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 |
|
|
|
|
| 3336 |
`E1` has an unsigned type or if `E1` has a signed type and a
|
| 3337 |
non-negative value, the value of the result is the integral part of the
|
| 3338 |
quotient of $\mathrm{E1}/2^\mathrm{E2}$. If `E1` has a signed type and a
|
| 3339 |
negative value, the resulting value is *implementation-defined*.
|
| 3340 |
|
| 3341 |
+
The expression `E1` is sequenced before the expression `E2`.
|
| 3342 |
+
|
| 3343 |
## Relational operators <a id="expr.rel">[[expr.rel]]</a>
|
| 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 |
shift-expression
|
| 3353 |
relational-expression '<' shift-expression
|
|
|
|
| 3366 |
conversions ([[conv.ptr]]) and qualification conversions (
|
| 3367 |
[[conv.qual]]) are performed to bring them to their composite pointer
|
| 3368 |
type (Clause [[expr]]). After conversions, the operands shall have the
|
| 3369 |
same type.
|
| 3370 |
|
| 3371 |
+
Comparing unequal pointers to objects [^26] is defined as follows:
|
| 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 compares greater.
|
|
|
|
|
|
|
|
|
|
| 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 compares greater provided the two members
|
| 3379 |
have the same access control (Clause [[class.access]]) and provided
|
| 3380 |
their class is not a union.
|
| 3381 |
+
- Otherwise, neither pointer compares greater than the other.
|
| 3382 |
|
| 3383 |
If two operands `p` and `q` compare equal ([[expr.eq]]), `p<=q` and
|
| 3384 |
`p>=q` both yield `true` and `p<q` and `p>q` both yield `false`.
|
| 3385 |
Otherwise, if a pointer `p` compares greater than a pointer `q`, `p>=q`,
|
| 3386 |
`p>q`, `q<=p`, and `q<p` all yield `true` and `p<=q`, `p<q`, `q>=p`, and
|
|
|
|
| 3406 |
and `!=` both yield `true` or `false`, i.e., a result of type `bool`. In
|
| 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]]), function pointer conversions ([[conv.fctptr]]), and
|
| 3412 |
+
qualification conversions ([[conv.qual]]) are performed on both
|
| 3413 |
+
operands to bring them to their composite pointer type (Clause
|
| 3414 |
+
[[expr]]). Comparing pointers is defined as follows:
|
| 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,[^27] the result of the comparison is
|
| 3419 |
+
unspecified.
|
| 3420 |
+
- Otherwise, if the pointers are both null, both point to the same
|
| 3421 |
function, or both represent the same address ([[basic.compound]]),
|
| 3422 |
+
they compare equal.
|
| 3423 |
+
- Otherwise, the pointers compare unequal.
|
| 3424 |
|
| 3425 |
If at least one of the operands is a pointer to member, pointer to
|
| 3426 |
member conversions ([[conv.mem]]) and qualification conversions (
|
| 3427 |
[[conv.qual]]) are performed on both operands to bring them to their
|
| 3428 |
composite pointer type (Clause [[expr]]). Comparing pointers to members
|
|
|
|
| 3435 |
- If either is a pointer to a virtual member function, the result is
|
| 3436 |
unspecified.
|
| 3437 |
- If one refers to a member of class `C1` and the other refers to a
|
| 3438 |
member of a different class `C2`, where neither is a base class of the
|
| 3439 |
other, the result is unspecified.
|
| 3440 |
+
\[*Example 1*:
|
| 3441 |
``` cpp
|
| 3442 |
struct A {};
|
| 3443 |
struct B : A { int x; };
|
| 3444 |
struct C : A { int x; };
|
| 3445 |
|
| 3446 |
int A::*bx = (int(A::*))&B::x;
|
| 3447 |
int A::*cx = (int(A::*))&C::x;
|
| 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]]), they compare equal.
|
| 3455 |
- Otherwise, two pointers to members compare equal if they would refer
|
| 3456 |
to the same member of the same most derived object ([[intro.object]])
|
| 3457 |
or the same subobject if indirection with a hypothetical object of the
|
| 3458 |
associated class type were performed, otherwise they compare unequal.
|
| 3459 |
+
\[*Example 2*:
|
| 3460 |
``` cpp
|
| 3461 |
struct B {
|
| 3462 |
int f();
|
| 3463 |
};
|
| 3464 |
struct L : B { };
|
|
|
|
| 3472 |
int (D::*pdr)() = pr;
|
| 3473 |
bool x = (pdl == pdr); // false
|
| 3474 |
bool y = (pb == pl); // true
|
| 3475 |
```
|
| 3476 |
|
| 3477 |
+
— *end example*]
|
| 3478 |
+
|
| 3479 |
Two operands of type `std::nullptr_t` or one operand of type
|
| 3480 |
`std::nullptr_t` and the other a null pointer constant compare equal.
|
| 3481 |
|
| 3482 |
If two operands compare equal, the result is `true` for the `==`
|
| 3483 |
operator and `false` for the `!=` operator. If two operands compare
|
|
|
|
| 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* ([[expr.throw]]); the result is of
|
| 3589 |
+
the type and value category of the other. The *conditional-expression*
|
| 3590 |
+
is a bit-field if that operand is a bit-field.
|
| 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 *cv* `T` for the remainder of
|
| 3598 |
+
this section, where *cv* is the union of *cv1* and *cv2*.
|
| 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 ([[over.best.ics]]) from each of those operands to the type of
|
| 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*]
|
| 3610 |
+
|
| 3611 |
+
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 ([[dcl.init.ref]]) to an lvalue.
|
| 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 |
+
cv-qualification), or one is a base class of the other, and `T2` is
|
| 3625 |
+
at least as cv-qualified as `T1`, the target type is `T2`,
|
| 3626 |
+
- otherwise, the target type is the type that `E2` would have after
|
| 3627 |
+
applying the lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 3628 |
+
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 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
|
| 3634 |
+
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 section.
|
| 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
|
| 3646 |
and have the same type, the result is of that type and value category
|
| 3647 |
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 |
|
|
|
|
| 3660 |
and function-to-pointer ([[conv.func]]) standard conversions are
|
| 3661 |
performed on the second and third operands. After those conversions, one
|
| 3662 |
of the following shall hold:
|
| 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 bring them to a common
|
| 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 ([[conv.ptr]]), function pointer conversions (
|
| 3672 |
+
[[conv.fctptr]]), and qualification conversions ([[conv.qual]]) are
|
| 3673 |
+
performed to bring them to their composite pointer type (Clause
|
| 3674 |
+
[[expr]]). The result is of the composite pointer type.
|
| 3675 |
- One or both of the second and third operands have pointer to member
|
| 3676 |
type; pointer to member conversions ([[conv.mem]]) and qualification
|
| 3677 |
conversions ([[conv.qual]]) are performed to bring them to their
|
| 3678 |
composite pointer type (Clause [[expr]]). The result is of the
|
| 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 |
+
## Throwing an exception <a id="expr.throw">[[expr.throw]]</a>
|
| 3685 |
+
|
| 3686 |
+
``` bnf
|
| 3687 |
+
throw-expression:
|
| 3688 |
+
'throw' assignment-expressionₒₚₜ
|
| 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]]); the type of the exception object is determined by
|
| 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 ([[except.handle]]). The exception is reactivated with the
|
| 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 |
+
|
| 3706 |
+
Code that must be executed because of an exception, but cannot
|
| 3707 |
+
completely handle the exception itself, can be written like this:
|
| 3708 |
+
|
| 3709 |
+
``` cpp
|
| 3710 |
+
try {
|
| 3711 |
+
// ...
|
| 3712 |
+
} catch (...) { // catch all exceptions
|
| 3713 |
+
// respond (partially) to exception
|
| 3714 |
+
throw; // pass the exception to some other handler
|
| 3715 |
+
}
|
| 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 and return an lvalue referring to the left operand. The result
|
| 3729 |
in all cases is a bit-field if the left operand is a bit-field. In all
|
| 3730 |
cases, the assignment is sequenced after the value computation of the
|
| 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 shall not intervene between the
|
| 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 |
logical-or-expression assignment-operator initializer-clause
|
|
|
|
| 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 |
The behavior of an expression of the form `E1` *op*`=` `E2` is
|
| 3772 |
equivalent to `E1 = E1` *op* `E2` except that `E1` is evaluated only
|
| 3773 |
once. In `+=` and `-=`, `E1` shall either have arithmetic type or be a
|
| 3774 |
pointer to a possibly cv-qualified completely-defined object type. In
|
| 3775 |
all other cases, `E1` shall have arithmetic type.
|
| 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 3*: This restriction applies to the relationship between the
|
| 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
|
| 3788 |
|
| 3789 |
- an assignment to a scalar, in which case the initializer list shall
|
| 3790 |
have at most a single element. The meaning of `x = {v}`, where `T` is
|
|
|
|
| 3793 |
- an assignment to an object of class type, in which case the
|
| 3794 |
initializer list is passed as the argument to the assignment operator
|
| 3795 |
function selected by overload resolution ([[over.ass]],
|
| 3796 |
[[over.match]]).
|
| 3797 |
|
| 3798 |
+
[*Example 1*:
|
| 3799 |
+
|
| 3800 |
``` cpp
|
| 3801 |
complex<double> z;
|
| 3802 |
z = { 1,2 }; // meaning z.operator=({1,2\)}
|
| 3803 |
z += { 1, 2 }; // meaning z.operator+=({1,2\)}
|
| 3804 |
int a, b;
|
| 3805 |
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
|
|
|
|
| 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 (Clause [[expr]]).
|
| 3823 |
+
Every value computation and side effect associated with the left
|
| 3824 |
+
expression is sequenced before every value computation and side effect
|
| 3825 |
+
associated with the right expression. The type and value of the result
|
| 3826 |
+
are the type and value of the right operand; the result is of the same
|
| 3827 |
+
value category as its right operand, and is a bit-field if its right
|
| 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 ([[expr.call]]) and
|
| 3834 |
+
lists of initializers ([[dcl.init]]) — *end example*]
|
| 3835 |
+
|
| 3836 |
+
the comma operator as described in Clause [[expr]] can appear only in
|
| 3837 |
+
parentheses.
|
| 3838 |
+
|
| 3839 |
+
[*Example 2*:
|
| 3840 |
|
| 3841 |
``` cpp
|
| 3842 |
f(a, (t=3, t+2), c);
|
| 3843 |
```
|
| 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 called *constant
|
| 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 |
+
An expression `e` is a *core constant expression* unless the evaluation
|
| 3867 |
+
of `e`, following the rules of the abstract machine (
|
| 3868 |
[[intro.execution]]), would evaluate one of the following expressions:
|
| 3869 |
|
| 3870 |
+
- `this` ([[expr.prim.this]]), except in a constexpr function or a
|
| 3871 |
+
constexpr constructor that is being evaluated as part of `e`;
|
| 3872 |
+
- an invocation of a function other than a constexpr constructor for a
|
| 3873 |
+
literal class, a constexpr function, or an implicit invocation of a
|
| 3874 |
+
trivial destructor ([[class.dtor]]) \[*Note 2*: Overload resolution (
|
| 3875 |
+
[[over.match]]) is applied as usual — *end note*] ;
|
| 3876 |
+
- an invocation of an undefined constexpr function or an undefined
|
| 3877 |
+
constexpr constructor;
|
| 3878 |
+
- an invocation of an instantiated constexpr function or constexpr
|
| 3879 |
+
constructor that fails to satisfy the requirements for a constexpr
|
| 3880 |
+
function or constexpr constructor ([[dcl.constexpr]]);
|
| 3881 |
- an expression that would exceed the implementation-defined limits (see
|
| 3882 |
Annex [[implimits]]);
|
| 3883 |
+
- an operation that would have undefined behavior as specified in
|
| 3884 |
+
Clauses [[intro]] through [[cpp]] of this International Standard
|
| 3885 |
+
\[*Note 3*: including, for example, signed integer overflow (Clause
|
| 3886 |
+
[[expr]]), certain pointer arithmetic ([[expr.add]]), division by
|
| 3887 |
+
zero ([[expr.mul]]), or certain shift operations (
|
| 3888 |
+
[[expr.shift]]) — *end note*] ;
|
| 3889 |
- an lvalue-to-rvalue conversion ([[conv.lval]]) unless it is applied
|
| 3890 |
to
|
| 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 `e`;
|
| 3901 |
+
- an lvalue-to-rvalue conversion ([[conv.lval]]) that is applied to a
|
| 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 `e`;
|
| 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 initialized with a constant expression or
|
| 3915 |
+
- its lifetime began within the evaluation of `e`;
|
|
|
|
| 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; // OK, n is not odr-used and not captured here
|
| 3926 |
+
constexpr int j = *&n; // ill-formed, &n would be an odr-use of n
|
| 3927 |
+
};
|
| 3928 |
+
}
|
| 3929 |
+
```
|
| 3930 |
+
|
| 3931 |
+
— *end example*]
|
| 3932 |
+
\[*Note 4*:
|
| 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]]) of the *id-expression* into an access of
|
| 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 have captures to automatic objects created during constant expression evaluation.
|
| 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 dynamic cast ([[expr.dynamic.cast]]);
|
| 3953 |
- a `reinterpret_cast` ([[expr.reinterpret.cast]]);
|
| 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
|
|
|
|
| 3960 |
polymorphic class type;
|
| 3961 |
- a *new-expression* ([[expr.new]]);
|
| 3962 |
- a *delete-expression* ([[expr.delete]]);
|
| 3963 |
- a relational ([[expr.rel]]) or equality ([[expr.eq]]) operator where
|
| 3964 |
the result is unspecified; or
|
| 3965 |
+
- a *throw-expression* ([[expr.throw]]).
|
| 3966 |
+
|
| 3967 |
+
If `e` satisfies the constraints of a core constant expression, but
|
| 3968 |
+
evaluation of `e` would evaluate an operation that has undefined
|
| 3969 |
+
behavior as specified in Clauses [[library]] through [[thread]] of
|
| 3970 |
+
this International Standard, it is unspecified whether `e` is a core
|
| 3971 |
+
constant expression.
|
| 3972 |
+
|
| 3973 |
+
[*Example 3*:
|
| 3974 |
|
| 3975 |
``` cpp
|
| 3976 |
int x; // not constant
|
| 3977 |
struct A {
|
| 3978 |
constexpr A(bool b) : m(b?42:x) { }
|
| 3979 |
int m;
|
| 3980 |
};
|
| 3981 |
+
constexpr int v = A(true).m; // OK: constructor call initializes m with the value 42
|
| 3982 |
+
|
| 3983 |
+
constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
|
|
|
|
| 3984 |
|
| 3985 |
constexpr int f1(int k) {
|
| 3986 |
+
constexpr int x = k; // error: x is not initialized by a constant expression
|
| 3987 |
+
// because lifetime of k began outside the initializer of x
|
|
|
|
| 3988 |
return x;
|
| 3989 |
}
|
| 3990 |
constexpr int f2(int k) {
|
| 3991 |
int x = k; // OK: not required to be a constant expression
|
| 3992 |
// because x is not constexpr
|
|
|
|
| 3995 |
|
| 3996 |
constexpr int incr(int &n) {
|
| 3997 |
return ++n;
|
| 3998 |
}
|
| 3999 |
constexpr int g(int k) {
|
| 4000 |
+
constexpr int x = incr(k); // error: incr(k) is not a core constant expression
|
| 4001 |
+
// because lifetime of k began outside the expression incr(k)
|
|
|
|
| 4002 |
return x;
|
| 4003 |
}
|
| 4004 |
constexpr int h(int k) {
|
| 4005 |
+
int x = incr(k); // OK: incr(k) is not required to be a core constant expression
|
|
|
|
| 4006 |
return x;
|
| 4007 |
}
|
| 4008 |
constexpr int y = h(1); // OK: initializes y with the value 2
|
| 4009 |
// h(1) is a core constant expression because
|
| 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 5*: Such expressions may be used as bit-field lengths (
|
| 4020 |
+
[[class.bit]]), as enumerator initializers if the underlying type is not
|
| 4021 |
+
fixed ([[dcl.enum]]), and as alignments (
|
| 4022 |
+
[[dcl.align]]). — *end note*]
|
| 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 ([[conv.lval]]),
|
| 4030 |
+
- array-to-pointer conversions ([[conv.array]]),
|
| 4031 |
+
- function-to-pointer conversions ([[conv.func]]),
|
| 4032 |
+
- qualification conversions ([[conv.qual]]),
|
| 4033 |
+
- integral promotions ([[conv.prom]]),
|
| 4034 |
+
- integral conversions ([[conv.integral]]) other than narrowing
|
| 4035 |
+
conversions ([[dcl.init.list]]),
|
| 4036 |
+
- null pointer conversions ([[conv.ptr]]) from `std::nullptr_t`,
|
| 4037 |
+
- null member pointer conversions ([[conv.mem]]) from `std::nullptr_t`,
|
| 4038 |
+
and
|
| 4039 |
+
- function pointer conversions ([[conv.fctptr]]),
|
| 4040 |
+
|
| 4041 |
+
and where the reference binding (if any) binds directly.
|
| 4042 |
+
|
| 4043 |
+
[*Note 6*: Such expressions may be used in `new` expressions (
|
| 4044 |
[[expr.new]]), as case expressions ([[stmt.switch]]), as enumerator
|
| 4045 |
initializers if the underlying type is fixed ([[dcl.enum]]), as array
|
| 4046 |
+
bounds ([[dcl.array]]), and as non-type template arguments (
|
| 4047 |
+
[[temp.arg]]). — *end note*]
|
| 4048 |
+
|
| 4049 |
+
A *contextually converted constant expression of type `bool`* is an
|
| 4050 |
+
expression, contextually converted to `bool` (Clause [[conv]]), where
|
| 4051 |
+
the converted expression is a constant expression and the conversion
|
| 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
|
| 4057 |
+
whose value satisfies the following constraints:
|
| 4058 |
|
| 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 ([[expr.add]]), the address of a function, or a null pointer
|
| 4065 |
+
value, and
|
| 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 is either not a temporary
|
| 4071 |
+
object or is a temporary object whose value satisfies the above
|
| 4072 |
+
constraints, or it is a function.
|
| 4073 |
+
|
| 4074 |
+
[*Note 7*:
|
| 4075 |
|
| 4076 |
Since this International Standard imposes no restrictions on the
|
| 4077 |
accuracy of floating-point operations, it is unspecified whether the
|
| 4078 |
evaluation of a floating-point expression during translation yields the
|
| 4079 |
same result as the evaluation of the same expression (or the same
|
| 4080 |
operations on the same values) during program execution.[^28]
|
| 4081 |
|
| 4082 |
+
[*Example 4*:
|
| 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
|
| 4088 |
return sizeof(array) == size;
|
| 4089 |
}
|
| 4090 |
```
|
| 4091 |
|
| 4092 |
It is unspecified whether the value of `f()` will be `true` or `false`.
|
| 4093 |
|
| 4094 |
+
— *end example*]
|
| 4095 |
+
|
| 4096 |
+
— *end note*]
|
| 4097 |
+
|
| 4098 |
If an expression of literal class type is used in a context where an
|
| 4099 |
integral constant expression is required, then that expression is
|
| 4100 |
contextually implicitly converted (Clause [[conv]]) to an integral or
|
| 4101 |
unscoped enumeration type and the selected conversion function shall be
|
| 4102 |
`constexpr`.
|
| 4103 |
|
| 4104 |
+
[*Example 5*:
|
| 4105 |
+
|
| 4106 |
``` cpp
|
| 4107 |
struct A {
|
| 4108 |
constexpr A(int i) : val(i) { }
|
| 4109 |
constexpr operator int() const { return val; }
|
| 4110 |
constexpr operator long() const { return 43; }
|
|
|
|
| 4115 |
constexpr A a = 42;
|
| 4116 |
X<a> x; // OK: unique conversion to int
|
| 4117 |
int ary[a]; // error: ambiguous conversion
|
| 4118 |
```
|
| 4119 |
|
| 4120 |
+
— *end example*]
|
| 4121 |
+
|
| 4122 |
<!-- Link reference definitions -->
|
| 4123 |
[bad.alloc]: language.md#bad.alloc
|
| 4124 |
[bad.cast]: language.md#bad.cast
|
| 4125 |
[bad.typeid]: language.md#bad.typeid
|
| 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]: basic.md#basic.lval
|
|
|
|
| 4155 |
[class.copy]: special.md#class.copy
|
| 4156 |
[class.ctor]: special.md#class.ctor
|
| 4157 |
[class.derived]: class.md#class.derived
|
| 4158 |
[class.dtor]: special.md#class.dtor
|
| 4159 |
[class.free]: special.md#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.name]: class.md#class.name
|
| 4167 |
[class.qual]: basic.md#class.qual
|
| 4168 |
[class.static]: class.md#class.static
|
| 4169 |
[class.temporary]: special.md#class.temporary
|
| 4170 |
[class.this]: class.md#class.this
|
| 4171 |
+
[class.union]: class.md#class.union
|
| 4172 |
[class.virtual]: class.md#class.virtual
|
| 4173 |
[conv]: conv.md#conv
|
| 4174 |
[conv.array]: conv.md#conv.array
|
| 4175 |
[conv.bool]: conv.md#conv.bool
|
| 4176 |
+
[conv.fctptr]: conv.md#conv.fctptr
|
| 4177 |
[conv.fpint]: conv.md#conv.fpint
|
| 4178 |
[conv.fpprom]: conv.md#conv.fpprom
|
| 4179 |
[conv.func]: conv.md#conv.func
|
| 4180 |
[conv.integral]: conv.md#conv.integral
|
| 4181 |
[conv.lval]: conv.md#conv.lval
|
| 4182 |
[conv.mem]: conv.md#conv.mem
|
| 4183 |
[conv.prom]: conv.md#conv.prom
|
| 4184 |
[conv.ptr]: conv.md#conv.ptr
|
| 4185 |
[conv.qual]: conv.md#conv.qual
|
| 4186 |
+
[conv.rval]: conv.md#conv.rval
|
| 4187 |
+
[cpp]: cpp.md#cpp
|
| 4188 |
+
[cstddef.syn]: language.md#cstddef.syn
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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.pseudo]: #expr.pseudo
|
| 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]: intro.md#intro.execution
|
| 4269 |
[intro.memory]: intro.md#intro.memory
|
| 4270 |
[intro.object]: intro.md#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]: language.md#new.badlength
|
| 4276 |
[new.delete.array]: language.md#new.delete.array
|
| 4277 |
+
[new.delete.placement]: language.md#new.delete.placement
|
| 4278 |
[new.delete.single]: language.md#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
|
| 4284 |
[over.ics.user]: over.md#over.ics.user
|
| 4285 |
[over.literal]: over.md#over.literal
|
| 4286 |
[over.match]: over.md#over.match
|
| 4287 |
+
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 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]: language.md#support.runtime
|
| 4296 |
[support.types]: language.md#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]: language.md#type.info
|
| 4307 |
|
| 4308 |
[^1]: The precedence of operators is not directly specified, but it can
|
| 4309 |
be derived from the syntax.
|
| 4310 |
|
|
|
|
| 4375 |
expression is enclosed in parentheses.
|
| 4376 |
|
| 4377 |
[^21]: This implies that an object cannot be deleted using a pointer of
|
| 4378 |
type `void*` because `void` is not an object type.
|
| 4379 |
|
| 4380 |
+
[^22]: For nonzero-length arrays, this is the same as a pointer to the
|
| 4381 |
first element of the array created by that *new-expression*.
|
| 4382 |
Zero-length arrays do not have a first element.
|
| 4383 |
|
| 4384 |
[^23]: If the static type of the object to be deleted is complete and is
|
| 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 |
+
[^24]: This is often called truncation towards zero.
|
|
|
|
| 4390 |
|
| 4391 |
+
[^25]: An object that is not an array element is considered to belong to
|
| 4392 |
+
a single-element array for this purpose; see [[expr.unary.op]]. A
|
| 4393 |
+
pointer past the last element of an array `x` of n elements is
|
| 4394 |
+
considered to be equivalent to a pointer to a hypothetical element
|
| 4395 |
+
x[n] for this purpose; see [[basic.compound]].
|
| 4396 |
|
| 4397 |
+
[^26]: An object that is not an array element is considered to belong to
|
| 4398 |
+
a single-element array for this purpose; see [[expr.unary.op]]. A
|
| 4399 |
+
pointer past the last element of an array `x` of n elements is
|
| 4400 |
+
considered to be equivalent to a pointer to a hypothetical element
|
| 4401 |
+
x[n] for this purpose; see [[basic.compound]].
|
|
|
|
|
|
|
|
|
|
| 4402 |
|
| 4403 |
+
[^27]: An object that is not an array element is considered to belong to
|
| 4404 |
+
a single-element array for this purpose; see [[expr.unary.op]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|