cast_expression: prefix_expression (type_name) cast_expression
Type conversion can be implemented using the notation of function-style type conversion or the cast notation presented above. Cast notation is required when the type to which the cast expression is to be converted is not a simple type name. In the above syntax statement, type_name cannot define a new type.
Any type conversion not mentioned below and for which no application defined conversion is available is in error.
An object may only be converted to class type if an appropriate constructor or conversion operator has been defined.
Any of the standard conversions can be implemented via an explicit type cast, with the same result as the standard conversion.
Pointers may be converted to an integer of sufficient size to hold the address contained therein. Also, an integer may be converted to a pointer. If a pointer is converted to an integer and back to pointer type then provided that the size of the integral type is sufficient to hold the complete address, the pointer that is twice converted equals the original.
Subject to the considerations mentioned in this topic, a pointer to an object of one type may be converted to a pointer to an object of another type. The type void* is considered to be a pointer to an object type. It is guaranteed that, without changing the pointer, a pointer to an object may be converted to a pointer to a smaller object or an object of the same size and back again. Alignment requirements for objects are implementation dependent; therefore, pointers that are converted require care in their use. Compilers often have pragmas that influence the alignment of members within a structure.
If class B is a non-virtual, base class (direct or indirect) of class D for which an unambiguous conversion exists from D to B then a pointer to B may be cast to a pointer to D. Such a conversion assumes that the instance of B that is pointed to is contained within an instance of the derived class D; in which case, the resultant pointer is set to point at the containing instance of the class D.
A pointer to an as yet undefined class may be used in a cast expression provided that no assumptions are made about class inheritance hierarchies.
If a pointer to an object can be converted to type A* then the object itself can be converted to type A& (reference to A). In such cases, constructors or conversion operators are not called. The conversion of a reference to a base class to a reference to a derived class is subject to the same conditions as outlined above for the conversion of a pointer to a base class to a pointer to a derived class. For both pointer and reference conversions, the resultant refers to (or points to) the original object. For any type cast, only in the case of a cast to a reference is the end result an lvalue.
A pointer to a function may be converted to a pointer to a function of a different type. However, the effect of calling a function through a pointer to a function of a different type is undefined. Despite this, there are cases when the effects of calling a function through a pointer to a function of a different type are predictable (for example, a comparison function passed to a template class may be defined in terms of type* whereas the base class of the template class may define the comparison function in terms of void*). See also standard conversions.
A pointer to a function may be converted to a pointer to an object and vice-versa, although attempting access through the resultant pointer is likely to cause an addressing exception.
A pointer to a member may be converted to a pointer to a different member only if the members are of the same class or when the pointers are to methods contained in classes, one of which unambiguously derives from the other (see also standard conversions).
A pointer to an object of constant type can be cast to a pointer to an object of non-constant type, in which case the pointers refer to the same object. Similarly, an object of constant type (or a reference to an object of constant type) can be cast to a reference to an object of non-constant type, with the result that the reference so obtained still refers to the original object. The same is true for converting references and pointers to objects of volatile type.