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 T is an rvalue reference to object type; otherwise the result is a prvalue. [βNote: If T is a non-class type that is cv-qualified, the cv-qualifiers are discarded when determining the type of the resulting prvalue; see Clause [expr]. βββend noteβ]
An explicit type conversion can be expressed using functional notation, a type conversion operator (dynamic_Βcast, static_Βcast, reinterpret_Βcast, const_Βcast), or the cast notation.
cast-expression: unary-expression ( type-id ) cast-expression
Any type conversion not mentioned below and not explicitly defined by the user ([class.conv]) is ill-formed.
The conversions performed by
a const_Βcast,
a static_Βcast followed by a const_Βcast,
a reinterpret_Βcast, or
a reinterpret_Βcast followed by a const_Βcast,
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:
a pointer to an object of derived class type or an lvalue or rvalue of derived class type may be explicitly converted to a pointer or reference to an unambiguous base class type, respectively;
a pointer to member of derived class type may be explicitly converted to a pointer to member of an unambiguous non-virtual base class type;
a pointer to an object of an unambiguous non-virtual base class type, a glvalue of an unambiguous non-virtual base class type, or a pointer to member of an unambiguous non-virtual base class type may be explicitly converted to a pointer, a reference, or a pointer to member of a derived class type, respectively.
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 a conversion can be interpreted in more than one way as a static_Βcast followed by a const_Βcast, the conversion is ill-formed. [βExample:
struct A { };
struct I1 : A { };
struct I2 : A { };
struct D : I1, I2 { };
A* foo( D* p ) {
return (A*)( p ); // ill-formed static_Βcast interpretation
}
βββ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 the reinterpret_Βcast interpretation is used, even if there is an inheritance relationship between the two classes. [βNote: For example, if the classes were defined later in the translation unit, a multi-pass compiler would be permitted to interpret a cast between pointers to the classes as if the class types were complete at the point of the cast. βββend noteβ]