- tmp/tmpj_pcwzvt/{from.md → to.md} +327 -264
tmp/tmpj_pcwzvt/{from.md → to.md}
RENAMED
|
@@ -11,12 +11,14 @@ postfix-expression:
|
|
| 11 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 12 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 13 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 14 |
simple-type-specifier braced-init-list
|
| 15 |
typename-specifier braced-init-list
|
| 16 |
-
postfix-expression '.'
|
| 17 |
-
postfix-expression '
|
|
|
|
|
|
|
| 18 |
postfix-expression '++'
|
| 19 |
postfix-expression '--'
|
| 20 |
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 21 |
static_cast '<' type-id '>' '(' expression ')'
|
| 22 |
reinterpret_cast '<' type-id '>' '(' expression ')'
|
|
@@ -39,23 +41,24 @@ replacing a `>>` token by two consecutive `>` tokens
|
|
| 39 |
|
| 40 |
A *subscript expression* is a postfix expression followed by square
|
| 41 |
brackets containing a possibly empty, comma-separated list of
|
| 42 |
*initializer-clause*s that constitute the arguments to the subscript
|
| 43 |
operator. The *postfix-expression* and the initialization of the object
|
| 44 |
-
parameter of any applicable subscript operator function
|
| 45 |
-
before each expression in the
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
With the built-in subscript operator, an *expression-list* shall be
|
| 52 |
present, consisting of a single *assignment-expression*. One of the
|
| 53 |
expressions shall be a glvalue of type “array of `T`” or a prvalue of
|
| 54 |
type “pointer to `T`” and the other shall be a prvalue of unscoped
|
| 55 |
enumeration or integral type. The result is of type “`T`”. The type
|
| 56 |
-
“`T`” shall be a completely-defined object type.[^
|
| 57 |
|
| 58 |
The expression `E1[E2]` is identical (by definition) to `*((E1)+(E2))`,
|
| 59 |
except that in the case of an array operand, the result is an lvalue if
|
| 60 |
that operand is an lvalue and an xvalue otherwise.
|
| 61 |
|
|
@@ -68,20 +71,20 @@ of array types. — *end note*]
|
|
| 68 |
|
| 69 |
A function call is a postfix expression followed by parentheses
|
| 70 |
containing a possibly empty, comma-separated list of
|
| 71 |
*initializer-clause*s which constitute the arguments to the function.
|
| 72 |
|
| 73 |
-
[*Note 1*: If the postfix expression is a function
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
The postfix expression shall have function type or function pointer
|
| 78 |
type. For a call to a non-member function or to a static member
|
| 79 |
-
function, the postfix expression shall
|
| 80 |
to a function (in which case the function-to-pointer standard conversion
|
| 81 |
-
[[conv.func]] is suppressed on the postfix expression), or
|
| 82 |
-
pointer type.
|
| 83 |
|
| 84 |
If the selected function is non-virtual, or if the *id-expression* in
|
| 85 |
the class member access expression is a *qualified-id*, that function is
|
| 86 |
called. Otherwise, its final overrider [[class.virtual]] in the dynamic
|
| 87 |
type of the object expression is called; such a call is referred to as a
|
|
@@ -90,14 +93,13 @@ type of the object expression is called; such a call is referred to as a
|
|
| 90 |
[*Note 2*: The dynamic type is the type of the object referred to by
|
| 91 |
the current value of the object expression. [[class.cdtor]] describes
|
| 92 |
the behavior of virtual function calls when the object expression refers
|
| 93 |
to an object under construction or destruction. — *end note*]
|
| 94 |
|
| 95 |
-
[*Note 3*: If a function
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
call. — *end note*]
|
| 99 |
|
| 100 |
If the *postfix-expression* names a destructor or pseudo-destructor
|
| 101 |
[[expr.prim.id.dtor]], the type of the function call expression is
|
| 102 |
`void`; otherwise, the type of the function call expression is the
|
| 103 |
return type of the statically chosen function (i.e., ignoring the
|
|
@@ -106,41 +108,44 @@ different. If the *postfix-expression* names a pseudo-destructor (in
|
|
| 106 |
which case the *postfix-expression* is a possibly-parenthesized class
|
| 107 |
member access), the function call destroys the object of scalar type
|
| 108 |
denoted by the object expression of the class member access
|
| 109 |
[[expr.ref]], [[basic.life]].
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
-
[*Note 4*:
|
| 118 |
-
potentially-throwing function, but the called function has a
|
| 119 |
non-throwing exception specification, and the function types are
|
| 120 |
otherwise the same. — *end note*]
|
| 121 |
|
| 122 |
When a function is called, each parameter [[dcl.fct]] is initialized
|
| 123 |
-
[[dcl.init]], [[class.copy.ctor]] with its corresponding argument
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
[*Example 1*:
|
| 131 |
|
| 132 |
``` cpp
|
| 133 |
template<typename ...T> int f(int n = 0, T ...t);
|
| 134 |
int x = f<int>(); // error: no argument for second function parameter
|
| 135 |
```
|
| 136 |
|
| 137 |
— *end example*]
|
| 138 |
|
| 139 |
-
If the function is an implicit object member function, the
|
| 140 |
-
|
| 141 |
-
|
|
|
|
| 142 |
conversion [[expr.cast]].
|
| 143 |
|
| 144 |
[*Note 5*: There is no access or ambiguity checking on this conversion;
|
| 145 |
the access checking and disambiguation are done as part of the (possibly
|
| 146 |
implicit) class member access operator. See [[class.member.lookup]],
|
|
@@ -151,28 +156,37 @@ class type that is either incomplete or abstract.
|
|
| 151 |
|
| 152 |
[*Note 6*: This still allows a parameter to be a pointer or reference
|
| 153 |
to such a type. However, it prevents a passed-by-value parameter to have
|
| 154 |
an incomplete or abstract class type. — *end note*]
|
| 155 |
|
| 156 |
-
It is *implementation-defined* whether
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
[*Example 2*: The access of the constructor,
|
| 162 |
-
destructor is checked at the point of call
|
| 163 |
-
constructor or destructor for a function parameter throws an
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
handler that can handle the exception, this handler is not
|
| 167 |
considered. — *end example*]
|
| 168 |
|
| 169 |
The *postfix-expression* is sequenced before each *expression* in the
|
| 170 |
*expression-list* and any default argument. The initialization of a
|
| 171 |
-
parameter,
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
[*Note 7*: All side effects of argument evaluations are sequenced
|
| 176 |
before the function is entered (see
|
| 177 |
[[intro.execution]]). — *end note*]
|
| 178 |
|
|
@@ -216,17 +230,26 @@ control out of the called function (if any), except in a virtual
|
|
| 216 |
function call if the return type of the final overrider is different
|
| 217 |
from the return type of the statically chosen function, the value
|
| 218 |
returned from the final overrider is converted to the return type of the
|
| 219 |
statically chosen function.
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
[*Note 9*: A function can change the values of its non-const
|
| 222 |
parameters, but these changes cannot affect the values of the arguments
|
| 223 |
except where a parameter is of a reference type [[dcl.ref]]; if the
|
| 224 |
-
reference is to a const-qualified type, `const_cast`
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
[[dcl.type]], [[lex.literal]], [[lex.string]], [[dcl.array]], [[class.temporary]].
|
| 229 |
In addition, it is possible to modify the values of non-constant objects
|
| 230 |
through pointer parameters. — *end note*]
|
| 231 |
|
| 232 |
A function can be declared to accept fewer arguments (by declaring
|
|
@@ -252,15 +275,16 @@ The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
|
| 252 |
function-to-pointer [[conv.func]] standard conversions are performed on
|
| 253 |
the argument expression. An argument that has type cv `std::nullptr_t`
|
| 254 |
is converted to type `void*` [[conv.ptr]]. After these conversions, if
|
| 255 |
the argument does not have arithmetic, enumeration, pointer,
|
| 256 |
pointer-to-member, or class type, the program is ill-formed. Passing a
|
| 257 |
-
potentially-evaluated argument of a scoped enumeration type
|
| 258 |
-
class type [[class]] having an eligible non-trivial copy
|
| 259 |
-
|
| 260 |
-
[[
|
| 261 |
-
|
|
|
|
| 262 |
enumeration type that is subject to the integral promotions
|
| 263 |
[[conv.prom]], or a floating-point type that is subject to the
|
| 264 |
floating-point promotion [[conv.fpprom]], the value of the argument is
|
| 265 |
converted to the promoted type before the call. These promotions are
|
| 266 |
referred to as the *default argument promotions*.
|
|
@@ -268,11 +292,13 @@ referred to as the *default argument promotions*.
|
|
| 268 |
Recursive calls are permitted, except to the `main` function
|
| 269 |
[[basic.start.main]].
|
| 270 |
|
| 271 |
A function call is an lvalue if the result type is an lvalue reference
|
| 272 |
type or an rvalue reference to function type, an xvalue if the result
|
| 273 |
-
type is an rvalue reference to object type, and a prvalue otherwise.
|
|
|
|
|
|
|
| 274 |
|
| 275 |
#### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 276 |
|
| 277 |
A *simple-type-specifier* [[dcl.type.simple]] or *typename-specifier*
|
| 278 |
[[temp.res]] followed by a parenthesized optional *expression-list* or
|
|
@@ -281,11 +307,28 @@ specified type given the initializer. If the type is a placeholder for a
|
|
| 281 |
deduced class type, it is replaced by the return type of the function
|
| 282 |
selected by overload resolution for class template deduction
|
| 283 |
[[over.match.class.deduct]] for the remainder of this subclause.
|
| 284 |
Otherwise, if the type contains a placeholder type, it is replaced by
|
| 285 |
the type determined by placeholder type deduction
|
| 286 |
-
[[dcl.type.auto.deduct]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
[*Example 1*:
|
| 289 |
|
| 290 |
``` cpp
|
| 291 |
struct A {};
|
|
@@ -299,44 +342,41 @@ void h() {
|
|
| 299 |
}
|
| 300 |
```
|
| 301 |
|
| 302 |
— *end example*]
|
| 303 |
|
| 304 |
-
If the initializer is a parenthesized single expression, the type
|
| 305 |
-
conversion expression is equivalent to the corresponding cast expression
|
| 306 |
-
[[expr.cast]]. Otherwise, if the type is cv `void` and the initializer
|
| 307 |
-
is `()` or `{}` (after pack expansion, if any), the expression is a
|
| 308 |
-
prvalue of type `void` that performs no initialization. Otherwise, the
|
| 309 |
-
expression is a prvalue of the specified type whose result object is
|
| 310 |
-
direct-initialized [[dcl.init]] with the initializer. If the initializer
|
| 311 |
-
is a parenthesized optional *expression-list*, the specified type shall
|
| 312 |
-
not be an array type.
|
| 313 |
-
|
| 314 |
#### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 315 |
|
| 316 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 317 |
followed by the keyword `template`, and then followed by an
|
| 318 |
-
*id-expression*
|
| 319 |
-
the dot or arrow is evaluated;[^12]
|
| 320 |
|
| 321 |
-
the
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
-
|
| 330 |
-
the second option (arrow) the first expression shall be a prvalue having
|
| 331 |
-
pointer type. The expression `E1->E2` is converted to the equivalent
|
| 332 |
-
form `(*(E1)).E2`; the remainder of [[expr.ref]] will address only the
|
| 333 |
-
first option (dot).[^13]
|
| 334 |
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
cv-qualifications) and `E1.E2` is a prvalue of type “function of ()
|
| 339 |
returning `void`”.
|
| 340 |
|
| 341 |
[*Note 2*: This value can only be used for a notional function call
|
| 342 |
[[expr.prim.id.dtor]]. — *end note*]
|
|
@@ -349,68 +389,105 @@ definition of that class.
|
|
| 349 |
when the class is complete [[class.member.lookup]]. — *end note*]
|
| 350 |
|
| 351 |
[*Note 4*: [[basic.lookup.qual]] describes how names are looked up
|
| 352 |
after the `.` and `->` operators. — *end note*]
|
| 353 |
|
| 354 |
-
If `E2` is a
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
and *vq* represents either `volatile` or the absence of `volatile`. *cv*
|
| 358 |
-
represents an arbitrary set of cv-qualifiers, as defined in
|
| 359 |
-
[[basic.type.qualifier]].
|
| 360 |
|
| 361 |
-
If `E2`
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
`
|
| 365 |
-
|
| 366 |
-
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
- If `E2` refers to a static member function, `E1.E2` is an lvalue.
|
| 388 |
- Otherwise (when `E2` refers to a non-static member function),
|
| 389 |
-
`E1.E2` is a prvalue.
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
-
|
| 395 |
-
expression `E1.E2` is a prvalue of type `T` whose value is
|
| 396 |
-
of the enumerator.
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
`E2`.
|
| 402 |
-
|
| 403 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
ambiguous base of the class type of the object expression; see
|
| 405 |
[[class.access.base]]. — *end note*]
|
| 406 |
|
| 407 |
-
If `E2`
|
| 408 |
-
|
| 409 |
-
undefined.
|
| 410 |
|
| 411 |
-
[*Example
|
| 412 |
|
| 413 |
``` cpp
|
| 414 |
struct A { int i; };
|
| 415 |
struct B { int j; };
|
| 416 |
struct D : A, B {};
|
|
@@ -423,33 +500,32 @@ void f() {
|
|
| 423 |
|
| 424 |
— *end example*]
|
| 425 |
|
| 426 |
#### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 427 |
|
| 428 |
-
The value of a postfix `++` expression is the value
|
|
|
|
| 429 |
|
| 430 |
[*Note 1*: The value obtained is a copy of the original
|
| 431 |
value. — *end note*]
|
| 432 |
|
| 433 |
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 434 |
be an arithmetic type other than cv `bool`, or a pointer to a complete
|
| 435 |
object type. An operand with volatile-qualified type is deprecated; see
|
| 436 |
[[depr.volatile.type]]. The value of the operand object is modified
|
| 437 |
-
[[defns.access]]
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
|
|
|
| 441 |
|
| 442 |
[*Note 2*: Therefore, a function call cannot intervene between the
|
| 443 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 444 |
single postfix `++` operator. — *end note*]
|
| 445 |
|
| 446 |
The result is a prvalue. The type of the result is the cv-unqualified
|
| 447 |
-
version of the type of the operand.
|
| 448 |
-
cannot represent the incremented value, the resulting value of the
|
| 449 |
-
bit-field is *implementation-defined*. See also [[expr.add]] and
|
| 450 |
-
[[expr.ass]].
|
| 451 |
|
| 452 |
The operand of postfix `--` is decremented analogously to the postfix
|
| 453 |
`++` operator.
|
| 454 |
|
| 455 |
[*Note 3*: For prefix increment and decrement, see
|
|
@@ -477,11 +553,11 @@ If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
|
| 477 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 478 |
unique `B` subobject of the `D` object pointed to by `v`, or a null
|
| 479 |
pointer value if `v` is a null pointer value. Similarly, if `T` is
|
| 480 |
“reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B` is a
|
| 481 |
base class of `D`, the result is the unique `B` subobject of the `D`
|
| 482 |
-
object referred to by `v`.[^
|
| 483 |
|
| 484 |
In both the pointer and reference cases, the program is ill-formed if
|
| 485 |
`B` is an inaccessible or ambiguous base class of `D`.
|
| 486 |
|
| 487 |
[*Example 1*:
|
|
@@ -499,10 +575,18 @@ void foo(D* dp) {
|
|
| 499 |
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic type
|
| 500 |
[[class.virtual]].
|
| 501 |
|
| 502 |
If `v` is a null pointer value, the result is a null pointer value.
|
| 503 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
If `T` is “pointer to cv `void`”, then the result is a pointer to the
|
| 505 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 506 |
applied to see if the object pointed or referred to by `v` can be
|
| 507 |
converted to the type pointed or referred to by `T`.
|
| 508 |
|
|
@@ -510,11 +594,11 @@ Let `C` be the class type to which `T` points or refers. The runtime
|
|
| 510 |
check logically executes as follows:
|
| 511 |
|
| 512 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 513 |
points (refers) to a public base class subobject of a `C` object, and
|
| 514 |
if only one object of type `C` is derived from the subobject pointed
|
| 515 |
-
(referred) to by `v` the result points (refers) to that `C` object.
|
| 516 |
- Otherwise, if `v` points (refers) to a public base class subobject of
|
| 517 |
the most derived object, and the type of the most derived object has a
|
| 518 |
base class, of type `C`, that is unambiguous and public, the result
|
| 519 |
points (refers) to the `C` subobject of the most derived object.
|
| 520 |
- Otherwise, the runtime check *fails*.
|
|
@@ -560,31 +644,35 @@ destruction. — *end note*]
|
|
| 560 |
|
| 561 |
#### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 562 |
|
| 563 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 564 |
`std::type_info` [[type.info]] and dynamic type `const` `std::type_info`
|
| 565 |
-
or `const`
|
| 566 |
publicly derived from `std::type_info` which preserves the behavior
|
| 567 |
-
described in [[type.info]].[^
|
| 568 |
|
| 569 |
The lifetime of the object referred to by the lvalue extends to the end
|
| 570 |
of the program. Whether or not the destructor is called for the
|
| 571 |
`std::type_info` object at the end of the program is unspecified.
|
| 572 |
|
| 573 |
If the type of the *expression* or *type-id* operand is a (possibly
|
| 574 |
cv-qualified) class type or a reference to (possibly cv-qualified) class
|
| 575 |
type, that class shall be completely defined.
|
| 576 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
When `typeid` is applied to a glvalue whose type is a polymorphic class
|
| 578 |
type [[class.virtual]], the result refers to a `std::type_info` object
|
| 579 |
representing the type of the most derived object [[intro.object]] (that
|
| 580 |
-
is, the dynamic type) to which the glvalue refers.
|
| 581 |
-
obtained by applying the unary `*` operator to a pointer[^16]
|
| 582 |
-
|
| 583 |
-
and the pointer is a null pointer value [[basic.compound]], the `typeid`
|
| 584 |
-
expression throws an exception [[except.throw]] of a type that would
|
| 585 |
-
match a handler of type `std::bad_typeid` exception [[bad.typeid]].
|
| 586 |
|
| 587 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 588 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 589 |
representing the static type of the expression. Lvalue-to-rvalue
|
| 590 |
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
|
@@ -597,11 +685,11 @@ When `typeid` is applied to a *type-id*, the result refers to a
|
|
| 597 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 598 |
type of the *type-id* is a reference to a possibly cv-qualified type,
|
| 599 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 600 |
object representing the cv-unqualified referenced type.
|
| 601 |
|
| 602 |
-
[*Note
|
| 603 |
*cv-qualifier-seq* or a *ref-qualifier* [[dcl.fct]]. — *end note*]
|
| 604 |
|
| 605 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
| 606 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 607 |
representing the cv-unqualified type.
|
|
@@ -624,24 +712,23 @@ typeid(D) == typeid(const D&); // yields true
|
|
| 624 |
The type `std::type_info` [[type.info]] is not predefined; if a standard
|
| 625 |
library declaration [[typeinfo.syn]], [[std.modules]] of
|
| 626 |
`std::type_info` does not precede [[basic.lookup.general]] a `typeid`
|
| 627 |
expression, the program is ill-formed.
|
| 628 |
|
| 629 |
-
[*Note
|
| 630 |
applied to an object under construction or destruction. — *end note*]
|
| 631 |
|
| 632 |
#### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
| 633 |
|
| 634 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 635 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 636 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 637 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 638 |
-
otherwise, the result is a prvalue.
|
| 639 |
-
cast away constness [[expr.const.cast]].
|
| 640 |
|
| 641 |
An lvalue of type “*cv1* `B`”, where `B` is a class type, can be cast to
|
| 642 |
-
type “reference to *cv2* `D`”, where `D` is a class derived
|
| 643 |
[[class.derived]] from `B`, if *cv2* is the same cv-qualification as, or
|
| 644 |
greater cv-qualification than, *cv1*. If `B` is a virtual base class of
|
| 645 |
`D` or a base class of a virtual base class of `D`, or if no valid
|
| 646 |
standard conversion from “pointer to `D`” to “pointer to `B`” exists
|
| 647 |
[[conv.ptr]], the program is ill-formed. An xvalue of type “*cv1* `B`”
|
|
@@ -672,14 +759,25 @@ class subobject thereof; otherwise, the lvalue-to-rvalue conversion
|
|
| 672 |
used as the operand of the `static_cast` for the remainder of this
|
| 673 |
subclause. If `T2` is an inaccessible [[class.access]] or ambiguous
|
| 674 |
[[class.member.lookup]] base class of `T1`, a program that necessitates
|
| 675 |
such a cast is ill-formed.
|
| 676 |
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
function [[over.match.viable]], or if `T` is an aggregate type
|
| 682 |
[[dcl.init.aggr]] having a first element `x` and there is an implicit
|
| 683 |
conversion sequence from E to the type of `x`. If `T` is a reference
|
| 684 |
type, the effect is the same as performing the declaration and
|
| 685 |
initialization
|
|
@@ -690,59 +788,23 @@ T t(E);
|
|
| 690 |
|
| 691 |
for some invented temporary variable `t` [[dcl.init]] and then using the
|
| 692 |
temporary variable as the result of the conversion. Otherwise, the
|
| 693 |
result object is direct-initialized from E.
|
| 694 |
|
| 695 |
-
[*Note
|
| 696 |
expression of class type to an inaccessible or ambiguous base
|
| 697 |
class. — *end note*]
|
| 698 |
|
| 699 |
-
[*Note
|
| 700 |
direct-initialization defines the type of the expression as
|
| 701 |
`U[1]`. — *end note*]
|
| 702 |
|
| 703 |
-
Otherwise, the
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
case the operand is a discarded-value expression [[expr.prop]].
|
| 709 |
-
|
| 710 |
-
[*Note 3*: Such a `static_cast` has no result as it is a prvalue of
|
| 711 |
-
type `void`; see [[basic.lval]]. — *end note*]
|
| 712 |
-
|
| 713 |
-
[*Note 4*: However, if the value is in a temporary object
|
| 714 |
-
[[class.temporary]], the destructor for that object is not executed
|
| 715 |
-
until the usual time, and the value of the object is preserved for the
|
| 716 |
-
purpose of executing the destructor. — *end note*]
|
| 717 |
-
|
| 718 |
-
The inverse of any standard conversion sequence [[conv]] not containing
|
| 719 |
-
an lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]],
|
| 720 |
-
function-to-pointer [[conv.func]], null pointer [[conv.ptr]], null
|
| 721 |
-
member pointer [[conv.mem]], boolean [[conv.bool]], or function pointer
|
| 722 |
-
[[conv.fctptr]] conversion, can be performed explicitly using
|
| 723 |
-
`static_cast`. A program is ill-formed if it uses `static_cast` to
|
| 724 |
-
perform the inverse of an ill-formed standard conversion sequence.
|
| 725 |
-
|
| 726 |
-
[*Example 2*:
|
| 727 |
-
|
| 728 |
-
``` cpp
|
| 729 |
-
struct B { };
|
| 730 |
-
struct D : private B { };
|
| 731 |
-
void f() {
|
| 732 |
-
static_cast<D*>((B*)0); // error: B is a private base of D
|
| 733 |
-
static_cast<int B::*>((int D::*)0); // error: B is a private base of D
|
| 734 |
-
}
|
| 735 |
-
```
|
| 736 |
-
|
| 737 |
-
— *end example*]
|
| 738 |
-
|
| 739 |
-
The lvalue-to-rvalue [[conv.lval]], array-to-pointer [[conv.array]], and
|
| 740 |
-
function-to-pointer [[conv.func]] conversions are applied to the
|
| 741 |
-
operand. Such a `static_cast` is subject to the restriction that the
|
| 742 |
-
explicit conversion does not cast away constness [[expr.const.cast]],
|
| 743 |
-
and the following additional rules for specific cases:
|
| 744 |
|
| 745 |
A value of a scoped enumeration type [[dcl.enum]] can be explicitly
|
| 746 |
converted to an integral type; the result is the same as that of
|
| 747 |
converting to the enumeration’s underlying type and then to the
|
| 748 |
destination type. A value of a scoped enumeration type can also be
|
|
@@ -795,14 +857,13 @@ pointer-to-member-function types) are never cv-qualified
|
|
| 795 |
|
| 796 |
If no valid standard conversion from “pointer to member of `B` of type
|
| 797 |
`T`” to “pointer to member of `D` of type `T`” exists [[conv.mem]], the
|
| 798 |
program is ill-formed. The null member pointer value [[conv.mem]] is
|
| 799 |
converted to the null member pointer value of the destination type. If
|
| 800 |
-
class `B` contains the original member, or is a base
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
undefined.
|
| 804 |
|
| 805 |
[*Note 6*: Although class `B` need not contain the original member, the
|
| 806 |
dynamic type of the object with which indirection through the pointer to
|
| 807 |
member is performed must contain the original member; see
|
| 808 |
[[expr.mptr.oper]]. — *end note*]
|
|
@@ -810,17 +871,18 @@ member is performed must contain the original member; see
|
|
| 810 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
| 811 |
prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
|
| 812 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 813 |
*cv1*. If the original pointer value represents the address `A` of a
|
| 814 |
byte in memory and `A` does not satisfy the alignment requirement of
|
| 815 |
-
`T`, then the resulting pointer value is unspecified.
|
| 816 |
-
original pointer value points to an object *a*, and
|
| 817 |
-
*b* of type similar to `T` that is
|
| 818 |
-
[[basic.compound]] with *a*, the result is a
|
| 819 |
-
the pointer value is unchanged by the
|
|
|
|
| 820 |
|
| 821 |
-
[*Example
|
| 822 |
|
| 823 |
``` cpp
|
| 824 |
T* p1 = new T;
|
| 825 |
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
|
| 826 |
bool b = p1 == p2; // b will have the value true.
|
|
@@ -865,12 +927,12 @@ the conversion has the same meaning and validity as a conversion of
|
|
| 865 |
any type to the type `std::nullptr_t`. — *end note*]
|
| 866 |
|
| 867 |
A value of integral type or enumeration type can be explicitly converted
|
| 868 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 869 |
any such exists on the implementation) and back to the same pointer type
|
| 870 |
-
will have its original value; mappings between
|
| 871 |
-
otherwise *implementation-defined*.
|
| 872 |
|
| 873 |
A function pointer can be explicitly converted to a function pointer of
|
| 874 |
a different type.
|
| 875 |
|
| 876 |
[*Note 4*: The effect of calling a function through a pointer to a
|
|
@@ -880,21 +942,18 @@ definition of the function is undefined [[expr.call]]. — *end note*]
|
|
| 880 |
Except that converting a prvalue of type “pointer to `T1`” to the type
|
| 881 |
“pointer to `T2`” (where `T1` and `T2` are function types) and back to
|
| 882 |
its original type yields the original pointer value, the result of such
|
| 883 |
a pointer conversion is unspecified.
|
| 884 |
|
| 885 |
-
[*Note 5*: See also [[conv.ptr]] for more details of pointer
|
| 886 |
-
conversions. — *end note*]
|
| 887 |
-
|
| 888 |
An object pointer can be explicitly converted to an object pointer of a
|
| 889 |
-
different type.[^
|
| 890 |
|
| 891 |
When a prvalue `v` of object pointer type is converted to the object
|
| 892 |
pointer type “pointer to cv `T`”, the result is
|
| 893 |
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 894 |
|
| 895 |
-
[*Note
|
| 896 |
to an object of type `T1` to the type “pointer to `T2`” (where `T2` is
|
| 897 |
an object type and the alignment requirements of `T2` are no stricter
|
| 898 |
than those of `T1`) and back to its original type yields the original
|
| 899 |
pointer value. — *end note*]
|
| 900 |
|
|
@@ -906,19 +965,19 @@ other type and back, possibly with different cv-qualification, shall
|
|
| 906 |
yield the original pointer value.
|
| 907 |
|
| 908 |
The null pointer value [[basic.compound]] is converted to the null
|
| 909 |
pointer value of the destination type.
|
| 910 |
|
| 911 |
-
[*Note
|
| 912 |
converted to a pointer type, and a null pointer constant of integral
|
| 913 |
type is not necessarily converted to a null pointer
|
| 914 |
value. — *end note*]
|
| 915 |
|
| 916 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 917 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 918 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 919 |
-
object types.[^
|
| 920 |
|
| 921 |
The null member pointer value [[conv.mem]] is converted to the null
|
| 922 |
member pointer value of the destination type. The result of this
|
| 923 |
conversion is unspecified, except in the following cases:
|
| 924 |
|
|
@@ -929,79 +988,83 @@ conversion is unspecified, except in the following cases:
|
|
| 929 |
`T1`” to the type “pointer to data member of `Y` of type `T2`” (where
|
| 930 |
the alignment requirements of `T2` are no stricter than those of `T1`)
|
| 931 |
and back to its original type yields the original pointer-to-member
|
| 932 |
value.
|
| 933 |
|
| 934 |
-
|
| 935 |
-
type “reference to `T2`” if an expression of type
|
| 936 |
-
be explicitly converted to the type “pointer to
|
| 937 |
-
`reinterpret_cast`. The result is that of
|
| 938 |
-
where `p` is a pointer to *x* of type
|
| 939 |
-
|
| 940 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 941 |
|
| 942 |
#### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 943 |
|
| 944 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 945 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
| 946 |
is an rvalue reference to object type, the result is an xvalue;
|
| 947 |
otherwise, the result is a prvalue and the lvalue-to-rvalue
|
| 948 |
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
| 949 |
[[conv.func]] standard conversions are performed on the expression `v`.
|
| 950 |
-
|
| 951 |
-
|
| 952 |
-
`const_cast`.
|
|
|
|
| 953 |
|
| 954 |
[*Note 1*: Subject to the restrictions in this subclause, an expression
|
| 955 |
can be cast to its own type using a `const_cast`
|
| 956 |
operator. — *end note*]
|
| 957 |
|
| 958 |
-
For two similar
|
| 959 |
-
`
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
``` cpp
|
| 967 |
-
typedef int *A[3]; // array of 3 pointer to int
|
| 968 |
-
typedef const int *const CA[3]; // array of 3 const pointer to const int
|
| 969 |
-
|
| 970 |
-
CA &&r = A{}; // OK, reference binds to temporary array object
|
| 971 |
-
// after qualification conversion to type CA
|
| 972 |
-
A &&r1 = const_cast<A>(CA{}); // error: temporary array decayed to pointer
|
| 973 |
-
A &&r2 = const_cast<A&&>(CA{}); // OK
|
| 974 |
-
```
|
| 975 |
-
|
| 976 |
-
— *end example*]
|
| 977 |
|
| 978 |
For two object types `T1` and `T2`, if a pointer to `T1` can be
|
| 979 |
explicitly converted to the type “pointer to `T2`” using a `const_cast`,
|
| 980 |
then the following conversions can also be made:
|
| 981 |
|
| 982 |
- an lvalue of type `T1` can be explicitly converted to an lvalue of
|
| 983 |
type `T2` using the cast `const_cast<T2&>`;
|
| 984 |
- a glvalue of type `T1` can be explicitly converted to an xvalue of
|
| 985 |
type `T2` using the cast `const_cast<T2&&>`; and
|
| 986 |
-
- if `T1` is a class type, a prvalue of type `T1` can be
|
| 987 |
-
converted to an xvalue of type `T2` using the cast
|
|
|
|
|
|
|
| 988 |
|
| 989 |
-
The result
|
| 990 |
-
|
| 991 |
-
materialization conversion [[conv.rval]] otherwise.
|
| 992 |
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
| 996 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 997 |
|
| 998 |
[*Note 2*:
|
| 999 |
|
| 1000 |
Depending on the type of the object, a write operation through the
|
| 1001 |
pointer, lvalue or pointer to data member resulting from a `const_cast`
|
| 1002 |
-
that casts away a const-qualifier[^
|
| 1003 |
|
| 1004 |
can produce undefined behavior [[dcl.type.cv]].
|
| 1005 |
|
| 1006 |
— *end note*]
|
| 1007 |
|
|
|
|
| 11 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 12 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 13 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 14 |
simple-type-specifier braced-init-list
|
| 15 |
typename-specifier braced-init-list
|
| 16 |
+
postfix-expression '.' templateₒₚₜ id-expression
|
| 17 |
+
postfix-expression '.' splice-expression
|
| 18 |
+
postfix-expression '->' templateₒₚₜ id-expression
|
| 19 |
+
postfix-expression '->' splice-expression
|
| 20 |
postfix-expression '++'
|
| 21 |
postfix-expression '--'
|
| 22 |
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 23 |
static_cast '<' type-id '>' '(' expression ')'
|
| 24 |
reinterpret_cast '<' type-id '>' '(' expression ')'
|
|
|
|
| 41 |
|
| 42 |
A *subscript expression* is a postfix expression followed by square
|
| 43 |
brackets containing a possibly empty, comma-separated list of
|
| 44 |
*initializer-clause*s that constitute the arguments to the subscript
|
| 45 |
operator. The *postfix-expression* and the initialization of the object
|
| 46 |
+
parameter [[dcl.fct]] of any applicable subscript operator function
|
| 47 |
+
[[over.sub]] is sequenced before each expression in the
|
| 48 |
+
*expression-list* and also before any default argument
|
| 49 |
+
[[dcl.fct.default]]. The initialization of a non-object parameter of a
|
| 50 |
+
subscript operator function `S`, including every associated value
|
| 51 |
+
computation and side effect, is indeterminately sequenced with respect
|
| 52 |
+
to that of any other non-object parameter of `S`.
|
| 53 |
|
| 54 |
With the built-in subscript operator, an *expression-list* shall be
|
| 55 |
present, consisting of a single *assignment-expression*. One of the
|
| 56 |
expressions shall be a glvalue of type “array of `T`” or a prvalue of
|
| 57 |
type “pointer to `T`” and the other shall be a prvalue of unscoped
|
| 58 |
enumeration or integral type. The result is of type “`T`”. The type
|
| 59 |
+
“`T`” shall be a completely-defined object type.[^10]
|
| 60 |
|
| 61 |
The expression `E1[E2]` is identical (by definition) to `*((E1)+(E2))`,
|
| 62 |
except that in the case of an array operand, the result is an lvalue if
|
| 63 |
that operand is an lvalue and an xvalue otherwise.
|
| 64 |
|
|
|
|
| 71 |
|
| 72 |
A function call is a postfix expression followed by parentheses
|
| 73 |
containing a possibly empty, comma-separated list of
|
| 74 |
*initializer-clause*s which constitute the arguments to the function.
|
| 75 |
|
| 76 |
+
[*Note 1*: If the postfix expression is a function name, the
|
| 77 |
+
appropriate function and the validity of the call are determined
|
| 78 |
+
according to the rules in [[over.match]]. — *end note*]
|
| 79 |
|
| 80 |
The postfix expression shall have function type or function pointer
|
| 81 |
type. For a call to a non-member function or to a static member
|
| 82 |
+
function, the postfix expression shall be either an lvalue that refers
|
| 83 |
to a function (in which case the function-to-pointer standard conversion
|
| 84 |
+
[[conv.func]] is suppressed on the postfix expression), or a prvalue of
|
| 85 |
+
function pointer type.
|
| 86 |
|
| 87 |
If the selected function is non-virtual, or if the *id-expression* in
|
| 88 |
the class member access expression is a *qualified-id*, that function is
|
| 89 |
called. Otherwise, its final overrider [[class.virtual]] in the dynamic
|
| 90 |
type of the object expression is called; such a call is referred to as a
|
|
|
|
| 93 |
[*Note 2*: The dynamic type is the type of the object referred to by
|
| 94 |
the current value of the object expression. [[class.cdtor]] describes
|
| 95 |
the behavior of virtual function calls when the object expression refers
|
| 96 |
to an object under construction or destruction. — *end note*]
|
| 97 |
|
| 98 |
+
[*Note 3*: If a function name is used, and name lookup [[basic.lookup]]
|
| 99 |
+
does not find a declaration of that name, the program is ill-formed. No
|
| 100 |
+
function is implicitly declared by such a call. — *end note*]
|
|
|
|
| 101 |
|
| 102 |
If the *postfix-expression* names a destructor or pseudo-destructor
|
| 103 |
[[expr.prim.id.dtor]], the type of the function call expression is
|
| 104 |
`void`; otherwise, the type of the function call expression is the
|
| 105 |
return type of the statically chosen function (i.e., ignoring the
|
|
|
|
| 108 |
which case the *postfix-expression* is a possibly-parenthesized class
|
| 109 |
member access), the function call destroys the object of scalar type
|
| 110 |
denoted by the object expression of the class member access
|
| 111 |
[[expr.ref]], [[basic.life]].
|
| 112 |
|
| 113 |
+
A type `T`_call is *call-compatible* with a function type `T`_func if
|
| 114 |
+
`T`_call is the same type as `T`_func or if the type “pointer to
|
| 115 |
+
`T`_func” can be converted to type “pointer to `T`_call” via a function
|
| 116 |
+
pointer conversion [[conv.fctptr]]. Calling a function through an
|
| 117 |
+
expression whose function type is not call-compatible with the type of
|
| 118 |
+
the called function’s definition results in undefined behavior.
|
| 119 |
|
| 120 |
+
[*Note 4*: This requirement allows the case when the expression has the
|
| 121 |
+
type of a potentially-throwing function, but the called function has a
|
| 122 |
non-throwing exception specification, and the function types are
|
| 123 |
otherwise the same. — *end note*]
|
| 124 |
|
| 125 |
When a function is called, each parameter [[dcl.fct]] is initialized
|
| 126 |
+
[[dcl.init]], [[class.copy.ctor]] with its corresponding argument, and
|
| 127 |
+
each precondition assertion of the function is evaluated
|
| 128 |
+
[[dcl.contract.func]]. If the function is an explicit object member
|
| 129 |
+
function and there is an implied object argument [[over.call.func]], the
|
| 130 |
+
list of provided arguments is preceded by the implied object argument
|
| 131 |
+
for the purposes of this correspondence. If there is no corresponding
|
| 132 |
+
argument, the default argument for the parameter is used.
|
| 133 |
|
| 134 |
[*Example 1*:
|
| 135 |
|
| 136 |
``` cpp
|
| 137 |
template<typename ...T> int f(int n = 0, T ...t);
|
| 138 |
int x = f<int>(); // error: no argument for second function parameter
|
| 139 |
```
|
| 140 |
|
| 141 |
— *end example*]
|
| 142 |
|
| 143 |
+
If the function is an implicit object member function, the object
|
| 144 |
+
expression of the class member access shall be a glvalue and the
|
| 145 |
+
implicit object parameter of the function [[over.match.funcs]] is
|
| 146 |
+
initialized with that glvalue, converted as if by an explicit type
|
| 147 |
conversion [[expr.cast]].
|
| 148 |
|
| 149 |
[*Note 5*: There is no access or ambiguity checking on this conversion;
|
| 150 |
the access checking and disambiguation are done as part of the (possibly
|
| 151 |
implicit) class member access operator. See [[class.member.lookup]],
|
|
|
|
| 156 |
|
| 157 |
[*Note 6*: This still allows a parameter to be a pointer or reference
|
| 158 |
to such a type. However, it prevents a passed-by-value parameter to have
|
| 159 |
an incomplete or abstract class type. — *end note*]
|
| 160 |
|
| 161 |
+
It is *implementation-defined* whether a parameter is destroyed when the
|
| 162 |
+
function in which it is defined exits
|
| 163 |
+
[[stmt.return]], [[except.ctor]], [[expr.await]] or at the end of the
|
| 164 |
+
enclosing full-expression; parameters are always destroyed in the
|
| 165 |
+
reverse order of their construction. The initialization and destruction
|
| 166 |
+
of each parameter occurs within the context of the full-expression
|
| 167 |
+
[[intro.execution]] where the function call appears.
|
| 168 |
|
| 169 |
+
[*Example 2*: The access [[class.access.general]] of the constructor,
|
| 170 |
+
conversion functions, or destructor is checked at the point of call. If
|
| 171 |
+
a constructor or destructor for a function parameter throws an
|
| 172 |
+
exception, any *function-try-block* [[except.pre]] of the called
|
| 173 |
+
function with a handler that can handle the exception is not
|
|
|
|
| 174 |
considered. — *end example*]
|
| 175 |
|
| 176 |
The *postfix-expression* is sequenced before each *expression* in the
|
| 177 |
*expression-list* and any default argument. The initialization of a
|
| 178 |
+
parameter or, if the implementation introduces any temporary objects to
|
| 179 |
+
hold the values of function parameters [[class.temporary]], the
|
| 180 |
+
initialization of those temporaries, including every associated value
|
| 181 |
+
computation and side effect, is indeterminately sequenced with respect
|
| 182 |
+
to that of any other parameter. These evaluations are sequenced before
|
| 183 |
+
the evaluation of the precondition assertions of the function, which are
|
| 184 |
+
evaluated in sequence [[dcl.contract.func]]. For any temporaries
|
| 185 |
+
introduced to hold the values of function parameters, the initialization
|
| 186 |
+
of the parameter objects from those temporaries is indeterminately
|
| 187 |
+
sequenced with respect to the evaluation of each precondition assertion.
|
| 188 |
|
| 189 |
[*Note 7*: All side effects of argument evaluations are sequenced
|
| 190 |
before the function is entered (see
|
| 191 |
[[intro.execution]]). — *end note*]
|
| 192 |
|
|
|
|
| 230 |
function call if the return type of the final overrider is different
|
| 231 |
from the return type of the statically chosen function, the value
|
| 232 |
returned from the final overrider is converted to the return type of the
|
| 233 |
statically chosen function.
|
| 234 |
|
| 235 |
+
When the called function exits normally [[stmt.return]], [[expr.await]],
|
| 236 |
+
all postcondition assertions of the function are evaluated in sequence
|
| 237 |
+
[[dcl.contract.func]]. If the implementation introduces any temporary
|
| 238 |
+
objects to hold the result value as specified in [[class.temporary]],
|
| 239 |
+
the evaluation of each postcondition assertion is indeterminately
|
| 240 |
+
sequenced with respect to the initialization of any of those temporaries
|
| 241 |
+
or the result object. These evaluations, in turn, are sequenced before
|
| 242 |
+
the destruction of any function parameters.
|
| 243 |
+
|
| 244 |
[*Note 9*: A function can change the values of its non-const
|
| 245 |
parameters, but these changes cannot affect the values of the arguments
|
| 246 |
except where a parameter is of a reference type [[dcl.ref]]; if the
|
| 247 |
+
reference is to a const-qualified type, `const_cast` needs to be used to
|
| 248 |
+
cast away the constness in order to modify the argument’s value. Where a
|
| 249 |
+
parameter is of `const` reference type a temporary object is introduced
|
| 250 |
+
if needed
|
| 251 |
[[dcl.type]], [[lex.literal]], [[lex.string]], [[dcl.array]], [[class.temporary]].
|
| 252 |
In addition, it is possible to modify the values of non-constant objects
|
| 253 |
through pointer parameters. — *end note*]
|
| 254 |
|
| 255 |
A function can be declared to accept fewer arguments (by declaring
|
|
|
|
| 275 |
function-to-pointer [[conv.func]] standard conversions are performed on
|
| 276 |
the argument expression. An argument that has type cv `std::nullptr_t`
|
| 277 |
is converted to type `void*` [[conv.ptr]]. After these conversions, if
|
| 278 |
the argument does not have arithmetic, enumeration, pointer,
|
| 279 |
pointer-to-member, or class type, the program is ill-formed. Passing a
|
| 280 |
+
potentially-evaluated argument of a scoped enumeration type [[dcl.enum]]
|
| 281 |
+
or of a class type [[class]] having an eligible non-trivial copy
|
| 282 |
+
constructor [[special]], [[class.copy.ctor]], an eligible non-trivial
|
| 283 |
+
move constructor, or a non-trivial destructor [[class.dtor]], with no
|
| 284 |
+
corresponding parameter, is conditionally-supported with
|
| 285 |
+
*implementation-defined* semantics. If the argument has integral or
|
| 286 |
enumeration type that is subject to the integral promotions
|
| 287 |
[[conv.prom]], or a floating-point type that is subject to the
|
| 288 |
floating-point promotion [[conv.fpprom]], the value of the argument is
|
| 289 |
converted to the promoted type before the call. These promotions are
|
| 290 |
referred to as the *default argument promotions*.
|
|
|
|
| 292 |
Recursive calls are permitted, except to the `main` function
|
| 293 |
[[basic.start.main]].
|
| 294 |
|
| 295 |
A function call is an lvalue if the result type is an lvalue reference
|
| 296 |
type or an rvalue reference to function type, an xvalue if the result
|
| 297 |
+
type is an rvalue reference to object type, and a prvalue otherwise. If
|
| 298 |
+
it is a non-void prvalue, the type of the function call expression shall
|
| 299 |
+
be complete, except as specified in [[dcl.type.decltype]].
|
| 300 |
|
| 301 |
#### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 302 |
|
| 303 |
A *simple-type-specifier* [[dcl.type.simple]] or *typename-specifier*
|
| 304 |
[[temp.res]] followed by a parenthesized optional *expression-list* or
|
|
|
|
| 307 |
deduced class type, it is replaced by the return type of the function
|
| 308 |
selected by overload resolution for class template deduction
|
| 309 |
[[over.match.class.deduct]] for the remainder of this subclause.
|
| 310 |
Otherwise, if the type contains a placeholder type, it is replaced by
|
| 311 |
the type determined by placeholder type deduction
|
| 312 |
+
[[dcl.type.auto.deduct]]. Let `T` denote the resulting type. Then:
|
| 313 |
+
|
| 314 |
+
- If the initializer is a parenthesized single expression, the type
|
| 315 |
+
conversion expression is equivalent to the corresponding cast
|
| 316 |
+
expression [[expr.cast]].
|
| 317 |
+
- Otherwise, if `T` is cv `void`, the initializer shall be `()` or `{}`
|
| 318 |
+
(after pack expansion, if any), and the expression is a prvalue of
|
| 319 |
+
type `void` that performs no initialization.
|
| 320 |
+
- Otherwise, if `T` is a reference type, the expression has the same
|
| 321 |
+
effect as direct-initializing an invented variable `t` of type `T`
|
| 322 |
+
from the initializer and then using `t` as the result of the
|
| 323 |
+
expression; the result is an lvalue if `T` is an lvalue reference type
|
| 324 |
+
or an rvalue reference to function type and an xvalue otherwise.
|
| 325 |
+
- Otherwise, the expression is a prvalue of type `T` whose result object
|
| 326 |
+
is direct-initialized [[dcl.init]] with the initializer.
|
| 327 |
+
|
| 328 |
+
If the initializer is a parenthesized optional *expression-list*, `T`
|
| 329 |
+
shall not be an array type.
|
| 330 |
|
| 331 |
[*Example 1*:
|
| 332 |
|
| 333 |
``` cpp
|
| 334 |
struct A {};
|
|
|
|
| 342 |
}
|
| 343 |
```
|
| 344 |
|
| 345 |
— *end example*]
|
| 346 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
#### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 348 |
|
| 349 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 350 |
followed by the keyword `template`, and then followed by an
|
| 351 |
+
*id-expression* or a *splice-expression*, is a postfix expression.
|
|
|
|
| 352 |
|
| 353 |
+
[*Note 1*: If the keyword `template` is used and followed by an
|
| 354 |
+
*id-expression*, the unqualified name is considered to refer to a
|
| 355 |
+
template [[temp.names]]. If a *simple-template-id* results and is
|
| 356 |
+
followed by a `::`, the *id-expression* is a
|
| 357 |
+
*qualified-id*. — *end note*]
|
| 358 |
|
| 359 |
+
For a dot that is followed by an expression that designates a static
|
| 360 |
+
member or an enumerator, the first expression is a discarded-value
|
| 361 |
+
expression [[expr.context]]; if the expression after the dot designates
|
| 362 |
+
a non-static data member, the first expression shall be a glvalue. A
|
| 363 |
+
postfix expression that is followed by an arrow shall be a prvalue
|
| 364 |
+
having pointer type. The expression `E1->E2` is converted to the
|
| 365 |
+
equivalent form `(*(E1)).E2`; the remainder of [[expr.ref]] will address
|
| 366 |
+
only the form using a dot.[^11]
|
| 367 |
|
| 368 |
+
The postfix expression before the dot is evaluated;[^12]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
the result of that evaluation, together with the *id-expression* or
|
| 371 |
+
*splice-expression*, determines the result of the entire postfix
|
| 372 |
+
expression.
|
| 373 |
+
|
| 374 |
+
Abbreviating *postfix-expression*`.`*id-expression* or
|
| 375 |
+
*postfix-expression*`.`*splice-expression* as `E1.E2`, `E1` is called
|
| 376 |
+
the *object expression*. If the object expression is of scalar type,
|
| 377 |
+
`E2` shall name the pseudo-destructor of that same type (ignoring
|
| 378 |
cv-qualifications) and `E1.E2` is a prvalue of type “function of ()
|
| 379 |
returning `void`”.
|
| 380 |
|
| 381 |
[*Note 2*: This value can only be used for a notional function call
|
| 382 |
[[expr.prim.id.dtor]]. — *end note*]
|
|
|
|
| 389 |
when the class is complete [[class.member.lookup]]. — *end note*]
|
| 390 |
|
| 391 |
[*Note 4*: [[basic.lookup.qual]] describes how names are looked up
|
| 392 |
after the `.` and `->` operators. — *end note*]
|
| 393 |
|
| 394 |
+
If `E2` is a *splice-expression*, then let `T1` be the type of `E1`.
|
| 395 |
+
`E2` shall designate either a member of `T1` or a direct base class
|
| 396 |
+
relationship (`T1`, `B`).
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
+
If `E2` designates a bit-field, `E1.E2` is a bit-field. The type and
|
| 399 |
+
value category of `E1.E2` are determined as follows. In the remainder
|
| 400 |
+
of [[expr.ref]], *cq* represents either `const` or the absence of
|
| 401 |
+
`const` and *vq* represents either `volatile` or the absence of
|
| 402 |
+
`volatile`. *cv* represents an arbitrary set of cv-qualifiers, as
|
| 403 |
+
defined in [[basic.type.qualifier]].
|
| 404 |
|
| 405 |
+
If `E2` designates an entity that is declared to have type “reference to
|
| 406 |
+
`T`”, then `E1.E2` is an lvalue of type `T`. In that case, if `E2`
|
| 407 |
+
designates a static data member, `E1.E2` designates the object or
|
| 408 |
+
function to which the reference is bound, otherwise `E1.E2` designates
|
| 409 |
+
the object or function to which the corresponding reference member of
|
| 410 |
+
`E1` is bound. Otherwise, one of the following rules applies.
|
| 411 |
+
|
| 412 |
+
- If `E2` designates a static data member and the type of `E2` is `T`,
|
| 413 |
+
then `E1.E2` is an lvalue; the expression designates the named member
|
| 414 |
+
of the class. The type of `E1.E2` is `T`.
|
| 415 |
+
- Otherwise, if `E2` designates a non-static data member and the type of
|
| 416 |
+
`E1` is “*cq1 vq1* `X`”, and the type of `E2` is “*cq2 vq2* `T`”, the
|
| 417 |
+
expression designates the corresponding member subobject of the object
|
| 418 |
+
designated by `E1`. If `E1` is an lvalue, then `E1.E2` is an lvalue;
|
| 419 |
+
otherwise `E1.E2` is an xvalue. Let the notation *vq12* stand for the
|
| 420 |
+
“union” of *vq1* and *vq2*; that is, if *vq1* or *vq2* is `volatile`,
|
| 421 |
+
then *vq12* is `volatile`. Similarly, let the notation *cq12* stand
|
| 422 |
+
for the “union” of *cq1* and *cq2*; that is, if *cq1* or *cq2* is
|
| 423 |
+
`const`, then *cq12* is `const`. If the entity designated by `E2` is
|
| 424 |
+
declared to be a `mutable` member, then the type of `E1.E2` is “*vq12*
|
| 425 |
+
`T`”. If the entity designated by `E2` is not declared to be a
|
| 426 |
+
`mutable` member, then the type of `E1.E2` is “*cq12* *vq12* `T`”.
|
| 427 |
+
- Otherwise, if `E2` denotes an overload set, the expression shall be
|
| 428 |
+
the (possibly-parenthesized) left-hand operand of a member function
|
| 429 |
+
call [[expr.call]], and function overload resolution [[over.match]] is
|
| 430 |
+
used to select the function to which `E2` refers. The type of `E1.E2`
|
| 431 |
+
is the type of `E2` and `E1.E2` refers to the function referred to by
|
| 432 |
+
`E2`.
|
| 433 |
- If `E2` refers to a static member function, `E1.E2` is an lvalue.
|
| 434 |
- Otherwise (when `E2` refers to a non-static member function),
|
| 435 |
+
`E1.E2` is a prvalue. \[*Note 5*: Any redundant set of parentheses
|
| 436 |
+
surrounding the expression is ignored
|
| 437 |
+
[[expr.prim.paren]]. — *end note*]
|
| 438 |
+
- Otherwise, if `E2` designates a nested type, the expression `E1.E2` is
|
| 439 |
+
ill-formed.
|
| 440 |
+
- Otherwise, if `E2` designates a member enumerator and the type of `E2`
|
| 441 |
+
is `T`, the expression `E1.E2` is a prvalue of type `T` whose value is
|
| 442 |
+
the value of the enumerator.
|
| 443 |
+
- Otherwise, if `E2` designates a direct base class relationship (D, B)
|
| 444 |
+
and the type of `E1` is cv `T`, the expression designates the direct
|
| 445 |
+
base class subobject of type B of the object designated by `E1`. If
|
| 446 |
+
`E1` is an lvalue, then `E1.E2` is an lvalue; otherwise, `E1.E2` is an
|
| 447 |
+
xvalue. The type of `E1.E2` is “cv `B`”.
|
| 448 |
+
\[*Note 6*: This can only occur in an expression of the form
|
| 449 |
+
`e1.[:e2:]`. — *end note*]
|
| 450 |
+
\[*Example 1*:
|
| 451 |
+
``` cpp
|
| 452 |
+
struct B {
|
| 453 |
+
int b;
|
| 454 |
+
};
|
| 455 |
+
struct C : B {
|
| 456 |
+
int get() const { return b; }
|
| 457 |
+
};
|
| 458 |
+
struct D : B, C { };
|
| 459 |
+
|
| 460 |
+
constexpr int f() {
|
| 461 |
+
D d = {1, {}};
|
| 462 |
+
|
| 463 |
+
// b unambiguously refers to the direct base class of type B,
|
| 464 |
+
// not the indirect base class of type B
|
| 465 |
+
B& b = d.[: std::meta::bases_of(^^D, std::meta::access_context::current())[0] :];
|
| 466 |
+
b.b += 10;
|
| 467 |
+
return 10 * b.b + d.get();
|
| 468 |
+
}
|
| 469 |
+
static_assert(f() == 110);
|
| 470 |
+
```
|
| 471 |
+
|
| 472 |
+
— *end example*]
|
| 473 |
+
- Otherwise, the program is ill-formed.
|
| 474 |
+
|
| 475 |
+
If `E2` designates a non-static member (possibly after overload
|
| 476 |
+
resolution), the program is ill-formed if the class of which `E2`
|
| 477 |
+
designates a direct member is an ambiguous base [[class.member.lookup]]
|
| 478 |
+
of the designating class [[class.access.base]] of `E2`.
|
| 479 |
+
|
| 480 |
+
[*Note 7*: The program is also ill-formed if the naming class is an
|
| 481 |
ambiguous base of the class type of the object expression; see
|
| 482 |
[[class.access.base]]. — *end note*]
|
| 483 |
|
| 484 |
+
If `E2` designates a non-static member (possibly after overload
|
| 485 |
+
resolution) and the result of `E1` is an object whose type is not
|
| 486 |
+
similar [[conv.qual]] to the type of `E1`, the behavior is undefined.
|
| 487 |
|
| 488 |
+
[*Example 2*:
|
| 489 |
|
| 490 |
``` cpp
|
| 491 |
struct A { int i; };
|
| 492 |
struct B { int j; };
|
| 493 |
struct D : A, B {};
|
|
|
|
| 500 |
|
| 501 |
— *end example*]
|
| 502 |
|
| 503 |
#### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 504 |
|
| 505 |
+
The value of a postfix `++` expression is the value obtained by applying
|
| 506 |
+
the lvalue-to-rvalue conversion [[conv.lval]] to its operand.
|
| 507 |
|
| 508 |
[*Note 1*: The value obtained is a copy of the original
|
| 509 |
value. — *end note*]
|
| 510 |
|
| 511 |
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 512 |
be an arithmetic type other than cv `bool`, or a pointer to a complete
|
| 513 |
object type. An operand with volatile-qualified type is deprecated; see
|
| 514 |
[[depr.volatile.type]]. The value of the operand object is modified
|
| 515 |
+
[[defns.access]] as if it were the operand of the prefix `++` operator
|
| 516 |
+
[[expr.pre.incr]]. The value computation of the `++` expression is
|
| 517 |
+
sequenced before the modification of the operand object. With respect to
|
| 518 |
+
an indeterminately-sequenced function call, the operation of postfix
|
| 519 |
+
`++` is a single evaluation.
|
| 520 |
|
| 521 |
[*Note 2*: Therefore, a function call cannot intervene between the
|
| 522 |
lvalue-to-rvalue conversion and the side effect associated with any
|
| 523 |
single postfix `++` operator. — *end note*]
|
| 524 |
|
| 525 |
The result is a prvalue. The type of the result is the cv-unqualified
|
| 526 |
+
version of the type of the operand.
|
|
|
|
|
|
|
|
|
|
| 527 |
|
| 528 |
The operand of postfix `--` is decremented analogously to the postfix
|
| 529 |
`++` operator.
|
| 530 |
|
| 531 |
[*Note 3*: For prefix increment and decrement, see
|
|
|
|
| 553 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 554 |
unique `B` subobject of the `D` object pointed to by `v`, or a null
|
| 555 |
pointer value if `v` is a null pointer value. Similarly, if `T` is
|
| 556 |
“reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B` is a
|
| 557 |
base class of `D`, the result is the unique `B` subobject of the `D`
|
| 558 |
+
object referred to by `v`.[^13]
|
| 559 |
|
| 560 |
In both the pointer and reference cases, the program is ill-formed if
|
| 561 |
`B` is an inaccessible or ambiguous base class of `D`.
|
| 562 |
|
| 563 |
[*Example 1*:
|
|
|
|
| 575 |
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic type
|
| 576 |
[[class.virtual]].
|
| 577 |
|
| 578 |
If `v` is a null pointer value, the result is a null pointer value.
|
| 579 |
|
| 580 |
+
If `v` has type “pointer to cv `U`” and `v` does not point to an object
|
| 581 |
+
whose type is similar [[conv.qual]] to `U` and that is within its
|
| 582 |
+
lifetime or within its period of construction or destruction
|
| 583 |
+
[[class.cdtor]], the behavior is undefined. If `v` is a glvalue of type
|
| 584 |
+
`U` and `v` does not refer to an object whose type is similar to `U` and
|
| 585 |
+
that is within its lifetime or within its period of construction or
|
| 586 |
+
destruction, the behavior is undefined.
|
| 587 |
+
|
| 588 |
If `T` is “pointer to cv `void`”, then the result is a pointer to the
|
| 589 |
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 590 |
applied to see if the object pointed or referred to by `v` can be
|
| 591 |
converted to the type pointed or referred to by `T`.
|
| 592 |
|
|
|
|
| 594 |
check logically executes as follows:
|
| 595 |
|
| 596 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 597 |
points (refers) to a public base class subobject of a `C` object, and
|
| 598 |
if only one object of type `C` is derived from the subobject pointed
|
| 599 |
+
(referred) to by `v`, the result points (refers) to that `C` object.
|
| 600 |
- Otherwise, if `v` points (refers) to a public base class subobject of
|
| 601 |
the most derived object, and the type of the most derived object has a
|
| 602 |
base class, of type `C`, that is unambiguous and public, the result
|
| 603 |
points (refers) to the `C` subobject of the most derived object.
|
| 604 |
- Otherwise, the runtime check *fails*.
|
|
|
|
| 644 |
|
| 645 |
#### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 646 |
|
| 647 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 648 |
`std::type_info` [[type.info]] and dynamic type `const` `std::type_info`
|
| 649 |
+
or `const` `name` where `name` is an *implementation-defined* class
|
| 650 |
publicly derived from `std::type_info` which preserves the behavior
|
| 651 |
+
described in [[type.info]].[^14]
|
| 652 |
|
| 653 |
The lifetime of the object referred to by the lvalue extends to the end
|
| 654 |
of the program. Whether or not the destructor is called for the
|
| 655 |
`std::type_info` object at the end of the program is unspecified.
|
| 656 |
|
| 657 |
If the type of the *expression* or *type-id* operand is a (possibly
|
| 658 |
cv-qualified) class type or a reference to (possibly cv-qualified) class
|
| 659 |
type, that class shall be completely defined.
|
| 660 |
|
| 661 |
+
If an *expression* operand of `typeid` is a possibly-parenthesized
|
| 662 |
+
*unary-expression* whose *unary-operator* is `*` and whose operand
|
| 663 |
+
evaluates to a null pointer value [[basic.compound]], the `typeid`
|
| 664 |
+
expression throws an exception [[except.throw]] of a type that would
|
| 665 |
+
match a handler of type `std::bad_typeid` [[bad.typeid]].
|
| 666 |
+
|
| 667 |
+
[*Note 1*: In other contexts, evaluating such a *unary-expression*
|
| 668 |
+
results in undefined behavior [[expr.unary.op]]. — *end note*]
|
| 669 |
+
|
| 670 |
When `typeid` is applied to a glvalue whose type is a polymorphic class
|
| 671 |
type [[class.virtual]], the result refers to a `std::type_info` object
|
| 672 |
representing the type of the most derived object [[intro.object]] (that
|
| 673 |
+
is, the dynamic type) to which the glvalue refers.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 676 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 677 |
representing the static type of the expression. Lvalue-to-rvalue
|
| 678 |
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
|
|
|
| 685 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 686 |
type of the *type-id* is a reference to a possibly cv-qualified type,
|
| 687 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 688 |
object representing the cv-unqualified referenced type.
|
| 689 |
|
| 690 |
+
[*Note 2*: The *type-id* cannot denote a function type with a
|
| 691 |
*cv-qualifier-seq* or a *ref-qualifier* [[dcl.fct]]. — *end note*]
|
| 692 |
|
| 693 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
| 694 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 695 |
representing the cv-unqualified type.
|
|
|
|
| 712 |
The type `std::type_info` [[type.info]] is not predefined; if a standard
|
| 713 |
library declaration [[typeinfo.syn]], [[std.modules]] of
|
| 714 |
`std::type_info` does not precede [[basic.lookup.general]] a `typeid`
|
| 715 |
expression, the program is ill-formed.
|
| 716 |
|
| 717 |
+
[*Note 3*: Subclause [[class.cdtor]] describes the behavior of `typeid`
|
| 718 |
applied to an object under construction or destruction. — *end note*]
|
| 719 |
|
| 720 |
#### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
| 721 |
|
| 722 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 723 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 724 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 725 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 726 |
+
otherwise, the result is a prvalue.
|
|
|
|
| 727 |
|
| 728 |
An lvalue of type “*cv1* `B`”, where `B` is a class type, can be cast to
|
| 729 |
+
type “reference to *cv2* `D`”, where `D` is a complete class derived
|
| 730 |
[[class.derived]] from `B`, if *cv2* is the same cv-qualification as, or
|
| 731 |
greater cv-qualification than, *cv1*. If `B` is a virtual base class of
|
| 732 |
`D` or a base class of a virtual base class of `D`, or if no valid
|
| 733 |
standard conversion from “pointer to `D`” to “pointer to `B`” exists
|
| 734 |
[[conv.ptr]], the program is ill-formed. An xvalue of type “*cv1* `B`”
|
|
|
|
| 759 |
used as the operand of the `static_cast` for the remainder of this
|
| 760 |
subclause. If `T2` is an inaccessible [[class.access]] or ambiguous
|
| 761 |
[[class.member.lookup]] base class of `T1`, a program that necessitates
|
| 762 |
such a cast is ill-formed.
|
| 763 |
|
| 764 |
+
Any expression can be explicitly converted to type cv `void`, in which
|
| 765 |
+
case the operand is a discarded-value expression [[expr.prop]].
|
| 766 |
+
|
| 767 |
+
[*Note 1*: Such a `static_cast` has no result as it is a prvalue of
|
| 768 |
+
type `void`; see [[basic.lval]]. — *end note*]
|
| 769 |
+
|
| 770 |
+
[*Note 2*: However, if the value is in a temporary object
|
| 771 |
+
[[class.temporary]], the destructor for that object is not executed
|
| 772 |
+
until the usual time, and the value of the object is preserved for the
|
| 773 |
+
purpose of executing the destructor. — *end note*]
|
| 774 |
+
|
| 775 |
+
Otherwise, an expression E can be explicitly converted to a type `T` if
|
| 776 |
+
there is an implicit conversion sequence [[over.best.ics]] from E to
|
| 777 |
+
`T`, if overload resolution for a direct-initialization [[dcl.init]] of
|
| 778 |
+
an object or reference of type `T` from E would find at least one viable
|
| 779 |
function [[over.match.viable]], or if `T` is an aggregate type
|
| 780 |
[[dcl.init.aggr]] having a first element `x` and there is an implicit
|
| 781 |
conversion sequence from E to the type of `x`. If `T` is a reference
|
| 782 |
type, the effect is the same as performing the declaration and
|
| 783 |
initialization
|
|
|
|
| 788 |
|
| 789 |
for some invented temporary variable `t` [[dcl.init]] and then using the
|
| 790 |
temporary variable as the result of the conversion. Otherwise, the
|
| 791 |
result object is direct-initialized from E.
|
| 792 |
|
| 793 |
+
[*Note 3*: The conversion is ill-formed when attempting to convert an
|
| 794 |
expression of class type to an inaccessible or ambiguous base
|
| 795 |
class. — *end note*]
|
| 796 |
|
| 797 |
+
[*Note 4*: If `T` is “array of unknown bound of `U`”, this
|
| 798 |
direct-initialization defines the type of the expression as
|
| 799 |
`U[1]`. — *end note*]
|
| 800 |
|
| 801 |
+
Otherwise, the lvalue-to-rvalue [[conv.lval]], array-to-pointer
|
| 802 |
+
[[conv.array]], and function-to-pointer [[conv.func]] conversions are
|
| 803 |
+
applied to the operand, and the conversions that can be performed using
|
| 804 |
+
`static_cast` are listed below. No other conversion can be performed
|
| 805 |
+
using `static_cast`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
|
| 807 |
A value of a scoped enumeration type [[dcl.enum]] can be explicitly
|
| 808 |
converted to an integral type; the result is the same as that of
|
| 809 |
converting to the enumeration’s underlying type and then to the
|
| 810 |
destination type. A value of a scoped enumeration type can also be
|
|
|
|
| 857 |
|
| 858 |
If no valid standard conversion from “pointer to member of `B` of type
|
| 859 |
`T`” to “pointer to member of `D` of type `T`” exists [[conv.mem]], the
|
| 860 |
program is ill-formed. The null member pointer value [[conv.mem]] is
|
| 861 |
converted to the null member pointer value of the destination type. If
|
| 862 |
+
class `B` contains the original member, or is a base class of the class
|
| 863 |
+
containing the original member, the resulting pointer to member points
|
| 864 |
+
to the original member. Otherwise, the behavior is undefined.
|
|
|
|
| 865 |
|
| 866 |
[*Note 6*: Although class `B` need not contain the original member, the
|
| 867 |
dynamic type of the object with which indirection through the pointer to
|
| 868 |
member is performed must contain the original member; see
|
| 869 |
[[expr.mptr.oper]]. — *end note*]
|
|
|
|
| 871 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
| 872 |
prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
|
| 873 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 874 |
*cv1*. If the original pointer value represents the address `A` of a
|
| 875 |
byte in memory and `A` does not satisfy the alignment requirement of
|
| 876 |
+
`T`, then the resulting pointer value [[basic.compound]] is unspecified.
|
| 877 |
+
Otherwise, if the original pointer value points to an object *a*, and
|
| 878 |
+
there is an object *b* of type similar to `T` that is
|
| 879 |
+
pointer-interconvertible [[basic.compound]] with *a*, the result is a
|
| 880 |
+
pointer to *b*. Otherwise, the pointer value is unchanged by the
|
| 881 |
+
conversion.
|
| 882 |
|
| 883 |
+
[*Example 2*:
|
| 884 |
|
| 885 |
``` cpp
|
| 886 |
T* p1 = new T;
|
| 887 |
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
|
| 888 |
bool b = p1 == p2; // b will have the value true.
|
|
|
|
| 927 |
any type to the type `std::nullptr_t`. — *end note*]
|
| 928 |
|
| 929 |
A value of integral type or enumeration type can be explicitly converted
|
| 930 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 931 |
any such exists on the implementation) and back to the same pointer type
|
| 932 |
+
will have its original value [[basic.compound]]; mappings between
|
| 933 |
+
pointers and integers are otherwise *implementation-defined*.
|
| 934 |
|
| 935 |
A function pointer can be explicitly converted to a function pointer of
|
| 936 |
a different type.
|
| 937 |
|
| 938 |
[*Note 4*: The effect of calling a function through a pointer to a
|
|
|
|
| 942 |
Except that converting a prvalue of type “pointer to `T1`” to the type
|
| 943 |
“pointer to `T2`” (where `T1` and `T2` are function types) and back to
|
| 944 |
its original type yields the original pointer value, the result of such
|
| 945 |
a pointer conversion is unspecified.
|
| 946 |
|
|
|
|
|
|
|
|
|
|
| 947 |
An object pointer can be explicitly converted to an object pointer of a
|
| 948 |
+
different type.[^15]
|
| 949 |
|
| 950 |
When a prvalue `v` of object pointer type is converted to the object
|
| 951 |
pointer type “pointer to cv `T`”, the result is
|
| 952 |
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 953 |
|
| 954 |
+
[*Note 5*: Converting a pointer of type “pointer to `T1`” that points
|
| 955 |
to an object of type `T1` to the type “pointer to `T2`” (where `T2` is
|
| 956 |
an object type and the alignment requirements of `T2` are no stricter
|
| 957 |
than those of `T1`) and back to its original type yields the original
|
| 958 |
pointer value. — *end note*]
|
| 959 |
|
|
|
|
| 965 |
yield the original pointer value.
|
| 966 |
|
| 967 |
The null pointer value [[basic.compound]] is converted to the null
|
| 968 |
pointer value of the destination type.
|
| 969 |
|
| 970 |
+
[*Note 6*: A null pointer constant of type `std::nullptr_t` cannot be
|
| 971 |
converted to a pointer type, and a null pointer constant of integral
|
| 972 |
type is not necessarily converted to a null pointer
|
| 973 |
value. — *end note*]
|
| 974 |
|
| 975 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 976 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 977 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 978 |
+
object types.[^16]
|
| 979 |
|
| 980 |
The null member pointer value [[conv.mem]] is converted to the null
|
| 981 |
member pointer value of the destination type. The result of this
|
| 982 |
conversion is unspecified, except in the following cases:
|
| 983 |
|
|
|
|
| 988 |
`T1`” to the type “pointer to data member of `Y` of type `T2`” (where
|
| 989 |
the alignment requirements of `T2` are no stricter than those of `T1`)
|
| 990 |
and back to its original type yields the original pointer-to-member
|
| 991 |
value.
|
| 992 |
|
| 993 |
+
If `v` is a glvalue of type `T1`, designating an object or function *x*,
|
| 994 |
+
it can be cast to the type “reference to `T2`” if an expression of type
|
| 995 |
+
“pointer to `T1`” can be explicitly converted to the type “pointer to
|
| 996 |
+
`T2`” using a `reinterpret_cast`. The result is that of
|
| 997 |
+
`*reinterpret_cast<T2 *>(p)` where `p` is a pointer to *x* of type
|
| 998 |
+
“pointer to `T1`”.
|
| 999 |
+
|
| 1000 |
+
[*Note 7*:
|
| 1001 |
+
|
| 1002 |
+
No temporary is materialized [[conv.rval]] or created, no copy is made,
|
| 1003 |
+
and no constructors [[class.ctor]] or conversion functions
|
| 1004 |
+
[[class.conv]] are called.[^17]
|
| 1005 |
+
|
| 1006 |
+
— *end note*]
|
| 1007 |
|
| 1008 |
#### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 1009 |
|
| 1010 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 1011 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
| 1012 |
is an rvalue reference to object type, the result is an xvalue;
|
| 1013 |
otherwise, the result is a prvalue and the lvalue-to-rvalue
|
| 1014 |
[[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
|
| 1015 |
[[conv.func]] standard conversions are performed on the expression `v`.
|
| 1016 |
+
The temporary materialization conversion [[conv.rval]] is not performed
|
| 1017 |
+
on `v`, other than as specified below. Conversions that can be performed
|
| 1018 |
+
explicitly using `const_cast` are listed below. No other conversion
|
| 1019 |
+
shall be performed explicitly using `const_cast`.
|
| 1020 |
|
| 1021 |
[*Note 1*: Subject to the restrictions in this subclause, an expression
|
| 1022 |
can be cast to its own type using a `const_cast`
|
| 1023 |
operator. — *end note*]
|
| 1024 |
|
| 1025 |
+
For two similar object pointer or pointer to data member types `T1` and
|
| 1026 |
+
`T2` [[conv.qual]], a prvalue of type `T1` can be explicitly converted
|
| 1027 |
+
to the type `T2` using a `const_cast` if, considering the
|
| 1028 |
+
qualification-decompositions of both types, each P¹ᵢ is the same as P²ᵢ
|
| 1029 |
+
for all i. If `v` is a null pointer or null member pointer, the result
|
| 1030 |
+
is a null pointer or null member pointer, respectively. Otherwise, the
|
| 1031 |
+
result points to or past the end of the same object, or points to the
|
| 1032 |
+
same member, respectively, as `v`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1033 |
|
| 1034 |
For two object types `T1` and `T2`, if a pointer to `T1` can be
|
| 1035 |
explicitly converted to the type “pointer to `T2`” using a `const_cast`,
|
| 1036 |
then the following conversions can also be made:
|
| 1037 |
|
| 1038 |
- an lvalue of type `T1` can be explicitly converted to an lvalue of
|
| 1039 |
type `T2` using the cast `const_cast<T2&>`;
|
| 1040 |
- a glvalue of type `T1` can be explicitly converted to an xvalue of
|
| 1041 |
type `T2` using the cast `const_cast<T2&&>`; and
|
| 1042 |
+
- if `T1` is a class or array type, a prvalue of type `T1` can be
|
| 1043 |
+
explicitly converted to an xvalue of type `T2` using the cast
|
| 1044 |
+
`const_cast<T2&&>`. The temporary materialization conversion is
|
| 1045 |
+
performed on `v`.
|
| 1046 |
|
| 1047 |
+
The result refers to the same object as the (possibly converted)
|
| 1048 |
+
operand.
|
|
|
|
| 1049 |
|
| 1050 |
+
[*Example 1*:
|
| 1051 |
+
|
| 1052 |
+
``` cpp
|
| 1053 |
+
typedef int *A[3]; // array of 3 pointer to int
|
| 1054 |
+
typedef const int *const CA[3]; // array of 3 const pointer to const int
|
| 1055 |
+
|
| 1056 |
+
auto &&r2 = const_cast<A&&>(CA{}); // OK, temporary materialization conversion is performed
|
| 1057 |
+
```
|
| 1058 |
+
|
| 1059 |
+
— *end example*]
|
| 1060 |
|
| 1061 |
[*Note 2*:
|
| 1062 |
|
| 1063 |
Depending on the type of the object, a write operation through the
|
| 1064 |
pointer, lvalue or pointer to data member resulting from a `const_cast`
|
| 1065 |
+
that casts away a const-qualifier[^18]
|
| 1066 |
|
| 1067 |
can produce undefined behavior [[dcl.type.cv]].
|
| 1068 |
|
| 1069 |
— *end note*]
|
| 1070 |
|