______________________________________________________________________
4 Standard conversions [conv]
______________________________________________________________________
1 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. See _expr_.
--When used in the condition of an if statement or iteration statement
(_stmt.select_, _stmt.iter_). The destination type is bool.
--When used in the expression of a switch statement. The destination
type is integral (_stmt.select_).
--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. See _dcl.init_,
_dcl.init.ref_.
2 Standard conversions are implicit conversions defined for built-in
types. For user-defined types, user-defined conversions are consid
ered as well; see _class.conv_. In general, an implicit conversion
sequence (_over.best.ics_) consists of zero or more standard conver
sions and zero or one user-defined conversion.
3 One or more of the following standard conversions will be applied to
an expression if necessary to convert it to a required destination
type.
4 There are some contexts where certain conversions are suppressed. For
example, the lvalue-to-rvalue conversion is not done on the operand of
the unary & operator. Such exceptions are given in the descriptions
of those operators and contexts.
4.1 Lvalue-to-rvalue conversion [conv.lval]
1 An lvalue (_basic.lval_) of a non-array type T can be converted to an
rvalue. If T is an incomplete type, a program that necessitates this
conversion is ill-formed. If T is a non-class type, the type of the
rvalue is the unqualified version of T. Otherwise (i.e., T is a class
type), the type of the rvalue is T. 1)
_________________________
1) In C++ class rvalues can have qualified types (because they are ob
jects). This differs from ISO C, in which non-lvalues never have
2 The value contained in the object indicated by the lvalue is the
rvalue result. When an lvalue-to-rvalue conversion is done within the
operand of sizeof (_expr.sizeof_) the value contained in the refer
enced object is not accessed, since that operator does not evaluate
its operand.
3 See also _basic.lval_.
4.2 Array-to-pointer conversion [conv.array]
1 An lvalue or rvalue of type array of N T or array of unknown bound of
T can be converted to an rvalue of type pointer to T. The result is a
pointer to the first element of the array.
4.3 Function-to-pointer conversion [conv.func]
1 An lvalue of function type T can be converted to an rvalue of type
pointer to T. The result is a pointer to the function.2)
2 See _over.over_ for additional rules for the case where the function
is overloaded.
4.4 Qualification conversions [conv.qual]
1 An rvalue of type pointer to cv1 T can be converted to an rvalue of
type pointer to cv2 T if cv2 T is more cv-qualified than cv1 T.
2 An rvalue of type pointer to member of X of type cv1 T can be con
verted to an rvalue of type pointer to member of X of type cv2 T if
cv2 T is more cv-qualified than cv1 T.
3 A conversion can add type qualifiers at levels other than the first in
multi-level pointers, subject to the following rules:3)
Two pointer types T1 and T2 are similar if there exists a type T and
integer N>0 such that:
T1isTcv1,n*...cv1,1*cv1,0
and
T2isTcv2,n*...cv2,1*cv2,0
where each cvi,j is const, volatile, const volatile, or nothing. An
expression of type T1 can be converted to type T2 if and only if the
following conditions are satisfied:
--the pointer types are similar.
_________________________
qualified types.
2) This conversion never applies to nonstatic member functions because
there is no way to obtain an lvalue for a nonstatic member function.
3) These rules ensure that const-safety is preserved by the conver
sion.
--for every j>0, if const is in cv1,j then const is in cv2,j, and
similarly for volatile.
--the cv1,j and cv2,j are different, then const is in every cv2,k
for 0<k<j.
4 When a multi-level pointer is composed of data member pointers, or a
mix of object and data member pointers, the rules for adding type
qualifiers are the same as those for object pointers. That is, the
member aspect of the pointers is irrelevant in determining where type
qualifiers can be added.
4.5 Integral promotions [conv.prom]
1 An rvalue of type char, signed char, unsigned char, short int, or
unsigned short int can be converted to an rvalue of type int if int
can represent all the values of the source type; otherwise, the source
rvalue can be converted to an rvalue of type unsigned int.
2 An rvalue of type wchar_t (_basic.fundamental_) or an enumeration type
(_dcl.enum_) can be converted to an rvalue of the first of the follow
ing types that can represent all the values of the source type: int,
unsigned int, long, or unsigned long.
3 An rvalue for an integral bit-field (_class.bit_) can be converted to
an rvalue of type int if int can represent all the values of the bit-
field; otherwise, it can be converted to unsigned int if unsigned int
can represent all the values of the bit-field4).
4 An rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one.
5 These conversions are called integral promotions.
4.6 Floating point promotion [conv.fpprom]
1 An rvalue of type float can be converted to an rvalue of type double.
The value is unchanged.
2 This conversion is called floating point promotion.
4.7 Integral conversions [conv.integral]
1 An rvalue of an integer type can be converted to an rvalue of another
integer type.
2 If the destination type is unsigned, the resulting value is the least
unsigned integer congruent to the source integer (modulo 2n where n is
_________________________
4) If the bit-field is larger yet, it is not eligible for integral
promotion. If the bit-field has an enumerated type, it is treated as
any other value of that type for promotion purposes.
the number of bits used to represent the unsigned type). In a two's
complement representation, this conversion is conceptual and there is
no change in the bit pattern (if there is no truncation).
3 If the destination type is signed, the value is unchanged if it can be
represented in the destination type; otherwise, the value is implemen
tation-defined.
4 If the destination type is bool, see _conv.bool_. If the source type
is bool, the source integer is taken to be zero for false and one for
true.
5 The conversions allowed as integral promotions are excluded from the
set of integral conversions.
4.8 Floating point conversions [conv.double]
1 An rvalue of floating point type can be converted to an rvalue of
another floating point type. If the source value can be exactly rep
resented in the destination type, the result of the conversion is that
exact representation. If the source value is between two adjacent
destination values, the result of the conversion can be either of
those values. Otherwise, the behavior is undefined.
2 The conversions allowed as floating point promotions are excluded from
the set of floating point conversions.
4.9 Floating-integral conversions [conv.fpint]
1 An rvalue of a floating point type can be converted to an rvalue of an
integer type. The conversion truncates; that is, the fractional part
is discarded. The result is undefined if the truncated value cannot
be represented in the destination type. If the destination type is
bool, see _conv.bool_.
2 An rvalue of an integer type can be converted to an rvalue of a float
ing point type. The result is exact if possible. Otherwise, it can
be either the next lower or higher representable value. Loss of pre
cision occurs if the integral value cannot be represented exactly as a
value of the floating type. If the source type is bool, the source
integer is taken to be zero for false and one for true.
4.10 Pointer conversions [conv.ptr]
1 A constant expression (_expr.const_) rvalue of an integer type that
evaluates to zero (called a null pointer constant) can be converted to
a pointer type. The result is a value (called the null pointer value
of that type) distinguishable from every pointer to an object or func
tion. Two null pointer values of a given type compare equal.
2 An rvalue of type pointer to cv T, where T is an object type, can be
converted to an rvalue of type pointer to cv void.
3 An rvalue of type pointer to cv D, where D is a class type, can be
converted to an rvalue of type pointer to cv B, where B is a base
class (_class.derived_) of D. If B is an inaccessible
(_class.access_) or ambiguous (_class.member.lookup_) base class of D,
a program that necessitates this conversion is ill-formed. The result
of the conversion is a pointer to the base class sub-object of the
derived class object. The null pointer value is converted to the null
pointer value of the destination type.
4.11 Pointer to member conversions [conv.mem]
1 A null pointer constant (_conv.ptr_) can be converted to a pointer to
member type. The result is a value (called the null member pointer
value of that type) distinguishable from a pointer to any member. Two
null member pointer values of a given type compare equal.
2 An rvalue of type pointer to member of B of type cv T, where B is a
class type, can be converted to an rvalue of type pointer to member of
D of type cv T, where D is a derived class (_class.derived_) of B. If
B is an inaccessible (_class.access_) or ambiguous
(_class.member.lookup_) base class of D, a program that necessitates
this conversion is ill-formed. The result of the conversion refers to
the same member as the pointer to member before the conversion took
place, but it refers to the base class member as if it were a member
of the derived class. The result refers to the member in D's instance
of B. Since the result has type pointer to member of D of type cv T,
it can be dereferenced with a D object. The result is the same as if
the pointer to member of B were dereferenced with the B sub-object of
D. The null member pointer value is converted to the null member
pointer value of the destination type.5)
4.12 Base class conversion [conv.class]
1 An rvalue of type cv D, where D is a class type, can be converted to
an rvalue of type cv B, where B is a base class (_class.derived_) of
D. If B is an inaccessible (_class.access_) or ambiguous
(_class.member.lookup_) base class of D, or if the conversion is
implemented by calling a constructor (_class.conv.ctor_) and the con
structor is not callable, a program that necessitates this conversion
is ill-formed. The result of the conversion is the value of the base
class sub-object of the derived class object.
_________________________
5) The rule for conversion of pointers to members (from pointer to
member of base to pointer to member of derived) appears inverted com
pared to the rule for pointers to objects (from pointer to derived to
pointer to base) (_conv.ptr_, _class.derived_). This inversion is
necessary to ensure type safety. Note that a pointer to member is not
a pointer to object or a pointer to function and the rules for conver
sions of such pointers do not apply to pointers to members. In par
ticular, a pointer to member cannot be converted to a void*.
4.13 Boolean conversions [conv.bool]
1 An rvalue of arithmetic, enumeration, pointer, or pointer to member
type can be converted to an rvalue of type bool. A zero value, null
pointer value, or null member pointer value is converted to false; any
other value is converted to true.
2 The conversions allowed as integral promotions are excluded from the
set of boolean conversions.