[expr.cast] (original) (raw)

7 Expressions [expr]

7.6 Compound expressions [expr.compound]

7.6.3 Explicit type conversion (cast notation) [expr.cast]

The result of the expression (T) cast-expression is of type T.

The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type and an xvalue if Tis an rvalue reference to object type; otherwise the result is a prvalue.

[Note 1:

If T is a non-class type that is cv-qualified, thecv-qualifiers are discarded when determining the type of the resulting prvalue; see [expr.prop].

— _end note_]

An explicit type conversion can be expressed using functional notation ([expr.type.conv]), a type conversion operator (dynamic_cast, static_cast, reinterpret_cast,const_cast), or the cast notation.

Any type conversion not mentioned below and not explicitly defined by the user ([class.conv]) is ill-formed.

The conversions performed by

can be performed using the cast notation of explicit type conversion.

The same semantic restrictions and behaviors apply, with the exception that in performing a static_cast in the following situations the conversion is valid even if the base class is inaccessible:

If a conversion can be interpreted in more than one of the ways listed above, the interpretation that appears first in the list is used, even if a cast resulting from that interpretation is ill-formed.

If astatic_cast followed by a const_cast is used and the conversion can be interpreted in more than one way as such, the conversion is ill-formed.

[Example 1: struct A { };struct I1 : A { };struct I2 : A { };struct D : I1, I2 { }; A* foo( D* p ) { return (A*)( p ); } int*** ptr = 0;auto t = (int const*const*const*)ptr; struct S { operator const int*();operator volatile int*();};int *p = (int*)S(); — _end example_]

The operand of a cast using the cast notation can be a prvalue of type “pointer to incomplete class type”.

The destination type of a cast using the cast notation can be “pointer to incomplete class type”.

If both the operand and destination types are class types and one or both are incomplete, it is unspecified whether the static_cast or thereinterpret_cast interpretation is used, even if there is an inheritance relationship between the two classes.

[Note 2:

For example, if the classes were defined later in the translation unit, a multi-pass compiler could validly interpret a cast between pointers to the classes as if the class types were complete at the point of the cast.

— _end note_]