[expr.static.cast] (original) (raw)

7 Expressions [expr]

7.6 Compound expressions [expr.compound]

7.6.1 Postfix expressions [expr.post]

7.6.1.9 Static cast [expr.static.cast]

The result of the expression static_­cast<T>(v) is the result of converting the expression v to type T.

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

An lvalue of type “cv1 B”, where B is a class type, can be cast to type “reference to cv2 D”, whereD is a class derived from B, if cv2 is the same cv-qualification as, or greater cv-qualification than,cv1.

If B is a virtual base class of Dor a base class of a virtual base class of D, or if no valid standard conversion from “pointer to D” to “pointer to B” exists ([conv.ptr]), the program is ill-formed.

An xvalue of type “cv1 B” can be cast to type “rvalue reference to_cv2_ D” with the same constraints as for an lvalue of type “cv1 B”.

If the object of type “cv1 B” is actually a base class subobject of an object of type D, the result refers to the enclosing object of typeD.

Otherwise, the behavior is undefined.

[Example 1: struct B { };struct D : public B { }; D d; B &br = d;static_cast<D&>(br); — _end example_]

An lvalue of type T1 can be cast to type “rvalue reference to T2” if T2 is reference-compatible withT1 ([dcl.init.ref]).

If the value is not a bit-field, the result refers to the object or the specified base class subobject thereof; otherwise, the lvalue-to-rvalue conversionis applied to the bit-field and the resulting prvalue is used as theexpression of the static_­cast for the remainder of this subclause.

If T2 is an inaccessible orambiguous base class of T1, a program that necessitates such a cast is ill-formed.

An expression E can be explicitly converted to a type Tif there is an implicit conversion sequence ([over.best.ics]) from E to T, if overload resolution for a direct-initialization ([dcl.init]) of an object or reference of type T from Ewould find at least one viable function ([over.match.viable]), or if T is an aggregate type ([dcl.init.aggr]) having a first element x and there is an implicit conversion sequence from E to the type of x.

If T is a reference type, the effect is the same as performing the declaration and initializationT t(E);for some invented temporary variable t ([dcl.init]) and then using the temporary variable as the result of the conversion.

Otherwise, the result object is direct-initialized from E.

[Note 1:

The conversion is ill-formed when attempting to convert an expression of class type to an inaccessible or ambiguous base class.

— _end note_]

[Note 2:

If T is “array of unknown bound of U”, this direct-initialization defines the type of the expression as U[1].

— _end note_]

Otherwise, the static_­cast shall perform one of the conversions listed below.

No other conversion shall be performed explicitly using astatic_­cast.

Any expression can be explicitly converted to type cv void, in which case it becomes a discarded-value expression.

[Note 3:

However, if the value is in a temporary object, the destructor for that object is not executed until the usual time, and the value of the object is preserved for the purpose of executing the destructor.

— _end note_]

A program is ill-formed if it uses static_­cast to perform the inverse of an ill-formed standard conversion sequence.

[Example 2: struct B { };struct D : private B { };void f() { static_cast<D*>((B*)0); static_cast<int B::*>((int D::*)0); } — _end example_]

Such a static_­cast is subject to the restriction that the explicit conversion does not cast away constness, and the following additional rules for specific cases:

A value of a scoped enumeration type ([dcl.enum]) can be explicitly converted to an integral type; the result is the same as that of converting to the enumeration's underlying type and then to the destination type.

A value of a scoped enumeration type can also be explicitly converted to a floating-point type; the result is the same as that of converting from the original value to the floating-point type.

A value of integral or enumeration type can be explicitly converted to a complete enumeration type.

If the enumeration type has a fixed underlying type, the value is first converted to that type by integral conversion, if necessary, and then to the enumeration type.

If the enumeration type does not have a fixed underlying type, the value is unchanged if the original value is within the range of the enumeration values ([dcl.enum]), and otherwise, the behavior is undefined.

A value of floating-point type can also be explicitly converted to an enumeration type.

The resulting value is the same as converting the original value to the underlying type of the enumeration ([conv.fpint]), and subsequently to the enumeration type.

A prvalue of type “pointer to cv1 B”, where Bis a class type, can be converted to a prvalue of type “pointer to_cv2_ D”, where D is a complete classderivedfrom B, if cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.

If B is a virtual base class of D or a base class of a virtual base class of D, or if no valid standard conversion from “pointer to D” to “pointer to B” exists ([conv.ptr]), the program is ill-formed.

The null pointer value ([basic.compound]) is converted to the null pointer value of the destination type.

If the prvalue of type “pointer to cv1 B” points to a B that is actually a subobject of an object of type D, the resulting pointer points to the enclosing object of type D.

Otherwise, the behavior is undefined.

A prvalue of type “pointer to member of D of type cv1 T” can be converted to a prvalue of type “pointer to member ofB of type cv2 T”, whereD is a complete class type andB is a base class of D, if cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.

[Note 4:

Function types (including those used in pointer-to-member-function types) are never cv-qualified ([dcl.fct]).

— _end note_]

If no valid standard conversion from “pointer to member of B of type T” to “pointer to member of D of type T” exists ([conv.mem]), the program is ill-formed.

If class Bcontains the original member, or is a base or derived class of the class containing the original member, the resulting pointer to member points to the original member.

Otherwise, the behavior is undefined.

[Note 5:

Although class B need not contain the original member, the dynamic type of the object with which indirection through the pointer to member is performed must contain the original member; see [expr.mptr.oper].

— _end note_]

A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.

If the original pointer value represents the addressA of a byte in memory andA does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified.

Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b.

Otherwise, the pointer value is unchanged by the conversion.

[Example 3: T* p1 = new T;const T* p2 = static_cast<const T*>(static_cast<void*>(p1));bool b = p1 == p2; — _end example_]