next up previous contents index
Next: Pointer-to-Member Operators Up: Expressions Previous: Delete

5.4 Explicit Type Conversion

An explicit type conversion can be expressed using either functional notation (5.2.3) or the cast notation.

	cast-expression:
		unary-expression
		( type-name ) cast-expression
The cast notation is needed to express conversion to a type that does not have a simple-type-name .

Types may not be defined in casts.

Any type conversion not mentioned below and not explicitly defined by the user (12.3) is an error.

Any type that can be converted to another by a standard conversion (4) can also be converted by explicit conversion and the meaning is the same.

A pointer may be explicitly converted to any integral type large enough to hold it. The mapping function is implementation dependent, but is intended to be unsurprising to those who know the addressing structure of the underlying machine.

A value of integral type may be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation dependent.

A pointer to one object type may be explicitly converted to a pointer to another object type (subject to the restrictions mentioned in this section). The resulting pointer may cause addressing exceptions on use if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object of a given size may be converted to a pointer to an object of the same or smaller size and back again without change. Different machines may differ in the number of bits in pointers and in alignment requirements for objects. Aggregates are aligned on the strictest boundary required by any of their constituents. A void* is considered a pointer to object type.

A pointer to a class B may be explicitly converted to a pointer to a class D that has B as a direct or indirect base class if an unambiguous conversion from D to B exists (4.6, 10.1.1) and if B is not a virtual base class (10.1). Such a cast from a base to a derived class assumes that the object of the base class is a sub-object of an object of the derived class; the resulting pointer points to the enclosing object of the derived class. If the object of the base class is not a sub-object of an object of the derived class, the cast may cause an exception.

The null pointer (0 ) is converted into itself.

A yet undefined class may be used in a pointer cast, in which case no assumptions will be made about class lattices (10.1).

An object may be explicitly converted to a reference type X& if a pointer to that object may be explicitly converted to an X* . Constructors or conversion functions are not called as the result of a cast to a reference. Conversion of a reference to a base class to a reference to a derived class is handled similarly to the conversion of a pointer to a base class to a pointer to a derived class with respect to ambiguity, virtual classes, and so on.

The result of a cast to a reference type is an lvalue; the results of other casts are not. Operations performed on the result of a pointer or reference cast refer to the same object as the original (uncast) expression.

A pointer to function may be explicitly converted to a pointer to an object type provided the object pointer type has enough bits to hold the function pointer. A pointer to an object type may be explicitly converted to a pointer to function provided the function pointer type has enough bits to hold the object pointer. In both cases, use of the resulting pointer may cause addressing exceptions, or worse, if the subject pointer does not refer to suitable storage.

A pointer to a function may be explicitly converted to a pointer to a function of a different type. The effect of calling a function through a pointer to a function type that differs from the type used in the definition of the function is undefined. See also 4.6.

An object or a value may be converted to a class object (only) if an appropriate constructor or conversion operator has been declared (12.3).

A pointer to member may be explicitly converted into a different pointer to member type when the two types are both pointers to members of the same class or when the two types are pointers to member functions of classes one of which is unambiguously derived from the other (4.8).

A pointer to an object of a const type can be cast into a pointer to a non-const type. The resulting pointer will refer to the original object. An object of a const type or a reference to an object of a const type can be cast into a reference to a non-const type. The resulting reference will refer to the original object. The result of attempting to modify that object through such a pointer or reference will either cause an addressing exception or be the same as if the original pointer or reference had referred a non-const object. It is implementation dependent whether the addressing exception occurs.

A pointer to an object of a volatile type can be cast into a pointer to a non-volatile type. The resulting pointer will refer to the original object. An object of a volatile type or a reference to an object of a volatile type can be cast into a reference to a non-volatile type.



next up previous contents index
Next: Pointer-to-Member Operators Up: Expressions Previous: Delete

G1AF03E1 C++ Language Reference Manual