Standard Conversions
C++ 17 Standard draft, Clause 7 Standard Conversions.
Standard conversions are implicit conversions with built-in meaning. Clause 7 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:
Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.
Zero or one conversion from the following set: integral promotions, floating-point promotion, integral conversions, floating-point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.
Zero or one function pointer conversion.
Zero or one qualification conversion.
[Note: A standard convention sequence can be empty, i.e., it can consist of no conversions. —end note]
A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.
[Note: Expressions with a given type will be implicitly converted to other types in several contexts:
When used as operands of operators. The operator’s requirements for its operands dictate the destination type.
When used in the condition of an
if
statement or iteration statement. The destination type isbool
.When used as the expression of a
switch
statement. The destination type is integral.When used as the source expression for an initialization (which includes use as an argument in a function call and use as the expression in a
return
statement). The type of the entity being initialized is (generally) the destination type.
—end note]
An expression e
can be implicitly
converted to a type T
if and only if the
declaration T t=e;
is well-formed, for some invented
temporary variable t
.
Certain language constructs require that an expression be converted
to a Boolean value. An expression e
appearing in such a
context is said to be contextually converted to
bool
and is well-formed if and only if the
declaration bool t(e);
is well-formed, for some invented
temporary variable t
.
Certain language constructs require conversion to a value having one
of a specified set of types appropriate to the construct. An expression
e
of class type E
appearing in such a context
is said to be contextually implicitly converted to a
specified type T
and is well-formed if and only if
e
can be implicitly converted to a type T
that
is determined as follows: E
is searched for non-explicit
conversion functions whose return type is cv T
or
reference to cv T
such that T
is
allowed by the context. There shall be exactly one such
T
.
The effect of any implicit conversion is the same as performing the
corresponding declaration and initialization and then using the
temporary variable as the result of the conversion. The result is an
lvalue if T
is an lvalue reference type or an rvalue
reference to function type, an xvalue if T
is an rvalue
reference to object type, and a prvalue otherwise. The expression
e
is used as a glvalue if and only if the initialization
uses it as a glvalue.
[Note: For class types, user-defined conversions are considered as well. In general, an implicit conversion sequence consists of a standard conversion sequence followed by a user-defined conversion followed by another standard conversion sequence. —end note]