- tmp/tmp71dqzs_3/{from.md → to.md} +470 -375
tmp/tmp71dqzs_3/{from.md → to.md}
RENAMED
|
@@ -3,12 +3,11 @@
|
|
| 3 |
Postfix expressions group left-to-right.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
postfix-expression:
|
| 7 |
primary-expression
|
| 8 |
-
postfix-expression '['
|
| 9 |
-
postfix-expression '[' braced-init-list ']'
|
| 10 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 11 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 12 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 13 |
simple-type-specifier braced-init-list
|
| 14 |
typename-specifier braced-init-list
|
|
@@ -33,213 +32,247 @@ expression-list:
|
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
pseudo-destructor-name:
|
| 36 |
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 37 |
nested-name-specifier 'template' simple-template-id ':: ~' type-name
|
| 38 |
-
|
| 39 |
'~' decltype-specifier
|
| 40 |
```
|
| 41 |
|
| 42 |
-
The `>` token following the in a `dynamic_cast`,
|
| 43 |
-
`reinterpret_cast`, or `const_cast` may be the product of
|
| 44 |
-
`>{>}` token by two consecutive `>` tokens (
|
|
|
|
| 45 |
|
| 46 |
### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 47 |
|
| 48 |
A postfix expression followed by an expression in square brackets is a
|
| 49 |
-
postfix expression. One of the expressions shall
|
| 50 |
-
`T`” or “pointer to `T`” and the other shall
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
A *braced-init-list* shall not be used with the built-in subscript
|
| 59 |
operator.
|
| 60 |
|
| 61 |
### Function call <a id="expr.call">[[expr.call]]</a>
|
| 62 |
|
| 63 |
A function call is a postfix expression followed by parentheses
|
| 64 |
containing a possibly empty, comma-separated list of
|
| 65 |
*initializer-clause*s which constitute the arguments to the function.
|
| 66 |
-
The postfix expression shall have function type or
|
| 67 |
type. For a call to a non-member function or to a static member
|
| 68 |
function, the postfix expression shall be either an lvalue that refers
|
| 69 |
to a function (in which case the function-to-pointer standard
|
| 70 |
conversion ([[conv.func]]) is suppressed on the postfix expression), or
|
| 71 |
-
it shall have
|
| 72 |
-
expression whose function type
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
[[class.
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
function call of the form `f()` is interpreted as `(*this).f()` (see
|
| 83 |
-
[[class.mfct.non-static]]). If a function or member function name is
|
| 84 |
-
used, the name can be overloaded (Clause [[over]]), in which case the
|
| 85 |
-
appropriate function shall be selected according to the rules in
|
| 86 |
-
[[over.match]]. If the selected function is non-virtual, or if the
|
| 87 |
-
*id-expression* in the class member access expression is a
|
| 88 |
-
*qualified-id*, that function is called. Otherwise, its final
|
| 89 |
-
overrider ([[class.virtual]]) in the dynamic type of the object
|
| 90 |
-
expression is called; such a call is referred to as a *virtual function
|
| 91 |
-
call*. the dynamic type is the type of the object referred to by the
|
| 92 |
-
current value of the object expression. [[class.cdtor]] describes the
|
| 93 |
-
behavior of virtual function calls when the object expression refers to
|
| 94 |
-
an object under construction or destruction.
|
| 95 |
|
| 96 |
-
|
| 97 |
-
[[
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
If the *postfix-expression* designates a destructor ([[class.dtor]]),
|
| 101 |
the type of the function call expression is `void`; otherwise, the type
|
| 102 |
of the function call expression is the return type of the statically
|
| 103 |
chosen function (i.e., ignoring the `virtual` keyword), even if the type
|
| 104 |
of the function actually called is different. This return type shall be
|
| 105 |
-
an object type, a reference type or cv
|
| 106 |
|
| 107 |
When a function is called, each parameter ([[dcl.fct]]) shall be
|
| 108 |
initialized ([[dcl.init]], [[class.copy]], [[class.ctor]]) with its
|
| 109 |
-
corresponding argument.
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
[
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
[[class.
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
parameter
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
[[dcl.array]], [[class.temporary]]). In addition, it is possible to
|
| 149 |
-
modify the values of
|
|
|
|
| 150 |
|
| 151 |
A function can be declared to accept fewer arguments (by declaring
|
| 152 |
default arguments ([[dcl.fct.default]])) or more arguments (by using
|
| 153 |
the ellipsis, `...`, or a function parameter pack ([[dcl.fct]])) than
|
| 154 |
the number of parameters in the function definition ([[dcl.fct.def]]).
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
When there is no parameter for a given argument, the argument is passed
|
| 159 |
in such a way that the receiving function can obtain the value of the
|
| 160 |
-
argument by invoking `va_arg` ([[support.runtime]]).
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 166 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 167 |
conversions are performed on the argument expression. An argument that
|
| 168 |
-
has
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
enumeration type that is subject to the integral promotions (
|
| 177 |
-
[[conv.prom]]), or a floating
|
| 178 |
-
point promotion ([[conv.fpprom]]), the value of the argument
|
| 179 |
-
converted to the promoted type before the call. These promotions are
|
| 180 |
referred to as the *default argument promotions*.
|
| 181 |
|
| 182 |
-
The evaluations of the postfix expression and of the arguments are all
|
| 183 |
-
unsequenced relative to one another. All side effects of argument
|
| 184 |
-
evaluations are sequenced before the function is entered (see
|
| 185 |
-
[[intro.execution]]).
|
| 186 |
-
|
| 187 |
Recursive calls are permitted, except to the `main` function (
|
| 188 |
[[basic.start.main]]).
|
| 189 |
|
| 190 |
A function call is an lvalue if the result type is an lvalue reference
|
| 191 |
type or an rvalue reference to function type, an xvalue if the result
|
| 192 |
type is an rvalue reference to object type, and a prvalue otherwise.
|
| 193 |
|
| 194 |
-
If a function call is a prvalue of object type:
|
| 195 |
-
|
| 196 |
-
- if the function call is either
|
| 197 |
-
- the operand of a *decltype-specifier* or
|
| 198 |
-
- the right operand of a comma operator that is the operand of a
|
| 199 |
-
*decltype-specifier*,
|
| 200 |
-
|
| 201 |
-
a temporary object is not introduced for the prvalue. The type of the
|
| 202 |
-
prvalue may be incomplete. as a result, storage is not allocated for
|
| 203 |
-
the prvalue and it is not destroyed; thus, a class type is not
|
| 204 |
-
instantiated as a result of being the type of a function call in this
|
| 205 |
-
context. This is true regardless of whether the expression uses
|
| 206 |
-
function call notation or operator notation ([[over.match.oper]]).
|
| 207 |
-
unlike the rule for a *decltype-specifier* that considers whether an
|
| 208 |
-
*id-expression* is parenthesized ([[dcl.type.simple]]), parentheses
|
| 209 |
-
have no special meaning in this context.
|
| 210 |
-
- otherwise, the type of the prvalue shall be complete.
|
| 211 |
-
|
| 212 |
### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 213 |
|
| 214 |
A *simple-type-specifier* ([[dcl.type.simple]]) or
|
| 215 |
*typename-specifier* ([[temp.res]]) followed by a parenthesized
|
| 216 |
-
*expression-list*
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
conversion expression is equivalent (in definedness, and if defined in
|
| 219 |
meaning) to the corresponding cast expression ([[expr.cast]]). If the
|
| 220 |
-
type
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
[[
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
prvalue.
|
| 227 |
-
|
| 228 |
-
The expression `T()`, where `T` is a *simple-type-specifier* or
|
| 229 |
-
*typename-specifier* for a non-array complete object type or the
|
| 230 |
-
(possibly cv-qualified) `void` type, creates a prvalue of the specified
|
| 231 |
-
type, whose value is that produced by value-initializing ([[dcl.init]])
|
| 232 |
-
an object of type `T`; no initialization is done for the `void()` case.
|
| 233 |
-
if `T` is a non-class type that is cv-qualified, the *cv-qualifier*s are
|
| 234 |
-
discarded when determining the type of the resulting prvalue (Clause
|
| 235 |
-
[[expr]]).
|
| 236 |
-
|
| 237 |
-
Similarly, a *simple-type-specifier* or *typename-specifier* followed by
|
| 238 |
-
a *braced-init-list* creates a temporary object of the specified type
|
| 239 |
-
direct-list-initialized ([[dcl.init.list]]) with the specified
|
| 240 |
-
*braced-init-list*, and its value is that temporary object as a prvalue.
|
| 241 |
|
| 242 |
### Pseudo destructor call <a id="expr.pseudo">[[expr.pseudo]]</a>
|
| 243 |
|
| 244 |
The use of a *pseudo-destructor-name* after a dot `.` or arrow `->`
|
| 245 |
operator represents the destructor for the non-class type denoted by
|
|
@@ -257,40 +290,45 @@ the object type and of the type designated by the
|
|
| 257 |
|
| 258 |
``` bnf
|
| 259 |
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 260 |
```
|
| 261 |
|
| 262 |
-
shall designate the same scalar type.
|
| 263 |
|
| 264 |
### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 265 |
|
| 266 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 267 |
followed by the keyword `template` ([[temp.names]]), and then followed
|
| 268 |
by an *id-expression*, is a postfix expression. The postfix expression
|
| 269 |
before the dot or arrow is evaluated;[^6] the result of that evaluation,
|
| 270 |
together with the *id-expression*, determines the result of the entire
|
| 271 |
postfix expression.
|
| 272 |
|
| 273 |
-
For the first option (dot) the first expression shall
|
| 274 |
-
class type. For the second option (arrow) the first
|
| 275 |
-
|
| 276 |
-
converted to the equivalent form `(*(E1)).E2`;
|
| 277 |
-
[[expr.ref]] will address only the first option
|
| 278 |
-
case, the *id-expression* shall name a member of
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
Abbreviating *postfix-expression.id-expression* as `E1.E2`, `E1` is
|
| 285 |
-
called the *object expression*.
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
|
|
|
| 290 |
|
| 291 |
-
If `E2` is declared to have type “reference to `T`
|
| 292 |
lvalue; the type of `E1.E2` is `T`. Otherwise, one of the following
|
| 293 |
rules applies.
|
| 294 |
|
| 295 |
- If `E2` is a static data member and the type of `E2` is `T`, then
|
| 296 |
`E1.E2` is an lvalue; the expression designates the named member of
|
|
@@ -315,63 +353,75 @@ rules applies.
|
|
| 315 |
lvalue; the expression designates the static member function. The
|
| 316 |
type of `E1.E2` is the same type as that of `E2`, namely “function
|
| 317 |
of parameter-type-list returning `T`”.
|
| 318 |
- Otherwise, if `E1.E2` refers to a non-static member function and the
|
| 319 |
type of `E2` is “function of parameter-type-list *cv*
|
| 320 |
-
*ref-qualifierₒₚₜ
|
| 321 |
expression designates a non-static member function. The expression
|
| 322 |
can be used only as the left-hand operand of a member function
|
| 323 |
-
call ([[class.mfct]]). Any redundant set of parentheses
|
| 324 |
-
the expression is ignored (
|
| 325 |
-
|
|
|
|
| 326 |
- If `E2` is a nested type, the expression `E1.E2` is ill-formed.
|
| 327 |
- If `E2` is a member enumerator and the type of `E2` is `T`, the
|
| 328 |
expression `E1.E2` is a prvalue. The type of `E1.E2` is `T`.
|
| 329 |
|
| 330 |
If `E2` is a non-static data member or a non-static member function, the
|
| 331 |
program is ill-formed if the class of which `E2` is directly a member is
|
| 332 |
an ambiguous base ([[class.member.lookup]]) of the naming class (
|
| 333 |
-
[[class.access.base]]) of `E2`.
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
| 336 |
|
| 337 |
### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 338 |
|
| 339 |
-
The value of a postfix `++` expression is the value of its operand.
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
| 347 |
indeterminately-sequenced function call, the operation of postfix `++`
|
| 348 |
-
is a single evaluation.
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
The operand of postfix `\dcr` is decremented analogously to the postfix
|
| 355 |
-
`++` operator
|
| 356 |
-
|
|
|
|
|
|
|
| 357 |
|
| 358 |
### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 359 |
|
| 360 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 361 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 362 |
-
reference to a complete class type, or “pointer to *cv* `void`
|
| 363 |
`dynamic_cast` operator shall not cast away constness (
|
| 364 |
[[expr.const.cast]]).
|
| 365 |
|
| 366 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 367 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 368 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 369 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 370 |
-
is an rvalue reference type, `v` shall be
|
| 371 |
-
|
| 372 |
-
by `T`.
|
| 373 |
|
| 374 |
If the type of `v` is the same as `T`, or it is the same as `T` except
|
| 375 |
that the class object type in `T` is more cv-qualified than the class
|
| 376 |
object type in `v`, the result is `v` (converted if necessary).
|
| 377 |
|
|
@@ -381,33 +431,35 @@ result is the null pointer value of type `T`.
|
|
| 381 |
If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
| 382 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 383 |
unique `B` subobject of the `D` object pointed to by `v`. Similarly, if
|
| 384 |
`T` is “reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B`
|
| 385 |
is a base class of `D`, the result is the unique `B` subobject of the
|
| 386 |
-
`D` object referred to by `v`.
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
|
| 392 |
``` cpp
|
| 393 |
struct B { };
|
| 394 |
struct D : B { };
|
| 395 |
void foo(D* dp) {
|
| 396 |
B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
|
| 397 |
}
|
| 398 |
```
|
| 399 |
|
|
|
|
|
|
|
| 400 |
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
|
| 401 |
type ([[class.virtual]]).
|
| 402 |
|
| 403 |
-
If `T` is “pointer to *cv* `void`
|
| 404 |
-
most derived object pointed to by `v`. Otherwise, a
|
| 405 |
applied to see if the object pointed or referred to by `v` can be
|
| 406 |
converted to the type pointed or referred to by `T`.
|
| 407 |
|
| 408 |
-
If `C` is the class type to which `T` points or refers, the
|
| 409 |
check logically executes as follows:
|
| 410 |
|
| 411 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 412 |
points (refers) to a `public` base class subobject of a `C` object,
|
| 413 |
and if only one object of type `C` is derived from the subobject
|
|
@@ -416,17 +468,19 @@ check logically executes as follows:
|
|
| 416 |
- Otherwise, if `v` points (refers) to a `public` base class subobject
|
| 417 |
of the most derived object, and the type of the most derived object
|
| 418 |
has a base class, of type `C`, that is unambiguous and `public`, the
|
| 419 |
result points (refers) to the `C` subobject of the most derived
|
| 420 |
object.
|
| 421 |
-
- Otherwise, the
|
| 422 |
|
| 423 |
The value of a failed cast to pointer type is the null pointer value of
|
| 424 |
the required result type. A failed cast to reference type throws an
|
| 425 |
exception ([[except.throw]]) of a type that would match a handler (
|
| 426 |
[[except.handle]]) of type `std::bad_cast` ([[bad.cast]]).
|
| 427 |
|
|
|
|
|
|
|
| 428 |
``` cpp
|
| 429 |
class A { virtual void f(); };
|
| 430 |
class B { virtual void g(); };
|
| 431 |
class D : public virtual A, private B { };
|
| 432 |
void g() {
|
|
@@ -435,27 +489,28 @@ void g() {
|
|
| 435 |
A* ap = &d; // public derivation, no cast needed
|
| 436 |
D& dr = dynamic_cast<D&>(*bp); // fails
|
| 437 |
ap = dynamic_cast<A*>(bp); // fails
|
| 438 |
bp = dynamic_cast<B*>(ap); // fails
|
| 439 |
ap = dynamic_cast<A*>(&d); // succeeds
|
| 440 |
-
bp = dynamic_cast<B*>(&d); // ill-formed (not a
|
| 441 |
}
|
| 442 |
|
| 443 |
class E : public D, public B { };
|
| 444 |
class F : public E, public D { };
|
| 445 |
void h() {
|
| 446 |
F f;
|
| 447 |
A* ap = &f; // succeeds: finds unique A
|
| 448 |
-
D* dp = dynamic_cast<D*>(ap); // fails: yields
|
| 449 |
-
// f has two D subobjects
|
| 450 |
E* ep = (E*)ap; // ill-formed: cast from virtual base
|
| 451 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 452 |
}
|
| 453 |
```
|
| 454 |
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
| 457 |
|
| 458 |
### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 459 |
|
| 460 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 461 |
`std::type_info` ([[type.info]]) and dynamic type `const`
|
|
@@ -479,122 +534,146 @@ value ([[conv.ptr]]), the `typeid` expression throws an exception (
|
|
| 479 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 480 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 481 |
representing the static type of the expression. Lvalue-to-rvalue (
|
| 482 |
[[conv.lval]]), array-to-pointer ([[conv.array]]), and
|
| 483 |
function-to-pointer ([[conv.func]]) conversions are not applied to the
|
| 484 |
-
expression. If the
|
| 485 |
-
|
| 486 |
-
(Clause [[expr]]).
|
| 487 |
|
| 488 |
When `typeid` is applied to a *type-id*, the result refers to a
|
| 489 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 490 |
-
type of the *type-id* is a reference to a possibly
|
| 491 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 492 |
-
object representing the
|
| 493 |
the *type-id* is a class type or a reference to a class type, the class
|
| 494 |
shall be completely-defined.
|
| 495 |
|
| 496 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
| 497 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 498 |
representing the cv-unqualified type.
|
| 499 |
|
|
|
|
|
|
|
| 500 |
``` cpp
|
| 501 |
-
class D {
|
| 502 |
D d1;
|
| 503 |
const D d2;
|
| 504 |
|
| 505 |
typeid(d1) == typeid(d2); // yields true
|
| 506 |
typeid(D) == typeid(const D); // yields true
|
| 507 |
typeid(D) == typeid(d2); // yields true
|
| 508 |
typeid(D) == typeid(const D&); // yields true
|
| 509 |
```
|
| 510 |
|
|
|
|
|
|
|
| 511 |
If the header `<typeinfo>` ([[type.info]]) is not included prior to a
|
| 512 |
use of `typeid`, the program is ill-formed.
|
| 513 |
|
| 514 |
-
[[class.cdtor]] describes the behavior of `typeid` applied
|
| 515 |
-
under construction or destruction.
|
| 516 |
|
| 517 |
### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
| 518 |
|
| 519 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 520 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 521 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 522 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 523 |
otherwise, the result is a prvalue. The `static_cast` operator shall not
|
| 524 |
cast away constness ([[expr.const.cast]]).
|
| 525 |
|
| 526 |
-
An lvalue of type “*cv1* `B`
|
| 527 |
-
type “reference to *cv2* `D`
|
| 528 |
-
[[class.derived]]) from `B`, if
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
|
|
|
|
|
|
| 538 |
|
| 539 |
``` cpp
|
| 540 |
struct B { };
|
| 541 |
struct D : public B { };
|
| 542 |
D d;
|
| 543 |
B &br = d;
|
| 544 |
|
| 545 |
static_cast<D&>(br); // produces lvalue to the original d object
|
| 546 |
```
|
| 547 |
|
| 548 |
-
|
| 549 |
-
cast to type “rvalue reference to *cv2* `T2`” if “*cv2* `T2`” is
|
| 550 |
-
reference-compatible with “*cv1* `T1`” ([[dcl.init.ref]]). If the value
|
| 551 |
-
is not a bit-field, the result refers to the object or the specified
|
| 552 |
-
base class subobject thereof; otherwise, the lvalue-to-rvalue
|
| 553 |
-
conversion ([[conv.lval]]) is applied to the bit-field and the
|
| 554 |
-
resulting prvalue is used as the *expression* of the `static_cast` for
|
| 555 |
-
the remainder of this section. If `T2` is an inaccessible (Clause
|
| 556 |
-
[[class.access]]) or ambiguous ([[class.member.lookup]]) base class of
|
| 557 |
-
`T1`, a program that necessitates such a cast is ill-formed.
|
| 558 |
|
| 559 |
-
An
|
| 560 |
-
`
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
|
| 568 |
Otherwise, the `static_cast` shall perform one of the conversions listed
|
| 569 |
below. No other conversion shall be performed explicitly using a
|
| 570 |
`static_cast`.
|
| 571 |
|
| 572 |
Any expression can be explicitly converted to type cv `void`, in which
|
| 573 |
case it becomes a discarded-value expression (Clause [[expr]]).
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
the
|
| 577 |
-
|
|
|
|
| 578 |
|
| 579 |
The inverse of any standard conversion sequence (Clause [[conv]]) not
|
| 580 |
containing an lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 581 |
[[conv.array]]), function-to-pointer ([[conv.func]]), null pointer (
|
| 582 |
-
[[conv.ptr]]), null member pointer ([[conv.mem]]),
|
| 583 |
-
[[conv.bool]])
|
| 584 |
-
`static_cast`. A program is ill-formed if
|
| 585 |
-
perform the inverse of an ill-formed standard
|
|
|
|
|
|
|
|
|
|
| 586 |
|
| 587 |
``` cpp
|
| 588 |
struct B { };
|
| 589 |
struct D : private B { };
|
| 590 |
void f() {
|
| 591 |
-
static_cast<D*>((B*)0); //
|
| 592 |
-
static_cast<int B::*>((int D::*)0); //
|
| 593 |
}
|
| 594 |
```
|
| 595 |
|
|
|
|
|
|
|
| 596 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 597 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) conversions
|
| 598 |
are applied to the operand. Such a `static_cast` is subject to the
|
| 599 |
restriction that the explicit conversion does not cast away constness (
|
| 600 |
[[expr.const.cast]]), and the following additional rules for specific
|
|
@@ -608,65 +687,72 @@ unchanged if the original value can be represented by the specified
|
|
| 608 |
type. Otherwise, the resulting value is unspecified. A value of a scoped
|
| 609 |
enumeration type can also be explicitly converted to a floating-point
|
| 610 |
type; the result is the same as that of converting from the original
|
| 611 |
value to the floating-point type.
|
| 612 |
|
| 613 |
-
A value of integral or enumeration type can be explicitly converted to
|
| 614 |
-
|
| 615 |
-
within the range of the enumeration values ([[dcl.enum]]).
|
| 616 |
-
the
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
|
| 622 |
-
A prvalue of type “pointer to *cv1* `B`
|
| 623 |
-
be converted to a prvalue of type “pointer to *cv2* `D`
|
| 624 |
-
class derived (Clause [[class.derived]]) from `B`, if
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
`
|
| 629 |
-
value ([[conv.ptr]]) is converted to the null pointer
|
| 630 |
-
destination type. If the prvalue of type “pointer to *cv1*
|
| 631 |
-
to a `B` that is actually a subobject of an object of type
|
| 632 |
-
resulting pointer points to the enclosing object of type `D`.
|
| 633 |
-
the behavior is undefined.
|
| 634 |
|
| 635 |
A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
|
| 636 |
-
converted to a prvalue of type “pointer to member of `B`
|
| 637 |
-
`T`, where `B` is a base class (Clause [[class.derived]]) of `D`, if
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
to the null member pointer value of
|
| 643 |
-
contains the original member, or is a
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
| 651 |
-
prvalue of type “pointer to *cv2* `T`
|
| 652 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 653 |
-
*cv1*.
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
|
|
|
|
|
|
| 661 |
|
| 662 |
``` cpp
|
| 663 |
T* p1 = new T;
|
| 664 |
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
|
| 665 |
bool b = p1 == p2; // b will have the value true.
|
| 666 |
```
|
| 667 |
|
|
|
|
|
|
|
| 668 |
### Reinterpret cast <a id="expr.reinterpret.cast">[[expr.reinterpret.cast]]</a>
|
| 669 |
|
| 670 |
The result of the expression `reinterpret_cast<T>(v)` is the result of
|
| 671 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 672 |
type or an rvalue reference to function type, the result is an lvalue;
|
|
@@ -681,61 +767,77 @@ performed explicitly using `reinterpret_cast`.
|
|
| 681 |
The `reinterpret_cast` operator shall not cast away constness (
|
| 682 |
[[expr.const.cast]]). An expression of integral, enumeration, pointer,
|
| 683 |
or pointer-to-member type can be explicitly converted to its own type;
|
| 684 |
such a cast yields the value of its operand.
|
| 685 |
|
| 686 |
-
The mapping performed by `reinterpret_cast` might, or might
|
| 687 |
-
a representation different from the original
|
|
|
|
| 688 |
|
| 689 |
A pointer can be explicitly converted to any integral type large enough
|
| 690 |
-
to hold it. The mapping function is implementation-defined.
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
`
|
| 696 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
|
| 698 |
A value of integral type or enumeration type can be explicitly converted
|
| 699 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 700 |
any such exists on the implementation) and back to the same pointer type
|
| 701 |
will have its original value; mappings between pointers and integers are
|
| 702 |
-
otherwise *implementation-defined*.
|
| 703 |
-
|
| 704 |
-
|
|
|
|
|
|
|
| 705 |
|
| 706 |
A function pointer can be explicitly converted to a function pointer of
|
| 707 |
-
a different type.
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
|
| 716 |
An object pointer can be explicitly converted to an object pointer of a
|
| 717 |
different type.[^12] When a prvalue `v` of object pointer type is
|
| 718 |
converted to the object pointer type “pointer to cv `T`”, the result is
|
| 719 |
-
`static_cast<cv T*>(static_cast<cv
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
|
|
|
|
|
|
| 724 |
|
| 725 |
Converting a function pointer to an object pointer type or vice versa is
|
| 726 |
conditionally-supported. The meaning of such a conversion is
|
| 727 |
*implementation-defined*, except that if an implementation supports
|
| 728 |
conversions in both directions, converting a prvalue of one type to the
|
| 729 |
other type and back, possibly with different cv-qualification, shall
|
| 730 |
yield the original pointer value.
|
| 731 |
|
| 732 |
The null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 733 |
-
value of the destination type.
|
| 734 |
-
|
| 735 |
-
pointer constant of
|
| 736 |
-
pointer
|
|
|
|
|
|
|
| 737 |
|
| 738 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 739 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 740 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 741 |
object types.[^13] The null member pointer value ([[conv.mem]]) is
|
|
@@ -753,16 +855,20 @@ result of this conversion is unspecified, except in the following cases:
|
|
| 753 |
|
| 754 |
A glvalue expression of type `T1` can be cast to the type “reference to
|
| 755 |
`T2`” if an expression of type “pointer to `T1`” can be explicitly
|
| 756 |
converted to the type “pointer to `T2`” using a `reinterpret_cast`. The
|
| 757 |
result refers to the same object as the source glvalue, but with the
|
| 758 |
-
specified type.
|
|
|
|
|
|
|
| 759 |
`reinterpret_cast<T&>(x)` has the same effect as the conversion
|
| 760 |
`*reinterpret_cast<T*>(&x)` with the built-in `&` and `*` operators (and
|
| 761 |
-
similarly for `reinterpret_cast<T&&>(x)`).
|
| 762 |
-
|
| 763 |
-
|
|
|
|
|
|
|
| 764 |
|
| 765 |
### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 766 |
|
| 767 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 768 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
|
@@ -772,22 +878,30 @@ otherwise, the result is a prvalue and the lvalue-to-rvalue (
|
|
| 772 |
function-to-pointer ([[conv.func]]) standard conversions are performed
|
| 773 |
on the expression `v`. Conversions that can be performed explicitly
|
| 774 |
using `const_cast` are listed below. No other conversion shall be
|
| 775 |
performed explicitly using `const_cast`.
|
| 776 |
|
| 777 |
-
Subject to the restrictions in this section, an expression
|
| 778 |
-
to its own type using a `const_cast`
|
|
|
|
| 779 |
|
| 780 |
-
For two
|
|
|
|
|
|
|
| 781 |
|
| 782 |
-
|
| 783 |
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
For two object types `T1` and `T2`, if a pointer to `T1` can be
|
| 791 |
explicitly converted to the type “pointer to `T2`” using a `const_cast`,
|
| 792 |
then the following conversions can also be made:
|
| 793 |
|
|
@@ -796,60 +910,41 @@ then the following conversions can also be made:
|
|
| 796 |
- a glvalue of type `T1` can be explicitly converted to an xvalue of
|
| 797 |
type `T2` using the cast `const_cast<T2&&>`; and
|
| 798 |
- if `T1` is a class type, a prvalue of type `T1` can be explicitly
|
| 799 |
converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
|
| 800 |
|
| 801 |
-
The result of a reference `const_cast` refers to the original object
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
pointers to data members and multi-level mixed pointers and pointers to
|
| 805 |
-
data members ([[conv.qual]]), the rules for `const_cast` are the same
|
| 806 |
-
as those used for pointers; the “member” aspect of a pointer to member
|
| 807 |
-
is ignored when determining where the cv-qualifiers are added or removed
|
| 808 |
-
by the `const_cast`. The result of a pointer to data member `const_cast`
|
| 809 |
-
refers to the same member as the original (uncast) pointer to data
|
| 810 |
-
member.
|
| 811 |
|
| 812 |
A null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 813 |
value of the destination type. The null member pointer value (
|
| 814 |
[[conv.mem]]) is converted to the null member pointer value of the
|
| 815 |
destination type.
|
| 816 |
|
| 817 |
-
Depending on the type of the object, a write operation
|
| 818 |
-
pointer, lvalue or pointer to data member resulting from a
|
| 819 |
-
that casts away a const-qualifier[^15] may produce
|
| 820 |
-
[[dcl.type.cv]]).
|
| 821 |
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
|
|
|
| 825 |
|
| 826 |
-
|
| 827 |
-
type `T` there does not exist an implicit conversion (Clause [[conv]])
|
| 828 |
-
from:
|
| 829 |
-
|
| 830 |
-
to
|
| 831 |
|
| 832 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 833 |
lvalue reference cast or casting from an expression of type `T1` to an
|
| 834 |
xvalue of type `T2` using an rvalue reference cast casts away constness
|
| 835 |
if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
|
| 836 |
to `T2`” casts away constness.
|
| 837 |
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
pointer to
|
| 845 |
-
|
| 846 |
-
been cast away.
|
| 847 |
-
|
| 848 |
-
some conversions which involve only changes in cv-qualification cannot
|
| 849 |
-
be done using `const_cast.` For instance, conversions between pointers
|
| 850 |
-
to functions are not covered because such conversions lead to values
|
| 851 |
-
whose use causes undefined behavior. For the same reasons, conversions
|
| 852 |
-
between pointers to member functions, and in particular, the conversion
|
| 853 |
-
from a pointer to a const member function to a pointer to a non-const
|
| 854 |
-
member function, are not covered.
|
| 855 |
|
|
|
|
| 3 |
Postfix expressions group left-to-right.
|
| 4 |
|
| 5 |
``` bnf
|
| 6 |
postfix-expression:
|
| 7 |
primary-expression
|
| 8 |
+
postfix-expression '[' expr-or-braced-init-list ']'
|
|
|
|
| 9 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 10 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 11 |
typename-specifier '(' expression-listₒₚₜ ')'
|
| 12 |
simple-type-specifier braced-init-list
|
| 13 |
typename-specifier braced-init-list
|
|
|
|
| 32 |
|
| 33 |
``` bnf
|
| 34 |
pseudo-destructor-name:
|
| 35 |
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 36 |
nested-name-specifier 'template' simple-template-id ':: ~' type-name
|
| 37 |
+
'~' type-name
|
| 38 |
'~' decltype-specifier
|
| 39 |
```
|
| 40 |
|
| 41 |
+
[*Note 1*: The `>` token following the *type-id* in a `dynamic_cast`,
|
| 42 |
+
`static_cast`, `reinterpret_cast`, or `const_cast` may be the product of
|
| 43 |
+
replacing a `>{>}` token by two consecutive `>` tokens (
|
| 44 |
+
[[temp.names]]). — *end note*]
|
| 45 |
|
| 46 |
### Subscripting <a id="expr.sub">[[expr.sub]]</a>
|
| 47 |
|
| 48 |
A postfix expression followed by an expression in square brackets is a
|
| 49 |
+
postfix expression. One of the expressions shall be a glvalue of type
|
| 50 |
+
“array of `T`” or a prvalue of type “pointer to `T`” and the other shall
|
| 51 |
+
be a prvalue of unscoped enumeration or integral type. The result is of
|
| 52 |
+
type “`T`”. The type “`T`” shall be a completely-defined object
|
| 53 |
+
type.[^5] The expression `E1[E2]` is identical (by definition) to
|
| 54 |
+
`*((E1)+(E2))`
|
| 55 |
+
|
| 56 |
+
[*Note 1*: see [[expr.unary]] and [[expr.add]] for details of `*` and
|
| 57 |
+
`+` and [[dcl.array]] for details of arrays. — *end note*]
|
| 58 |
+
|
| 59 |
+
, except that in the case of an array operand, the result is an lvalue
|
| 60 |
+
if that operand is an lvalue and an xvalue otherwise. The expression
|
| 61 |
+
`E1` is sequenced before the expression `E2`.
|
| 62 |
|
| 63 |
A *braced-init-list* shall not be used with the built-in subscript
|
| 64 |
operator.
|
| 65 |
|
| 66 |
### Function call <a id="expr.call">[[expr.call]]</a>
|
| 67 |
|
| 68 |
A function call is a postfix expression followed by parentheses
|
| 69 |
containing a possibly empty, comma-separated list of
|
| 70 |
*initializer-clause*s which constitute the arguments to the function.
|
| 71 |
+
The postfix expression shall have function type or function pointer
|
| 72 |
type. For a call to a non-member function or to a static member
|
| 73 |
function, the postfix expression shall be either an lvalue that refers
|
| 74 |
to a function (in which case the function-to-pointer standard
|
| 75 |
conversion ([[conv.func]]) is suppressed on the postfix expression), or
|
| 76 |
+
it shall have function pointer type. Calling a function through an
|
| 77 |
+
expression whose function type is different from the function type of
|
| 78 |
+
the called function’s definition results in undefined behavior (
|
| 79 |
+
[[dcl.link]]). For a call to a non-static member function, the postfix
|
| 80 |
+
expression shall be an implicit ([[class.mfct.non-static]],
|
| 81 |
+
[[class.static]]) or explicit class member access ([[expr.ref]]) whose
|
| 82 |
+
*id-expression* is a function member name, or a pointer-to-member
|
| 83 |
+
expression ([[expr.mptr.oper]]) selecting a function member; the call
|
| 84 |
+
is as a member of the class object referred to by the object expression.
|
| 85 |
+
In the case of an implicit class member access, the implied object is
|
| 86 |
+
the one pointed to by `this`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
[*Note 1*: A member function call of the form `f()` is interpreted as
|
| 89 |
+
`(*this).f()` (see [[class.mfct.non-static]]). — *end note*]
|
| 90 |
+
|
| 91 |
+
If a function or member function name is used, the name can be
|
| 92 |
+
overloaded (Clause [[over]]), in which case the appropriate function
|
| 93 |
+
shall be selected according to the rules in [[over.match]]. If the
|
| 94 |
+
selected function is non-virtual, or if the *id-expression* in the class
|
| 95 |
+
member access expression is a *qualified-id*, that function is called.
|
| 96 |
+
Otherwise, its final overrider ([[class.virtual]]) in the dynamic type
|
| 97 |
+
of the object expression is called; such a call is referred to as a
|
| 98 |
+
*virtual function call*.
|
| 99 |
+
|
| 100 |
+
[*Note 2*: The dynamic type is the type of the object referred to by
|
| 101 |
+
the current value of the object expression. [[class.cdtor]] describes
|
| 102 |
+
the behavior of virtual function calls when the object expression refers
|
| 103 |
+
to an object under construction or destruction. — *end note*]
|
| 104 |
+
|
| 105 |
+
[*Note 3*: If a function or member function name is used, and name
|
| 106 |
+
lookup ([[basic.lookup]]) does not find a declaration of that name, the
|
| 107 |
+
program is ill-formed. No function is implicitly declared by such a
|
| 108 |
+
call. — *end note*]
|
| 109 |
|
| 110 |
If the *postfix-expression* designates a destructor ([[class.dtor]]),
|
| 111 |
the type of the function call expression is `void`; otherwise, the type
|
| 112 |
of the function call expression is the return type of the statically
|
| 113 |
chosen function (i.e., ignoring the `virtual` keyword), even if the type
|
| 114 |
of the function actually called is different. This return type shall be
|
| 115 |
+
an object type, a reference type or cv `void`.
|
| 116 |
|
| 117 |
When a function is called, each parameter ([[dcl.fct]]) shall be
|
| 118 |
initialized ([[dcl.init]], [[class.copy]], [[class.ctor]]) with its
|
| 119 |
+
corresponding argument. If the function is a non-static member function,
|
| 120 |
+
the `this` parameter of the function ([[class.this]]) shall be
|
| 121 |
+
initialized with a pointer to the object of the call, converted as if by
|
| 122 |
+
an explicit type conversion ([[expr.cast]]).
|
| 123 |
+
|
| 124 |
+
[*Note 4*: There is no access or ambiguity checking on this conversion;
|
| 125 |
+
the access checking and disambiguation are done as part of the (possibly
|
| 126 |
+
implicit) class member access operator. See [[class.member.lookup]],
|
| 127 |
+
[[class.access.base]], and [[expr.ref]]. — *end note*]
|
| 128 |
+
|
| 129 |
+
When a function is called, the parameters that have object type shall
|
| 130 |
+
have completely-defined object type.
|
| 131 |
+
|
| 132 |
+
[*Note 5*: this still allows a parameter to be a pointer or reference
|
| 133 |
+
to an incomplete class type. However, it prevents a passed-by-value
|
| 134 |
+
parameter to have an incomplete class type. — *end note*]
|
| 135 |
+
|
| 136 |
+
It is *implementation-defined* whether the lifetime of a parameter ends
|
| 137 |
+
when the function in which it is defined returns or at the end of the
|
| 138 |
+
enclosing full-expression. The initialization and destruction of each
|
| 139 |
+
parameter occurs within the context of the calling function.
|
| 140 |
+
|
| 141 |
+
[*Example 1*: The access of the constructor, conversion functions or
|
| 142 |
+
destructor is checked at the point of call in the calling function. If a
|
| 143 |
+
constructor or destructor for a function parameter throws an exception,
|
| 144 |
+
the search for a handler starts in the scope of the calling function; in
|
| 145 |
+
particular, if the function called has a *function-try-block* (Clause
|
| 146 |
+
[[except]]) with a handler that could handle the exception, this handler
|
| 147 |
+
is not considered. — *end example*]
|
| 148 |
+
|
| 149 |
+
The *postfix-expression* is sequenced before each *expression* in the
|
| 150 |
+
*expression-list* and any default argument. The initialization of a
|
| 151 |
+
parameter, including every associated value computation and side effect,
|
| 152 |
+
is indeterminately sequenced with respect to that of any other
|
| 153 |
+
parameter.
|
| 154 |
+
|
| 155 |
+
[*Note 6*: All side effects of argument evaluations are sequenced
|
| 156 |
+
before the function is entered (see
|
| 157 |
+
[[intro.execution]]). — *end note*]
|
| 158 |
+
|
| 159 |
+
[*Example 2*:
|
| 160 |
+
|
| 161 |
+
``` cpp
|
| 162 |
+
void f() {
|
| 163 |
+
std::string s = "but I have heard it works even if you don't believe in it";
|
| 164 |
+
s.replace(0, 4, "").replace(s.find("even"), 4, "only").replace(s.find(" don't"), 6, "");
|
| 165 |
+
assert(s == "I have heard it works only if you believe in it"); // OK
|
| 166 |
+
}
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
— *end example*]
|
| 170 |
+
|
| 171 |
+
[*Note 7*: If an operator function is invoked using operator notation,
|
| 172 |
+
argument evaluation is sequenced as specified for the built-in operator;
|
| 173 |
+
see [[over.match.oper]]. — *end note*]
|
| 174 |
+
|
| 175 |
+
[*Example 3*:
|
| 176 |
+
|
| 177 |
+
``` cpp
|
| 178 |
+
struct S {
|
| 179 |
+
S(int);
|
| 180 |
+
};
|
| 181 |
+
int operator<<(S, int);
|
| 182 |
+
int i, j;
|
| 183 |
+
int x = S(i=1) << (i=2);
|
| 184 |
+
int y = operator<<(S(j=1), j=2);
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
After performing the initializations, the value of `i` is 2 (see
|
| 188 |
+
[[expr.shift]]), but it is unspecified whether the value of `j` is 1 or
|
| 189 |
+
2.
|
| 190 |
+
|
| 191 |
+
— *end example*]
|
| 192 |
+
|
| 193 |
+
The result of a function call is the result of the operand of the
|
| 194 |
+
evaluated `return` statement ([[stmt.return]]) in the called function
|
| 195 |
+
(if any), except in a virtual function call if the return type of the
|
| 196 |
+
final overrider is different from the return type of the statically
|
| 197 |
+
chosen function, the value returned from the final overrider is
|
| 198 |
+
converted to the return type of the statically chosen function.
|
| 199 |
+
|
| 200 |
+
[*Note 8*: A function can change the values of its non-const
|
| 201 |
+
parameters, but these changes cannot affect the values of the arguments
|
| 202 |
+
except where a parameter is of a reference type ([[dcl.ref]]); if the
|
| 203 |
+
reference is to a const-qualified type, `const_cast` is required to be
|
| 204 |
+
used to cast away the constness in order to modify the argument’s value.
|
| 205 |
+
Where a parameter is of `const` reference type a temporary object is
|
| 206 |
+
introduced if needed ([[dcl.type]], [[lex.literal]], [[lex.string]],
|
| 207 |
[[dcl.array]], [[class.temporary]]). In addition, it is possible to
|
| 208 |
+
modify the values of non-constant objects through pointer
|
| 209 |
+
parameters. — *end note*]
|
| 210 |
|
| 211 |
A function can be declared to accept fewer arguments (by declaring
|
| 212 |
default arguments ([[dcl.fct.default]])) or more arguments (by using
|
| 213 |
the ellipsis, `...`, or a function parameter pack ([[dcl.fct]])) than
|
| 214 |
the number of parameters in the function definition ([[dcl.fct.def]]).
|
| 215 |
+
|
| 216 |
+
[*Note 9*: This implies that, except where the ellipsis (`...`) or a
|
| 217 |
+
function parameter pack is used, a parameter is available for each
|
| 218 |
+
argument. — *end note*]
|
| 219 |
|
| 220 |
When there is no parameter for a given argument, the argument is passed
|
| 221 |
in such a way that the receiving function can obtain the value of the
|
| 222 |
+
argument by invoking `va_arg` ([[support.runtime]]).
|
| 223 |
+
|
| 224 |
+
[*Note 10*: This paragraph does not apply to arguments passed to a
|
| 225 |
+
function parameter pack. Function parameter packs are expanded during
|
| 226 |
+
template instantiation ([[temp.variadic]]), thus each such argument has
|
| 227 |
+
a corresponding parameter when a function template specialization is
|
| 228 |
+
actually called. — *end note*]
|
| 229 |
+
|
| 230 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 231 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) standard
|
| 232 |
conversions are performed on the argument expression. An argument that
|
| 233 |
+
has type cv `std::nullptr_t` is converted to type `void*` (
|
| 234 |
+
[[conv.ptr]]). After these conversions, if the argument does not have
|
| 235 |
+
arithmetic, enumeration, pointer, pointer to member, or class type, the
|
| 236 |
+
program is ill-formed. Passing a potentially-evaluated argument of class
|
| 237 |
+
type (Clause [[class]]) having a non-trivial copy constructor, a
|
| 238 |
+
non-trivial move constructor, or a non-trivial destructor, with no
|
| 239 |
+
corresponding parameter, is conditionally-supported with
|
| 240 |
+
*implementation-defined* semantics. If the argument has integral or
|
| 241 |
enumeration type that is subject to the integral promotions (
|
| 242 |
+
[[conv.prom]]), or a floating-point type that is subject to the
|
| 243 |
+
floating-point promotion ([[conv.fpprom]]), the value of the argument
|
| 244 |
+
is converted to the promoted type before the call. These promotions are
|
| 245 |
referred to as the *default argument promotions*.
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
Recursive calls are permitted, except to the `main` function (
|
| 248 |
[[basic.start.main]]).
|
| 249 |
|
| 250 |
A function call is an lvalue if the result type is an lvalue reference
|
| 251 |
type or an rvalue reference to function type, an xvalue if the result
|
| 252 |
type is an rvalue reference to object type, and a prvalue otherwise.
|
| 253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
### Explicit type conversion (functional notation) <a id="expr.type.conv">[[expr.type.conv]]</a>
|
| 255 |
|
| 256 |
A *simple-type-specifier* ([[dcl.type.simple]]) or
|
| 257 |
*typename-specifier* ([[temp.res]]) followed by a parenthesized
|
| 258 |
+
optional *expression-list* or by a *braced-init-list* (the initializer)
|
| 259 |
+
constructs a value of the specified type given the initializer. If the
|
| 260 |
+
type is a placeholder for a deduced class type, it is replaced by the
|
| 261 |
+
return type of the function selected by overload resolution for class
|
| 262 |
+
template deduction ([[over.match.class.deduct]]) for the remainder of
|
| 263 |
+
this section.
|
| 264 |
+
|
| 265 |
+
If the initializer is a parenthesized single expression, the type
|
| 266 |
conversion expression is equivalent (in definedness, and if defined in
|
| 267 |
meaning) to the corresponding cast expression ([[expr.cast]]). If the
|
| 268 |
+
type is cv `void` and the initializer is `()`, the expression is a
|
| 269 |
+
prvalue of the specified type that performs no initialization.
|
| 270 |
+
Otherwise, the expression is a prvalue of the specified type whose
|
| 271 |
+
result object is direct-initialized ([[dcl.init]]) with the
|
| 272 |
+
initializer. For an expression of the form `T()`, `T` shall not be an
|
| 273 |
+
array type.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
### Pseudo destructor call <a id="expr.pseudo">[[expr.pseudo]]</a>
|
| 276 |
|
| 277 |
The use of a *pseudo-destructor-name* after a dot `.` or arrow `->`
|
| 278 |
operator represents the destructor for the non-class type denoted by
|
|
|
|
| 290 |
|
| 291 |
``` bnf
|
| 292 |
nested-name-specifierₒₚₜ type-name ':: ~' type-name
|
| 293 |
```
|
| 294 |
|
| 295 |
+
shall designate the same scalar type (ignoring cv-qualification).
|
| 296 |
|
| 297 |
### Class member access <a id="expr.ref">[[expr.ref]]</a>
|
| 298 |
|
| 299 |
A postfix expression followed by a dot `.` or an arrow `->`, optionally
|
| 300 |
followed by the keyword `template` ([[temp.names]]), and then followed
|
| 301 |
by an *id-expression*, is a postfix expression. The postfix expression
|
| 302 |
before the dot or arrow is evaluated;[^6] the result of that evaluation,
|
| 303 |
together with the *id-expression*, determines the result of the entire
|
| 304 |
postfix expression.
|
| 305 |
|
| 306 |
+
For the first option (dot) the first expression shall be a glvalue
|
| 307 |
+
having complete class type. For the second option (arrow) the first
|
| 308 |
+
expression shall be a prvalue having pointer to complete class type. The
|
| 309 |
+
expression `E1->E2` is converted to the equivalent form `(*(E1)).E2`;
|
| 310 |
+
the remainder of [[expr.ref]] will address only the first option
|
| 311 |
+
(dot).[^7] In either case, the *id-expression* shall name a member of
|
| 312 |
+
the class or of one of its base classes.
|
| 313 |
+
|
| 314 |
+
[*Note 1*: Because the name of a class is inserted in its class scope
|
| 315 |
+
(Clause [[class]]), the name of a class is also considered a nested
|
| 316 |
+
member of that class. — *end note*]
|
| 317 |
+
|
| 318 |
+
[*Note 2*: [[basic.lookup.classref]] describes how names are looked up
|
| 319 |
+
after the `.` and `->` operators. — *end note*]
|
| 320 |
|
| 321 |
Abbreviating *postfix-expression.id-expression* as `E1.E2`, `E1` is
|
| 322 |
+
called the *object expression*. If `E2` is a bit-field, `E1.E2` is a
|
| 323 |
+
bit-field. The type and value category of `E1.E2` are determined as
|
| 324 |
+
follows. In the remainder of [[expr.ref]], *cq* represents either
|
| 325 |
+
`const` or the absence of `const` and *vq* represents either `volatile`
|
| 326 |
+
or the absence of `volatile`. *cv* represents an arbitrary set of
|
| 327 |
+
cv-qualifiers, as defined in [[basic.type.qualifier]].
|
| 328 |
|
| 329 |
+
If `E2` is declared to have type “reference to `T`”, then `E1.E2` is an
|
| 330 |
lvalue; the type of `E1.E2` is `T`. Otherwise, one of the following
|
| 331 |
rules applies.
|
| 332 |
|
| 333 |
- If `E2` is a static data member and the type of `E2` is `T`, then
|
| 334 |
`E1.E2` is an lvalue; the expression designates the named member of
|
|
|
|
| 353 |
lvalue; the expression designates the static member function. The
|
| 354 |
type of `E1.E2` is the same type as that of `E2`, namely “function
|
| 355 |
of parameter-type-list returning `T`”.
|
| 356 |
- Otherwise, if `E1.E2` refers to a non-static member function and the
|
| 357 |
type of `E2` is “function of parameter-type-list *cv*
|
| 358 |
+
*ref-qualifier*ₒₚₜ returning `T`”, then `E1.E2` is a prvalue. The
|
| 359 |
expression designates a non-static member function. The expression
|
| 360 |
can be used only as the left-hand operand of a member function
|
| 361 |
+
call ([[class.mfct]]). \[*Note 3*: Any redundant set of parentheses
|
| 362 |
+
surrounding the expression is ignored (
|
| 363 |
+
[[expr.prim]]). — *end note*] The type of `E1.E2` is “function of
|
| 364 |
+
parameter-type-list *cv* returning `T`”.
|
| 365 |
- If `E2` is a nested type, the expression `E1.E2` is ill-formed.
|
| 366 |
- If `E2` is a member enumerator and the type of `E2` is `T`, the
|
| 367 |
expression `E1.E2` is a prvalue. The type of `E1.E2` is `T`.
|
| 368 |
|
| 369 |
If `E2` is a non-static data member or a non-static member function, the
|
| 370 |
program is ill-formed if the class of which `E2` is directly a member is
|
| 371 |
an ambiguous base ([[class.member.lookup]]) of the naming class (
|
| 372 |
+
[[class.access.base]]) of `E2`.
|
| 373 |
+
|
| 374 |
+
[*Note 4*: The program is also ill-formed if the naming class is an
|
| 375 |
+
ambiguous base of the class type of the object expression; see
|
| 376 |
+
[[class.access.base]]. — *end note*]
|
| 377 |
|
| 378 |
### Increment and decrement <a id="expr.post.incr">[[expr.post.incr]]</a>
|
| 379 |
|
| 380 |
+
The value of a postfix `++` expression is the value of its operand.
|
| 381 |
+
|
| 382 |
+
[*Note 1*: The value obtained is a copy of the original
|
| 383 |
+
value — *end note*]
|
| 384 |
+
|
| 385 |
+
The operand shall be a modifiable lvalue. The type of the operand shall
|
| 386 |
+
be an arithmetic type other than cv `bool`, or a pointer to a complete
|
| 387 |
+
object type. The value of the operand object is modified by adding `1`
|
| 388 |
+
to it. The value computation of the `++` expression is sequenced before
|
| 389 |
+
the modification of the operand object. With respect to an
|
| 390 |
indeterminately-sequenced function call, the operation of postfix `++`
|
| 391 |
+
is a single evaluation.
|
| 392 |
+
|
| 393 |
+
[*Note 2*: Therefore, a function call shall not intervene between the
|
| 394 |
+
lvalue-to-rvalue conversion and the side effect associated with any
|
| 395 |
+
single postfix ++ operator. — *end note*]
|
| 396 |
+
|
| 397 |
+
The result is a prvalue. The type of the result is the cv-unqualified
|
| 398 |
+
version of the type of the operand. If the operand is a bit-field that
|
| 399 |
+
cannot represent the incremented value, the resulting value of the
|
| 400 |
+
bit-field is *implementation-defined*. See also [[expr.add]] and
|
| 401 |
+
[[expr.ass]].
|
| 402 |
|
| 403 |
The operand of postfix `\dcr` is decremented analogously to the postfix
|
| 404 |
+
`++` operator.
|
| 405 |
+
|
| 406 |
+
[*Note 3*: For prefix increment and decrement, see
|
| 407 |
+
[[expr.pre.incr]]. — *end note*]
|
| 408 |
|
| 409 |
### Dynamic cast <a id="expr.dynamic.cast">[[expr.dynamic.cast]]</a>
|
| 410 |
|
| 411 |
The result of the expression `dynamic_cast<T>(v)` is the result of
|
| 412 |
converting the expression `v` to type `T`. `T` shall be a pointer or
|
| 413 |
+
reference to a complete class type, or “pointer to *cv* `void`”. The
|
| 414 |
`dynamic_cast` operator shall not cast away constness (
|
| 415 |
[[expr.const.cast]]).
|
| 416 |
|
| 417 |
If `T` is a pointer type, `v` shall be a prvalue of a pointer to
|
| 418 |
complete class type, and the result is a prvalue of type `T`. If `T` is
|
| 419 |
an lvalue reference type, `v` shall be an lvalue of a complete class
|
| 420 |
type, and the result is an lvalue of the type referred to by `T`. If `T`
|
| 421 |
+
is an rvalue reference type, `v` shall be a glvalue having a complete
|
| 422 |
+
class type, and the result is an xvalue of the type referred to by `T`.
|
|
|
|
| 423 |
|
| 424 |
If the type of `v` is the same as `T`, or it is the same as `T` except
|
| 425 |
that the class object type in `T` is more cv-qualified than the class
|
| 426 |
object type in `v`, the result is `v` (converted if necessary).
|
| 427 |
|
|
|
|
| 431 |
If `T` is “pointer to *cv1* `B`” and `v` has type “pointer to *cv2* `D`”
|
| 432 |
such that `B` is a base class of `D`, the result is a pointer to the
|
| 433 |
unique `B` subobject of the `D` object pointed to by `v`. Similarly, if
|
| 434 |
`T` is “reference to *cv1* `B`” and `v` has type *cv2* `D` such that `B`
|
| 435 |
is a base class of `D`, the result is the unique `B` subobject of the
|
| 436 |
+
`D` object referred to by `v`.[^8] In both the pointer and reference
|
| 437 |
+
cases, the program is ill-formed if *cv2* has greater cv-qualification
|
| 438 |
+
than *cv1* or if `B` is an inaccessible or ambiguous base class of `D`.
|
| 439 |
+
|
| 440 |
+
[*Example 1*:
|
| 441 |
|
| 442 |
``` cpp
|
| 443 |
struct B { };
|
| 444 |
struct D : B { };
|
| 445 |
void foo(D* dp) {
|
| 446 |
B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
|
| 447 |
}
|
| 448 |
```
|
| 449 |
|
| 450 |
+
— *end example*]
|
| 451 |
+
|
| 452 |
Otherwise, `v` shall be a pointer to or a glvalue of a polymorphic
|
| 453 |
type ([[class.virtual]]).
|
| 454 |
|
| 455 |
+
If `T` is “pointer to *cv* `void`”, then the result is a pointer to the
|
| 456 |
+
most derived object pointed to by `v`. Otherwise, a runtime check is
|
| 457 |
applied to see if the object pointed or referred to by `v` can be
|
| 458 |
converted to the type pointed or referred to by `T`.
|
| 459 |
|
| 460 |
+
If `C` is the class type to which `T` points or refers, the runtime
|
| 461 |
check logically executes as follows:
|
| 462 |
|
| 463 |
- If, in the most derived object pointed (referred) to by `v`, `v`
|
| 464 |
points (refers) to a `public` base class subobject of a `C` object,
|
| 465 |
and if only one object of type `C` is derived from the subobject
|
|
|
|
| 468 |
- Otherwise, if `v` points (refers) to a `public` base class subobject
|
| 469 |
of the most derived object, and the type of the most derived object
|
| 470 |
has a base class, of type `C`, that is unambiguous and `public`, the
|
| 471 |
result points (refers) to the `C` subobject of the most derived
|
| 472 |
object.
|
| 473 |
+
- Otherwise, the runtime check *fails*.
|
| 474 |
|
| 475 |
The value of a failed cast to pointer type is the null pointer value of
|
| 476 |
the required result type. A failed cast to reference type throws an
|
| 477 |
exception ([[except.throw]]) of a type that would match a handler (
|
| 478 |
[[except.handle]]) of type `std::bad_cast` ([[bad.cast]]).
|
| 479 |
|
| 480 |
+
[*Example 2*:
|
| 481 |
+
|
| 482 |
``` cpp
|
| 483 |
class A { virtual void f(); };
|
| 484 |
class B { virtual void g(); };
|
| 485 |
class D : public virtual A, private B { };
|
| 486 |
void g() {
|
|
|
|
| 489 |
A* ap = &d; // public derivation, no cast needed
|
| 490 |
D& dr = dynamic_cast<D&>(*bp); // fails
|
| 491 |
ap = dynamic_cast<A*>(bp); // fails
|
| 492 |
bp = dynamic_cast<B*>(ap); // fails
|
| 493 |
ap = dynamic_cast<A*>(&d); // succeeds
|
| 494 |
+
bp = dynamic_cast<B*>(&d); // ill-formed (not a runtime check)
|
| 495 |
}
|
| 496 |
|
| 497 |
class E : public D, public B { };
|
| 498 |
class F : public E, public D { };
|
| 499 |
void h() {
|
| 500 |
F f;
|
| 501 |
A* ap = &f; // succeeds: finds unique A
|
| 502 |
+
D* dp = dynamic_cast<D*>(ap); // fails: yields null; f has two D subobjects
|
|
|
|
| 503 |
E* ep = (E*)ap; // ill-formed: cast from virtual base
|
| 504 |
E* ep1 = dynamic_cast<E*>(ap); // succeeds
|
| 505 |
}
|
| 506 |
```
|
| 507 |
|
| 508 |
+
— *end example*]
|
| 509 |
+
|
| 510 |
+
[*Note 1*: [[class.cdtor]] describes the behavior of a `dynamic_cast`
|
| 511 |
+
applied to an object under construction or destruction. — *end note*]
|
| 512 |
|
| 513 |
### Type identification <a id="expr.typeid">[[expr.typeid]]</a>
|
| 514 |
|
| 515 |
The result of a `typeid` expression is an lvalue of static type `const`
|
| 516 |
`std::type_info` ([[type.info]]) and dynamic type `const`
|
|
|
|
| 534 |
When `typeid` is applied to an expression other than a glvalue of a
|
| 535 |
polymorphic class type, the result refers to a `std::type_info` object
|
| 536 |
representing the static type of the expression. Lvalue-to-rvalue (
|
| 537 |
[[conv.lval]]), array-to-pointer ([[conv.array]]), and
|
| 538 |
function-to-pointer ([[conv.func]]) conversions are not applied to the
|
| 539 |
+
expression. If the expression is a prvalue, the temporary
|
| 540 |
+
materialization conversion ([[conv.rval]]) is applied. The expression
|
| 541 |
+
is an unevaluated operand (Clause [[expr]]).
|
| 542 |
|
| 543 |
When `typeid` is applied to a *type-id*, the result refers to a
|
| 544 |
`std::type_info` object representing the type of the *type-id*. If the
|
| 545 |
+
type of the *type-id* is a reference to a possibly cv-qualified type,
|
| 546 |
the result of the `typeid` expression refers to a `std::type_info`
|
| 547 |
+
object representing the cv-unqualified referenced type. If the type of
|
| 548 |
the *type-id* is a class type or a reference to a class type, the class
|
| 549 |
shall be completely-defined.
|
| 550 |
|
| 551 |
If the type of the expression or *type-id* is a cv-qualified type, the
|
| 552 |
result of the `typeid` expression refers to a `std::type_info` object
|
| 553 |
representing the cv-unqualified type.
|
| 554 |
|
| 555 |
+
[*Example 1*:
|
| 556 |
+
|
| 557 |
``` cpp
|
| 558 |
+
class D { ... };
|
| 559 |
D d1;
|
| 560 |
const D d2;
|
| 561 |
|
| 562 |
typeid(d1) == typeid(d2); // yields true
|
| 563 |
typeid(D) == typeid(const D); // yields true
|
| 564 |
typeid(D) == typeid(d2); // yields true
|
| 565 |
typeid(D) == typeid(const D&); // yields true
|
| 566 |
```
|
| 567 |
|
| 568 |
+
— *end example*]
|
| 569 |
+
|
| 570 |
If the header `<typeinfo>` ([[type.info]]) is not included prior to a
|
| 571 |
use of `typeid`, the program is ill-formed.
|
| 572 |
|
| 573 |
+
[*Note 1*: [[class.cdtor]] describes the behavior of `typeid` applied
|
| 574 |
+
to an object under construction or destruction. — *end note*]
|
| 575 |
|
| 576 |
### Static cast <a id="expr.static.cast">[[expr.static.cast]]</a>
|
| 577 |
|
| 578 |
The result of the expression `static_cast<T>(v)` is the result of
|
| 579 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 580 |
type or an rvalue reference to function type, the result is an lvalue;
|
| 581 |
if `T` is an rvalue reference to object type, the result is an xvalue;
|
| 582 |
otherwise, the result is a prvalue. The `static_cast` operator shall not
|
| 583 |
cast away constness ([[expr.const.cast]]).
|
| 584 |
|
| 585 |
+
An lvalue of type “*cv1* `B`”, where `B` is a class type, can be cast to
|
| 586 |
+
type “reference to *cv2* `D`”, where `D` is a class derived (Clause
|
| 587 |
+
[[class.derived]]) from `B`, if *cv2* is the same cv-qualification as,
|
| 588 |
+
or greater cv-qualification than, *cv1*. If `B` is a virtual base class
|
| 589 |
+
of `D` or a base class of a virtual base class of `D`, or if no valid
|
| 590 |
+
standard conversion from “pointer to `D`” to “pointer to `B`” exists (
|
| 591 |
+
[[conv.ptr]]), the program is ill-formed. An xvalue of type “*cv1* `B`”
|
| 592 |
+
can be cast to type “rvalue reference to *cv2* `D`” with the same
|
| 593 |
+
constraints as for an lvalue of type “*cv1* `B`”. If the object of type
|
| 594 |
+
“*cv1* `B`” is actually a base class subobject of an object of type `D`,
|
| 595 |
+
the result refers to the enclosing object of type `D`. Otherwise, the
|
| 596 |
+
behavior is undefined.
|
| 597 |
+
|
| 598 |
+
[*Example 1*:
|
| 599 |
|
| 600 |
``` cpp
|
| 601 |
struct B { };
|
| 602 |
struct D : public B { };
|
| 603 |
D d;
|
| 604 |
B &br = d;
|
| 605 |
|
| 606 |
static_cast<D&>(br); // produces lvalue to the original d object
|
| 607 |
```
|
| 608 |
|
| 609 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
+
An lvalue of type “*cv1* `T1`” can be cast to type “rvalue reference to
|
| 612 |
+
*cv2* `T2`” if “*cv2* `T2`” is reference-compatible with “*cv1* `T1`” (
|
| 613 |
+
[[dcl.init.ref]]). If the value is not a bit-field, the result refers to
|
| 614 |
+
the object or the specified base class subobject thereof; otherwise, the
|
| 615 |
+
lvalue-to-rvalue conversion ([[conv.lval]]) is applied to the bit-field
|
| 616 |
+
and the resulting prvalue is used as the *expression* of the
|
| 617 |
+
`static_cast` for the remainder of this section. If `T2` is an
|
| 618 |
+
inaccessible (Clause [[class.access]]) or ambiguous (
|
| 619 |
+
[[class.member.lookup]]) base class of `T1`, a program that necessitates
|
| 620 |
+
such a cast is ill-formed.
|
| 621 |
+
|
| 622 |
+
An expression `e` can be explicitly converted to a type `T` if there is
|
| 623 |
+
an implicit conversion sequence ([[over.best.ics]]) from `e` to `T`, or
|
| 624 |
+
if overload resolution for a direct-initialization ([[dcl.init]]) of an
|
| 625 |
+
object or reference of type `T` from `e` would find at least one viable
|
| 626 |
+
function ([[over.match.viable]]). If `T` is a reference type, the
|
| 627 |
+
effect is the same as performing the declaration and initialization
|
| 628 |
+
|
| 629 |
+
``` cpp
|
| 630 |
+
T t(e);
|
| 631 |
+
```
|
| 632 |
+
|
| 633 |
+
for some invented temporary variable `t` ([[dcl.init]]) and then using
|
| 634 |
+
the temporary variable as the result of the conversion. Otherwise, the
|
| 635 |
+
result object is direct-initialized from `e`.
|
| 636 |
+
|
| 637 |
+
[*Note 1*: The conversion is ill-formed when attempting to convert an
|
| 638 |
+
expression of class type to an inaccessible or ambiguous base
|
| 639 |
+
class. — *end note*]
|
| 640 |
|
| 641 |
Otherwise, the `static_cast` shall perform one of the conversions listed
|
| 642 |
below. No other conversion shall be performed explicitly using a
|
| 643 |
`static_cast`.
|
| 644 |
|
| 645 |
Any expression can be explicitly converted to type cv `void`, in which
|
| 646 |
case it becomes a discarded-value expression (Clause [[expr]]).
|
| 647 |
+
|
| 648 |
+
[*Note 2*: However, if the value is in a temporary object (
|
| 649 |
+
[[class.temporary]]), the destructor for that object is not executed
|
| 650 |
+
until the usual time, and the value of the object is preserved for the
|
| 651 |
+
purpose of executing the destructor. — *end note*]
|
| 652 |
|
| 653 |
The inverse of any standard conversion sequence (Clause [[conv]]) not
|
| 654 |
containing an lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 655 |
[[conv.array]]), function-to-pointer ([[conv.func]]), null pointer (
|
| 656 |
+
[[conv.ptr]]), null member pointer ([[conv.mem]]), boolean (
|
| 657 |
+
[[conv.bool]]), or function pointer ([[conv.fctptr]]) conversion, can
|
| 658 |
+
be performed explicitly using `static_cast`. A program is ill-formed if
|
| 659 |
+
it uses `static_cast` to perform the inverse of an ill-formed standard
|
| 660 |
+
conversion sequence.
|
| 661 |
+
|
| 662 |
+
[*Example 2*:
|
| 663 |
|
| 664 |
``` cpp
|
| 665 |
struct B { };
|
| 666 |
struct D : private B { };
|
| 667 |
void f() {
|
| 668 |
+
static_cast<D*>((B*)0); // error: B is a private base of D
|
| 669 |
+
static_cast<int B::*>((int D::*)0); // error: B is a private base of D
|
| 670 |
}
|
| 671 |
```
|
| 672 |
|
| 673 |
+
— *end example*]
|
| 674 |
+
|
| 675 |
The lvalue-to-rvalue ([[conv.lval]]), array-to-pointer (
|
| 676 |
[[conv.array]]), and function-to-pointer ([[conv.func]]) conversions
|
| 677 |
are applied to the operand. Such a `static_cast` is subject to the
|
| 678 |
restriction that the explicit conversion does not cast away constness (
|
| 679 |
[[expr.const.cast]]), and the following additional rules for specific
|
|
|
|
| 687 |
type. Otherwise, the resulting value is unspecified. A value of a scoped
|
| 688 |
enumeration type can also be explicitly converted to a floating-point
|
| 689 |
type; the result is the same as that of converting from the original
|
| 690 |
value to the floating-point type.
|
| 691 |
|
| 692 |
+
A value of integral or enumeration type can be explicitly converted to a
|
| 693 |
+
complete enumeration type. The value is unchanged if the original value
|
| 694 |
+
is within the range of the enumeration values ([[dcl.enum]]).
|
| 695 |
+
Otherwise, the behavior is undefined. A value of floating-point type can
|
| 696 |
+
also be explicitly converted to an enumeration type. The resulting value
|
| 697 |
+
is the same as converting the original value to the underlying type of
|
| 698 |
+
the enumeration ([[conv.fpint]]), and subsequently to the enumeration
|
| 699 |
+
type.
|
| 700 |
|
| 701 |
+
A prvalue of type “pointer to *cv1* `B`”, where `B` is a class type, can
|
| 702 |
+
be converted to a prvalue of type “pointer to *cv2* `D`”, where `D` is a
|
| 703 |
+
class derived (Clause [[class.derived]]) from `B`, if *cv2* is the same
|
| 704 |
+
cv-qualification as, or greater cv-qualification than, *cv1*. If `B` is
|
| 705 |
+
a virtual base class of `D` or a base class of a virtual base class of
|
| 706 |
+
`D`, or if no valid standard conversion from “pointer to `D`” to
|
| 707 |
+
“pointer to `B`” exists ([[conv.ptr]]), the program is ill-formed. The
|
| 708 |
+
null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 709 |
+
value of the destination type. If the prvalue of type “pointer to *cv1*
|
| 710 |
+
`B`” points to a `B` that is actually a subobject of an object of type
|
| 711 |
+
`D`, the resulting pointer points to the enclosing object of type `D`.
|
| 712 |
+
Otherwise, the behavior is undefined.
|
| 713 |
|
| 714 |
A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
|
| 715 |
+
converted to a prvalue of type “pointer to member of `B` of type *cv2*
|
| 716 |
+
`T`”, where `B` is a base class (Clause [[class.derived]]) of `D`, if
|
| 717 |
+
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 718 |
+
*cv1*.[^11] If no valid standard conversion from “pointer to member of
|
| 719 |
+
`B` of type `T`” to “pointer to member of `D` of type `T`” exists (
|
| 720 |
+
[[conv.mem]]), the program is ill-formed. The null member pointer
|
| 721 |
+
value ([[conv.mem]]) is converted to the null member pointer value of
|
| 722 |
+
the destination type. If class `B` contains the original member, or is a
|
| 723 |
+
base or derived class of the class containing the original member, the
|
| 724 |
+
resulting pointer to member points to the original member. Otherwise,
|
| 725 |
+
the behavior is undefined.
|
| 726 |
+
|
| 727 |
+
[*Note 3*: Although class `B` need not contain the original member, the
|
| 728 |
+
dynamic type of the object with which indirection through the pointer to
|
| 729 |
+
member is performed must contain the original member; see
|
| 730 |
+
[[expr.mptr.oper]]. — *end note*]
|
| 731 |
|
| 732 |
A prvalue of type “pointer to *cv1* `void`” can be converted to a
|
| 733 |
+
prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
|
| 734 |
*cv2* is the same cv-qualification as, or greater cv-qualification than,
|
| 735 |
+
*cv1*. If the original pointer value represents the address `A` of a
|
| 736 |
+
byte in memory and `A` does not satisfy the alignment requirement of
|
| 737 |
+
`T`, then the resulting pointer value is unspecified. Otherwise, if the
|
| 738 |
+
original pointer value points to an object *a*, and there is an object
|
| 739 |
+
*b* of type `T` (ignoring cv-qualification) that is
|
| 740 |
+
pointer-interconvertible ([[basic.compound]]) with *a*, the result is a
|
| 741 |
+
pointer to *b*. Otherwise, the pointer value is unchanged by the
|
| 742 |
+
conversion.
|
| 743 |
+
|
| 744 |
+
[*Example 3*:
|
| 745 |
|
| 746 |
``` cpp
|
| 747 |
T* p1 = new T;
|
| 748 |
const T* p2 = static_cast<const T*>(static_cast<void*>(p1));
|
| 749 |
bool b = p1 == p2; // b will have the value true.
|
| 750 |
```
|
| 751 |
|
| 752 |
+
— *end example*]
|
| 753 |
+
|
| 754 |
### Reinterpret cast <a id="expr.reinterpret.cast">[[expr.reinterpret.cast]]</a>
|
| 755 |
|
| 756 |
The result of the expression `reinterpret_cast<T>(v)` is the result of
|
| 757 |
converting the expression `v` to type `T`. If `T` is an lvalue reference
|
| 758 |
type or an rvalue reference to function type, the result is an lvalue;
|
|
|
|
| 767 |
The `reinterpret_cast` operator shall not cast away constness (
|
| 768 |
[[expr.const.cast]]). An expression of integral, enumeration, pointer,
|
| 769 |
or pointer-to-member type can be explicitly converted to its own type;
|
| 770 |
such a cast yields the value of its operand.
|
| 771 |
|
| 772 |
+
[*Note 1*: The mapping performed by `reinterpret_cast` might, or might
|
| 773 |
+
not, produce a representation different from the original
|
| 774 |
+
value. — *end note*]
|
| 775 |
|
| 776 |
A pointer can be explicitly converted to any integral type large enough
|
| 777 |
+
to hold it. The mapping function is *implementation-defined*.
|
| 778 |
+
|
| 779 |
+
[*Note 2*: It is intended to be unsurprising to those who know the
|
| 780 |
+
addressing structure of the underlying machine. — *end note*]
|
| 781 |
+
|
| 782 |
+
A value of type `std::nullptr_t` can be converted to an integral type;
|
| 783 |
+
the conversion has the same meaning and validity as a conversion of
|
| 784 |
+
`(void*)0` to the integral type.
|
| 785 |
+
|
| 786 |
+
[*Note 3*: A `reinterpret_cast` cannot be used to convert a value of
|
| 787 |
+
any type to the type `std::nullptr_t`. — *end note*]
|
| 788 |
|
| 789 |
A value of integral type or enumeration type can be explicitly converted
|
| 790 |
to a pointer. A pointer converted to an integer of sufficient size (if
|
| 791 |
any such exists on the implementation) and back to the same pointer type
|
| 792 |
will have its original value; mappings between pointers and integers are
|
| 793 |
+
otherwise *implementation-defined*.
|
| 794 |
+
|
| 795 |
+
[*Note 4*: Except as described in [[basic.stc.dynamic.safety]], the
|
| 796 |
+
result of such a conversion will not be a safely-derived pointer
|
| 797 |
+
value. — *end note*]
|
| 798 |
|
| 799 |
A function pointer can be explicitly converted to a function pointer of
|
| 800 |
+
a different type.
|
| 801 |
+
|
| 802 |
+
[*Note 5*: The effect of calling a function through a pointer to a
|
| 803 |
+
function type ([[dcl.fct]]) that is not the same as the type used in
|
| 804 |
+
the definition of the function is undefined. — *end note*]
|
| 805 |
+
|
| 806 |
+
Except that converting a prvalue of type “pointer to `T1`” to the type
|
| 807 |
+
“pointer to `T2`” (where `T1` and `T2` are function types) and back to
|
| 808 |
+
its original type yields the original pointer value, the result of such
|
| 809 |
+
a pointer conversion is unspecified.
|
| 810 |
+
|
| 811 |
+
[*Note 6*: See also [[conv.ptr]] for more details of pointer
|
| 812 |
+
conversions. — *end note*]
|
| 813 |
|
| 814 |
An object pointer can be explicitly converted to an object pointer of a
|
| 815 |
different type.[^12] When a prvalue `v` of object pointer type is
|
| 816 |
converted to the object pointer type “pointer to cv `T`”, the result is
|
| 817 |
+
`static_cast<cv T*>(static_cast<cv~void*>(v))`.
|
| 818 |
+
|
| 819 |
+
[*Note 7*: Converting a prvalue of type “pointer to `T1`” to the type
|
| 820 |
+
“pointer to `T2`” (where `T1` and `T2` are object types and where the
|
| 821 |
+
alignment requirements of `T2` are no stricter than those of `T1`) and
|
| 822 |
+
back to its original type yields the original pointer
|
| 823 |
+
value. — *end note*]
|
| 824 |
|
| 825 |
Converting a function pointer to an object pointer type or vice versa is
|
| 826 |
conditionally-supported. The meaning of such a conversion is
|
| 827 |
*implementation-defined*, except that if an implementation supports
|
| 828 |
conversions in both directions, converting a prvalue of one type to the
|
| 829 |
other type and back, possibly with different cv-qualification, shall
|
| 830 |
yield the original pointer value.
|
| 831 |
|
| 832 |
The null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 833 |
+
value of the destination type.
|
| 834 |
+
|
| 835 |
+
[*Note 8*: A null pointer constant of type `std::nullptr_t` cannot be
|
| 836 |
+
converted to a pointer type, and a null pointer constant of integral
|
| 837 |
+
type is not necessarily converted to a null pointer
|
| 838 |
+
value. — *end note*]
|
| 839 |
|
| 840 |
A prvalue of type “pointer to member of `X` of type `T1`” can be
|
| 841 |
explicitly converted to a prvalue of a different type “pointer to member
|
| 842 |
of `Y` of type `T2`” if `T1` and `T2` are both function types or both
|
| 843 |
object types.[^13] The null member pointer value ([[conv.mem]]) is
|
|
|
|
| 855 |
|
| 856 |
A glvalue expression of type `T1` can be cast to the type “reference to
|
| 857 |
`T2`” if an expression of type “pointer to `T1`” can be explicitly
|
| 858 |
converted to the type “pointer to `T2`” using a `reinterpret_cast`. The
|
| 859 |
result refers to the same object as the source glvalue, but with the
|
| 860 |
+
specified type.
|
| 861 |
+
|
| 862 |
+
[*Note 9*: That is, for lvalues, a reference cast
|
| 863 |
`reinterpret_cast<T&>(x)` has the same effect as the conversion
|
| 864 |
`*reinterpret_cast<T*>(&x)` with the built-in `&` and `*` operators (and
|
| 865 |
+
similarly for `reinterpret_cast<T&&>(x)`). — *end note*]
|
| 866 |
+
|
| 867 |
+
No temporary is created, no copy is made, and constructors (
|
| 868 |
+
[[class.ctor]]) or conversion functions ([[class.conv]]) are not
|
| 869 |
+
called.[^14]
|
| 870 |
|
| 871 |
### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 872 |
|
| 873 |
The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
|
| 874 |
is an lvalue reference to object type, the result is an lvalue; if `T`
|
|
|
|
| 878 |
function-to-pointer ([[conv.func]]) standard conversions are performed
|
| 879 |
on the expression `v`. Conversions that can be performed explicitly
|
| 880 |
using `const_cast` are listed below. No other conversion shall be
|
| 881 |
performed explicitly using `const_cast`.
|
| 882 |
|
| 883 |
+
[*Note 1*: Subject to the restrictions in this section, an expression
|
| 884 |
+
may be cast to its own type using a `const_cast`
|
| 885 |
+
operator. — *end note*]
|
| 886 |
|
| 887 |
+
For two similar types `T1` and `T2` ([[conv.qual]]), a prvalue of type
|
| 888 |
+
`T1` may be explicitly converted to the type `T2` using a `const_cast`.
|
| 889 |
+
The result of a `const_cast` refers to the original entity.
|
| 890 |
|
| 891 |
+
[*Example 1*:
|
| 892 |
|
| 893 |
+
``` cpp
|
| 894 |
+
typedef int *A[3]; // array of 3 pointer to int
|
| 895 |
+
typedef const int *const CA[3]; // array of 3 const pointer to const int
|
| 896 |
+
|
| 897 |
+
CA &&r = A{}; // OK, reference binds to temporary array object after qualification conversion to type CA
|
| 898 |
+
A &&r1 = const_cast<A>(CA{}); // error: temporary array decayed to pointer
|
| 899 |
+
A &&r2 = const_cast<A&&>(CA{}); // OK
|
| 900 |
+
```
|
| 901 |
+
|
| 902 |
+
— *end example*]
|
| 903 |
|
| 904 |
For two object types `T1` and `T2`, if a pointer to `T1` can be
|
| 905 |
explicitly converted to the type “pointer to `T2`” using a `const_cast`,
|
| 906 |
then the following conversions can also be made:
|
| 907 |
|
|
|
|
| 910 |
- a glvalue of type `T1` can be explicitly converted to an xvalue of
|
| 911 |
type `T2` using the cast `const_cast<T2&&>`; and
|
| 912 |
- if `T1` is a class type, a prvalue of type `T1` can be explicitly
|
| 913 |
converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
|
| 914 |
|
| 915 |
+
The result of a reference `const_cast` refers to the original object if
|
| 916 |
+
the operand is a glvalue and to the result of applying the temporary
|
| 917 |
+
materialization conversion ([[conv.rval]]) otherwise.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 918 |
|
| 919 |
A null pointer value ([[conv.ptr]]) is converted to the null pointer
|
| 920 |
value of the destination type. The null member pointer value (
|
| 921 |
[[conv.mem]]) is converted to the null member pointer value of the
|
| 922 |
destination type.
|
| 923 |
|
| 924 |
+
[*Note 2*: Depending on the type of the object, a write operation
|
| 925 |
+
through the pointer, lvalue or pointer to data member resulting from a
|
| 926 |
+
`const_cast` that casts away a const-qualifier[^15] may produce
|
| 927 |
+
undefined behavior ([[dcl.type.cv]]). — *end note*]
|
| 928 |
|
| 929 |
+
A conversion from a type `T1` to a type `T2` *casts away constness* if
|
| 930 |
+
`T1` and `T2` are different, there is a cv-decomposition (
|
| 931 |
+
[[conv.qual]]) of `T1` yielding *n* such that `T2` has a
|
| 932 |
+
cv-decomposition of the form
|
| 933 |
|
| 934 |
+
and there is no qualification conversion that converts `T1` to
|
|
|
|
|
|
|
|
|
|
|
|
|
| 935 |
|
| 936 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 937 |
lvalue reference cast or casting from an expression of type `T1` to an
|
| 938 |
xvalue of type `T2` using an rvalue reference cast casts away constness
|
| 939 |
if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
|
| 940 |
to `T2`” casts away constness.
|
| 941 |
|
| 942 |
+
[*Note 3*: Some conversions which involve only changes in
|
| 943 |
+
cv-qualification cannot be done using `const_cast.` For instance,
|
| 944 |
+
conversions between pointers to functions are not covered because such
|
| 945 |
+
conversions lead to values whose use causes undefined behavior. For the
|
| 946 |
+
same reasons, conversions between pointers to member functions, and in
|
| 947 |
+
particular, the conversion from a pointer to a const member function to
|
| 948 |
+
a pointer to a non-const member function, are not
|
| 949 |
+
covered. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 950 |
|