cpp/language/explicit cast

Converts between types using a combination of explicit and implicit conversions.

Syntax
Returns a value of type.

Explanation
@1@ When the C-style cast expression is encountered, the compiler attempts to interpret it as the following cast expressions, in this order:
 * @a@ ;
 * @b@, with extensions: pointer or reference to a  is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is  (that is, this cast ignores the private inheritance specifier). Same applies to casting  to pointer to member of unambiguous non-virtual base;
 * @c@ (with extensions) followed by ;
 * @d@ ;
 * @e@ followed by.

@@ The first choice that satisfies the requirements of the respective cast operator is selected, even if it cannot be compiled (see example). If the cast can be interpreted in more than one way as followed by a, it cannot be compiled. @@ In addition, C-style cast notation is allowed to cast from, to, and between pointers to incomplete class type. If both and  are pointers to incomplete class types, it's unspecified whether  or  gets selected.

@2@ The functional-style cast expression consists of a simple type specifier or a typedef specifier (in other words, a single-word type name, that is, cases such as and  are not valid), followed by a comma-separated list of expressions in parentheses.


 * If there is exactly one expression in parentheses, this cast expression is exactly equivalent to the corresponding C-style cast expression.
 * If there are more than one expression in parentheses,  must be a class with a suitably declared . This expression is a prvalue of type     with.
 * If there's no expression in parentheses: if names a non-array complete object type, this expression is a prvalue of type,  of that type. If  is an object type, the object is . If  is (possibly ) , the expression is a  prvalue.

@3@ A single-word type name followed by a braced-init-list is a prvalue of the specified type  with the specified braced-init-list. If is (possibly ), the expression is a  prvalue.

@4,5@ Same as, except first performs.

@6,7@ The specifier is replaced with the deduced type of the invented variable  declared with  (which is never interpreted as a function declaration) or  respectively. The result is always a prvalue of an object type.

Ambiguity Resolution
In the case of an ambiguity between an expression statement with a function-style cast expression as its leftmost subexpression and a declaration statement, the ambiguity is resolved by treating it as a declaration. This disambiguation is purely syntactic: it does not consider the meaning of names occurring in the statement other than whether they are type names:

The ambiguity above can also occur in the context of a declaration. In that context, the choice is between an object declaration with a function-style cast as the initializer and a declaration involving a function declarator with a redundant set of parentheses around a parameter name. The resolution is also to consider any construct, such as the potential parameter declaration, that could possibly be a declaration to be a declaration:

An ambiguity can arise from the similarity between a function-style cast and a. The resolution is that any construct that could possibly be a type-id in its syntactic context shall be considered a type-id: