cpp/language/static cast

Converts between types using a combination of implicit and user-defined conversions.

Syntax
Returns a value of type.

Explanation
Only the following conversions can be done with, except when such conversions would cast away constness or volatility.

@1@ If is a reference to some class  and  is an lvalue of its non-virtual base, or  is a pointer to some complete class  and  is a prvalue pointer to its non-virtual base ,  performs a downcast. (This downcast is ill-formed if is ambiguous, inaccessible, or virtual base (or a base of a virtual base) of .) Such a downcast makes no runtime checks to ensure that the object's runtime type is actually, and may only be used safely if this precondition is guaranteed by other means, such as when implementing. Safe downcast may be done with. If the object refers or points to is actually a base class subobject of an object of type, the result refers to the enclosing object of type. Otherwise, the behavior is undefined:

@3@ If there is an from  to, or if overload resolution for a  of an object or reference of type  from  would find at least one viable function, then  returns the imaginary variable  initialized as if by , which may involve s, a call to the  of  or a call to a. @4@ If is the type  (possibly cv-qualified),  discards the value of  after evaluating it. @5@ If a sequence from  to the type of  exists, that does not include lvalue-to-rvalue, array-to-pointer, function-to-pointer, null pointer, null member pointer,  or boolean conversion, then  can perform the inverse of that implicit conversion. @6@ If conversion of to  involves lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion, it can be performed explicitly by.

@8@ A value of integer or enumeration type can be converted to any complete. @@ A value of a floating-point type can also be converted to any complete enumeration type.
 * If the underlying type is not fixed, the behavior is undefined if the value of is out of range (the range is all values possible for the smallest bit-field large enough to hold all enumerators of the target enumeration).
 * If the underlying type is fixed, the result is the same as the original value first to the underlying type of the enumeration and then to the enumeration type.
 * The result is the same as the original value first to the underlying type of the enumeration, and then to the enumeration type.

@10@ A pointer to member of some complete class can be upcast to a pointer to member of its unambiguous, accessible base class. This makes no checks to ensure the member actually exists in the runtime type of the pointed-to object. @11@ A prvalue of type pointer to (possibly cv-qualified) can be converted to pointer to any object type. If the original represents an address of a byte in memory that does not satisfy the alignment requirement of the target type, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object, and there is an object of the target type (ignoring cv-qualification) that is pointer-interconvertible (as defined below) with , the result is a pointer to. Otherwise the pointer value is unchanged. Conversion of any pointer to pointer to void and back to pointer to the original (or more cv-qualified) type preserves its original value.

Two objects and  are pointer-interconvertible if:
 * they are the same object, or
 * one is a union object and the other is a non-static data member of that object, or
 * one is a class object and the other is the first non-static data member of that object or any base class subobject of that object, or
 * there exists an object such that  and  are pointer-interconvertible, and  and  are pointer-interconvertible.