Files
2025-10-25 03:02:53 +03:00

829 lines
36 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[conv]
# 7 Expressions [[expr]](./#expr)
## 7.3 Standard conversions [conv]
### [7.3.1](#general) General [[conv.general]](conv.general)
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L503)
Standard conversions are implicit conversions with built-in meaning[.](#general-1.sentence-1)
[conv] enumerates the full set of such conversions[.](#general-1.sentence-2)
A[*standard conversion sequence*](#def:conversion_sequence,standard "7.3.1General[conv.general]") is a sequence of standard
conversions in the following order:
- [(1.1)](#general-1.1)
Zero or one conversion from the following set: lvalue-to-rvalue
conversion, array-to-pointer conversion, and function-to-pointer
conversion[.](#general-1.1.sentence-1)
- [(1.2)](#general-1.2)
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[.](#general-1.2.sentence-1)
- [(1.3)](#general-1.3)
Zero or one function pointer conversion[.](#general-1.3.sentence-1)
- [(1.4)](#general-1.4)
Zero or one qualification conversion[.](#general-1.4.sentence-1)
[*Note [1](#general-note-1)*:
A standard conversion sequence can be empty, i.e., it can consist of no
conversions[.](#general-1.sentence-4)
— *end note*]
A standard conversion sequence will be applied to
an expression if necessary to convert it to an expression having
a required destination type and value category[.](#general-1.sentence-5)
[2](#general-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L533)
[*Note [2](#general-note-2)*:
Expressions with a given type will be implicitly converted to other
types in several contexts:
- [(2.1)](#general-2.1)
When used as operands of operators[.](#general-2.1.sentence-1)
The operator's requirements
for its operands dictate the destination type ([[expr.compound]](expr.compound "7.6Compound expressions"))[.](#general-2.1.sentence-2)
- [(2.2)](#general-2.2)
When used in the condition of an if statement ([[stmt.if]](stmt.if "8.5.2The if statement")) or
iteration statement ([[stmt.iter]](stmt.iter "8.6Iteration statements"))[.](#general-2.2.sentence-1)
The destination type isbool[.](#general-2.2.sentence-2)
- [(2.3)](#general-2.3)
When used in the expression of a switch statement ([[stmt.switch]](stmt.switch "8.5.3The switch statement"))[.](#general-2.3.sentence-1)
The destination type is integral[.](#general-2.3.sentence-2)
- [(2.4)](#general-2.4)
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)[.](#general-2.4.sentence-1)
The type of the entity being initialized
is (generally) the destination type[.](#general-2.4.sentence-2)
See [[dcl.init]](dcl.init "9.5Initializers"), [[dcl.init.ref]](dcl.init.ref "9.5.4References")[.](#general-2.4.sentence-3)
— *end note*]
[3](#general-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L556)
An expression E can be[*implicitly converted*](#def:conversion,implicit "7.3.1General[conv.general]") to a type T if and only if the
declaration T t = E; is well-formed, for some invented temporary
variable t ([[dcl.init]](dcl.init "9.5Initializers"))[.](#general-3.sentence-1)
[4](#general-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L562)
Certain language constructs require that an expression be converted to a Boolean
value[.](#general-4.sentence-1)
An expression E appearing in such a context is said to be[*contextually converted to bool*](#def:conversion,contextual_to_bool "7.3.1General[conv.general]") and is well-formed if and only if
the declaration bool t(E); is well-formed, for some invented temporary
variable t ([[dcl.init]](dcl.init "9.5Initializers"))[.](#general-4.sentence-2)
[5](#general-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L569)
Certain language constructs require conversion to a value having
one of a specified set of types appropriate to the construct[.](#general-5.sentence-1)
An
expression E of class type C appearing in such a
context is said to be[*contextually implicitly converted*](#def:contextually_implicitly_converted "7.3.1General[conv.general]") 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:C is searched for non-explicit conversion functions
whose return type is cv T or reference to cvT such that T is allowed by the context[.](#general-5.sentence-2)
There shall be exactly one such T[.](#general-5.sentence-3)
[6](#general-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L583)
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[.](#general-6.sentence-1)
The result is an lvalue if T is an lvalue reference
type or an rvalue reference to function type ([[dcl.ref]](dcl.ref "9.3.4.3References")),
an xvalue if T is an rvalue reference to object type,
and a prvalue otherwise[.](#general-6.sentence-2)
The expression E is used as a glvalue if and only if the initialization uses it as a glvalue[.](#general-6.sentence-3)
[7](#general-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L593)
[*Note [3](#general-note-3)*:
For class types, user-defined conversions are considered as well;
see [[class.conv]](class.conv "11.4.8Conversions")[.](#general-7.sentence-1)
In general, an implicit conversion
sequence ([[over.best.ics]](over.best.ics "12.2.4.2Implicit conversion sequences")) consists of a standard conversion
sequence followed by a user-defined conversion followed by another
standard conversion sequence[.](#general-7.sentence-2)
— *end note*]
[8](#general-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L602)
[*Note [4](#general-note-4)*:
There are some contexts where certain conversions are suppressed[.](#general-8.sentence-1)
For
example, the lvalue-to-rvalue conversion is not done on the operand of
the unary & operator[.](#general-8.sentence-2)
Specific exceptions are given in the
descriptions of those operators and contexts[.](#general-8.sentence-3)
— *end note*]
### [7.3.2](#lval) Lvalue-to-rvalue conversion [[conv.lval]](conv.lval)
[1](#lval-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L612)
A [glvalue](basic.lval#def:glvalue "7.2.1Value category[basic.lval]") of a non-function, non-array type T can be converted to
a prvalue[.](#lval-1.sentence-1)[43](#footnote-43 "For historical reasons, this conversion is called the “lvalue-to-rvalue” conversion, even though that name does not accurately reflect the taxonomy of expressions described in [basic.lval].")
If T is an incomplete type, a
program that necessitates this conversion is ill-formed[.](#lval-1.sentence-2)
If T is a non-class type, the type of the prvalue is
the cv-unqualified version of T[.](#lval-1.sentence-3)
Otherwise, the type of the
prvalue is T[.](#lval-1.sentence-4)[44](#footnote-44 "In C++ class and array prvalues can have cv-qualified types. This differs from C, in which non-lvalues never have cv-qualified types.")
[2](#lval-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L634)
When an lvalue-to-rvalue conversion
is applied to an expression E, and either
- [(2.1)](#lval-2.1)
E is not potentially evaluated, or
- [(2.2)](#lval-2.2)
the evaluation of E results in the evaluation of a member Ex of the set of potential results of E, and Ex names a variable x that is not odr-used by Ex ([[basic.def.odr]](basic.def.odr "6.3One-definition rule")),
the value contained in the referenced object is not accessed[.](#lval-2.sentence-1)
[*Example [1](#lval-example-1)*: struct S { int n; };auto f() { S x { 1 };constexpr S y { 2 };return [&](bool b) { return (b ? y : x).n; };}auto g = f();int m = g(false); // undefined behavior: access of x.n outside its lifetimeint n = g(true); // OK, does not access y.n — *end example*]
[3](#lval-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L659)
The result of the conversion is determined according to the
following rules:
- [(3.1)](#lval-3.1)
If T is cv std::nullptr_t, the result is a
null pointer constant ([[conv.ptr]](#ptr "7.3.12Pointer conversions"))[.](#lval-3.1.sentence-1)
[*Note [1](#lval-note-1)*:
Since the conversion does not access the object to which the glvalue refers,
there is no side effect even if T is volatile-qualified ([[intro.execution]](intro.execution "6.10.1Sequential execution")), and
the glvalue can refer to an inactive member of a union ([[class.union]](class.union "11.5Unions"))[.](#lval-3.1.sentence-2)
— *end note*]
- [(3.2)](#lval-3.2)
Otherwise, if T has a class
type, the conversion copy-initializes the result object from
the glvalue[.](#lval-3.2.sentence-1)
- [(3.3)](#lval-3.3)
Otherwise, if the object to which the glvalue refers contains an invalid
pointer value ([[basic.compound]](basic.compound "6.9.4Compound types")), the behavior isimplementation-defined[.](#lval-3.3.sentence-1)
- [(3.4)](#lval-3.4)
Otherwise, if the bits in the value representation of
the object to which the glvalue refers
are not valid for the object's type, the behavior is undefined[.](#lval-3.4.sentence-1)
[*Example [2](#lval-example-2)*: bool f() {bool b = true; char c = 42;
memcpy(&b, &c, 1); return b; // undefined behavior if 42 is not a valid value representation for bool} — *end example*]
- [(3.5)](#lval-3.5)
Otherwise, the object indicated by the glvalue is read ([[defns.access]](defns.access "3.1access"))[.](#lval-3.5.sentence-1)
Let V be the value contained in the object[.](#lval-3.5.sentence-2)
If T is an integer type,
the prvalue result is
the value of type T congruent ([[basic.fundamental]](basic.fundamental "6.9.2Fundamental types")) to V, andV otherwise[.](#lval-3.5.sentence-3)
[4](#lval-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L702)
[*Note [2](#lval-note-2)*:
See also [[basic.lval]](basic.lval "7.2.1Value category")[.](#lval-4.sentence-1)
— *end note*]
[43)](#footnote-43)[43)](#footnoteref-43)
For historical reasons, this conversion is called the “lvalue-to-rvalue”
conversion, even though that name does not accurately reflect the taxonomy
of expressions described in [[basic.lval]](basic.lval "7.2.1Value category")[.](#footnote-43.sentence-1)
[44)](#footnote-44)[44)](#footnoteref-44)
In C++ class and array prvalues can have cv-qualified types[.](#footnote-44.sentence-1)
This differs from C, in which non-lvalues never have
cv-qualified types[.](#footnote-44.sentence-2)
### [7.3.3](#array) Array-to-pointer conversion [[conv.array]](conv.array)
[1](#array-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L709)
An lvalue or rvalue of type “array of N T” or “array
of unknown bound of T” can be converted to a prvalue of type
“pointer to T”[.](#array-1.sentence-1)
The temporary materialization conversion ([[conv.rval]](#rval "7.3.5Temporary materialization conversion")) is applied[.](#array-1.sentence-2)
The result is a pointer to the first element of the array[.](#array-1.sentence-3)
### [7.3.4](#func) Function-to-pointer conversion [[conv.func]](conv.func)
[1](#func-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L721)
An lvalue of function type T can be converted to a prvalue of
type “pointer to T”[.](#func-1.sentence-1)
The result is a pointer to the
function[.](#func-1.sentence-2)[45](#footnote-45 "This conversion never applies to non-static member functions because an lvalue that refers to a non-static member function cannot be obtained.")
[45)](#footnote-45)[45)](#footnoteref-45)
This conversion never applies to non-static member functions because an
lvalue that refers to a non-static member function cannot be obtained[.](#footnote-45.sentence-1)
### [7.3.5](#rval) Temporary materialization conversion [[conv.rval]](conv.rval)
[1](#rval-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L734)
A prvalue of type T can be converted to an xvalue of type T[.](#rval-1.sentence-1)
This conversion initializes a temporary object ([[class.temporary]](class.temporary "6.8.7Temporary objects")) of type T from the prvalue
by evaluating the prvalue with the temporary object as its result object,
and produces an xvalue denoting the temporary object[.](#rval-1.sentence-2)
T shall be a complete type[.](#rval-1.sentence-3)
[*Note [1](#rval-note-1)*:
If T is a class type (or array thereof),
it must have an accessible and non-deleted destructor;
see [[class.dtor]](class.dtor "11.4.7Destructors")[.](#rval-1.sentence-4)
— *end note*]
[*Example [1](#rval-example-1)*: struct X { int n; };int k = X().n; // OK, X() prvalue is converted to xvalue — *end example*]
### [7.3.6](#qual) Qualification conversions [[conv.qual]](conv.qual)
[1](#qual-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L755)
A [*qualification-decomposition*](#def:qualification-decomposition "7.3.6Qualification conversions[conv.qual]") of a type T is a sequence ofcvi and Pi such that T is “cv0 P0 cv1 P1 ⋯ cvn−1 Pn−1 cvn U” for n ≥ 0, where
each cvi is a set of cv-qualifiers ([[basic.type.qualifier]](basic.type.qualifier "6.9.5CV-qualifiers")), and
each Pi is
“pointer to” ([[dcl.ptr]](dcl.ptr "9.3.4.2Pointers")),
“pointer to member of class Ci of type” ([[dcl.mptr]](dcl.mptr "9.3.4.4Pointers to members")),
“array of Ni”, or
“array of unknown bound of” ([[dcl.array]](dcl.array "9.3.4.5Arrays"))[.](#qual-1.sentence-1)
If Pi designates an array,
the cv-qualifiers cvi+1 on the element type are also taken as
the cv-qualifiers cvi of the array[.](#qual-1.sentence-2)
[*Example [1](#qual-example-1)*:
The type denoted by the [*type-id*](dcl.name#nt:type-id "9.3.2Type names[dcl.name]") const int ** has three qualification-decompositions,
taking U as “int”,
as “pointer to const int”, and
as “pointer to pointer to const int”[.](#qual-1.sentence-3)
— *end example*]
[2](#qual-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L782)
Two types T1 and T2 are [*similar*](#def:similar_types "7.3.6Qualification conversions[conv.qual]") if
they have qualification-decompositions with the same n such that corresponding Pi components are either the same
or one is “array of Ni” and the other is “array of unknown bound of”,
and the types denoted by U are the same[.](#qual-2.sentence-1)
[3](#qual-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L790)
The [*qualification-combined type*](#def:type,qualification-combined "7.3.6Qualification conversions[conv.qual]") of two types T1 and T2 is the type T3 similar to T1 whose qualification-decomposition is such that:
- [(3.1)](#qual-3.1)
for every i>0, cv3i is the union ofcv1i and cv2i,
- [(3.2)](#qual-3.2)
if either P1i or P2i is “array of unknown bound of”,P3i is “array of unknown bound of”, otherwise it is P1i, and
- [(3.3)](#qual-3.3)
if the resulting cv3i is different from cv1i or cv2i,
or the resulting P3i is different from P1i or P2i,
then const is added to every cv3k for 0<k<i,
where cvji and Pji are the components of
the qualification-decomposition of Tj[.](#qual-3.sentence-1)
A prvalue of type T1 can be converted to type T2 if the qualification-combined type of T1 and T2 is T2[.](#qual-3.sentence-2)
[*Note [1](#qual-note-1)*:
If a program could assign a pointer of type T** to a pointer of
type const T** (that is, if line #1 below were
allowed), a program could inadvertently modify a const object
(as it is done on line #2)[.](#qual-3.sentence-3)
For example,int main() {const char c = 'c';char* pc;const char** pcc = &pc; // #1: not allowed*pcc = &c;*pc = 'C'; // #2: modifies a const object}
— *end note*]
[*Note [2](#qual-note-2)*:
Given similar types T1 and T2, this
construction ensures that
both can be converted to the qualification-combined type of T1 and T2[.](#qual-3.sentence-5)
— *end note*]
[4](#qual-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L832)
[*Note [3](#qual-note-3)*:
A prvalue of type “pointer to *cv1* € can be
converted to a prvalue of type “pointer to *cv2* € if
“*cv2* € is more cv-qualified than “*cv1*€[.](#qual-4.sentence-1)
A prvalue of type “pointer to member of X of type *cv1*€ can be converted to a prvalue of type “pointer to member
of X of type *cv2* € if “*cv2*€ is more cv-qualified than “*cv1* €[.](#qual-4.sentence-2)
— *end note*]
[5](#qual-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L844)
[*Note [4](#qual-note-4)*:
Function types (including those used in pointer-to-member-function types)
are never cv-qualified ([[dcl.fct]](dcl.fct "9.3.4.6Functions"))[.](#qual-5.sentence-1)
— *end note*]
### [7.3.7](#prom) Integral promotions [[conv.prom]](conv.prom)
[1](#prom-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L853)
For the purposes of [[conv.prom]](#prom "7.3.7Integral promotions"),
a [*converted bit-field*](#def:bit-field,converted "7.3.7Integral promotions[conv.prom]") is a prvalue that is the result of
an lvalue-to-rvalue conversion ([[conv.lval]](#lval "7.3.2Lvalue-to-rvalue conversion")) applied to
a bit-field ([[class.bit]](class.bit "11.4.10Bit-fields"))[.](#prom-1.sentence-1)
[2](#prom-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L859)
A prvalue that is not a converted bit-field and has an integer type other thanbool, char8_t, char16_t,char32_t, or wchar_t whose integer conversion
rank ([[conv.rank]](conv.rank "6.9.6Conversion ranks")) is less than the rank of int can be
converted to a prvalue of type int if int can represent
all the values of the source type; otherwise, the source prvalue can be
converted to a prvalue of type unsigned int[.](#prom-2.sentence-1)
[3](#prom-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L869)
A prvalue of an unscoped enumeration type whose underlying type is not
fixed can be converted to a prvalue of the first of the following
types that can represent all the values of the enumeration ([[dcl.enum]](dcl.enum "9.8.1Enumeration declarations")): int,unsigned int, long int, unsigned long int,long long int, or unsigned long long int[.](#prom-3.sentence-1)
If none of the types in that
list can represent all the values of the enumeration, a prvalue of an unscoped
enumeration type can be converted to a prvalue of the extended integer type with lowest
integer conversion rank ([[conv.rank]](conv.rank "6.9.6Conversion ranks")) greater than the rank of long long in which all the values of the enumeration can be represented[.](#prom-3.sentence-2)
If there are
two such extended types, the signed one is chosen[.](#prom-3.sentence-3)
[4](#prom-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L882)
A prvalue of an unscoped enumeration type whose underlying type is
fixed ([[dcl.enum]](dcl.enum "9.8.1Enumeration declarations")) can be converted to a prvalue of its underlying type[.](#prom-4.sentence-1)
Moreover,
if integral promotion can be applied to its underlying type, a prvalue of an unscoped
enumeration type whose underlying type is fixed can also be converted to a prvalue of
the promoted underlying type[.](#prom-4.sentence-2)
[*Note [1](#prom-note-1)*:
A converted bit-field of enumeration type is treated as
any other value of that type for promotion purposes[.](#prom-4.sentence-3)
— *end note*]
[5](#prom-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L893)
A converted bit-field of integral type can be converted
to a prvalue of type int if int can represent all the
values of the bit-field; otherwise, it can be converted tounsigned int if unsigned int can represent all the
values of the bit-field[.](#prom-5.sentence-1)
[6](#prom-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L900)
A prvalue of typechar8_t, char16_t, char32_t, orwchar_t ([[basic.fundamental]](basic.fundamental "6.9.2Fundamental types"))
(including a converted bit-field that was not
already promoted to int or unsigned int according to the rules above)
can be converted to a prvalue
of the first of the following types that can represent all the values of
its underlying type:int,unsigned int,long int,unsigned long int,long long int,unsigned long long int, or
its underlying type[.](#prom-6.sentence-1)
[7](#prom-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L921)
A prvalue of type bool can be converted to a prvalue of typeint, with false becoming zero and true becoming
one[.](#prom-7.sentence-1)
[8](#prom-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L927)
These conversions are called [*integral promotions*](#def:integral_promotion "7.3.7Integral promotions[conv.prom]")[.](#prom-8.sentence-1)
### [7.3.8](#fpprom) Floating-point promotion [[conv.fpprom]](conv.fpprom)
[1](#fpprom-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L932)
A prvalue of type float can be converted to a prvalue of typedouble[.](#fpprom-1.sentence-1)
The value is unchanged[.](#fpprom-1.sentence-2)
[2](#fpprom-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L937)
This conversion is called [*floating-point promotion*](#def:floating-point_promotion "7.3.8Floating-point promotion[conv.fpprom]")[.](#fpprom-2.sentence-1)
### [7.3.9](#integral) Integral conversions [[conv.integral]](conv.integral)
[1](#integral-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L942)
A prvalue of an integer type can be converted to a prvalue of another
integer type[.](#integral-1.sentence-1)
A prvalue of an unscoped enumeration type can be converted to
a prvalue of an integer type[.](#integral-1.sentence-2)
[2](#integral-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L948)
If the destination type is bool, see [[conv.bool]](#bool "7.3.15Boolean conversions")[.](#integral-2.sentence-1)
If the
source type is bool, the value false is converted to
zero and the value true is converted to one[.](#integral-2.sentence-2)
[3](#integral-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L954)
Otherwise, the result is the unique value of the destination type
that is congruent to the source integer modulo 2N,
where N is the width of the destination type[.](#integral-3.sentence-1)
[4](#integral-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L961)
The conversions allowed as integral promotions are excluded from the set
of integral conversions[.](#integral-4.sentence-1)
### [7.3.10](#double) Floating-point conversions [[conv.double]](conv.double)
[1](#double-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L967)
A prvalue of floating-point type can be converted to a prvalue of
another floating-point type
with a greater or equal conversion rank ([[conv.rank]](conv.rank "6.9.6Conversion ranks"))[.](#double-1.sentence-1)
A prvalue of standard floating-point type can be converted to
a prvalue of another standard floating-point type[.](#double-1.sentence-2)
[2](#double-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L975)
If the source value can be exactly
represented in the destination type, the result of the conversion is
that exact representation[.](#double-2.sentence-1)
If the source value is between two adjacent
destination values, the result of the conversion is animplementation-defined choice of either of those values[.](#double-2.sentence-2)
Otherwise, the behavior is undefined[.](#double-2.sentence-3)
[3](#double-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L983)
The conversions allowed as floating-point promotions are excluded from
the set of floating-point conversions[.](#double-3.sentence-1)
### [7.3.11](#fpint) Floating-integral conversions [[conv.fpint]](conv.fpint)
[1](#fpint-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L989)
A prvalue of a floating-point type can be converted to a prvalue of an
integer type[.](#fpint-1.sentence-1)
The conversion truncates; that is, the fractional part is
discarded[.](#fpint-1.sentence-2)
The behavior is undefined if the truncated value cannot be represented
in the destination type[.](#fpint-1.sentence-3)
[*Note [1](#fpint-note-1)*:
If the destination type is bool, see [[conv.bool]](#bool "7.3.15Boolean conversions")[.](#fpint-1.sentence-4)
— *end note*]
[2](#fpint-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1001)
A prvalue of an integer type or of an unscoped enumeration type can be converted to
a prvalue of a floating-point type[.](#fpint-2.sentence-1)
The result is exact if possible[.](#fpint-2.sentence-2)
If the value being
converted is in the range of values that can be represented but the value cannot be
represented exactly, it is an implementation-defined choice of either the next lower or higher representable
value[.](#fpint-2.sentence-3)
[*Note [2](#fpint-note-2)*:
Loss of precision occurs if the integral value cannot be represented
exactly as a value of the floating-point type[.](#fpint-2.sentence-4)
— *end note*]
If the value being converted is
outside the range of values that can be represented, the behavior is undefined[.](#fpint-2.sentence-5)
If the
source type is bool, the value false is converted to zero and the valuetrue is converted to one[.](#fpint-2.sentence-6)
### [7.3.12](#ptr) Pointer conversions [[conv.ptr]](conv.ptr)
[1](#ptr-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1022)
A [*null pointer constant*](#def:constant,null_pointer "7.3.12Pointer conversions[conv.ptr]") is an integer literal ([[lex.icon]](lex.icon "5.13.2Integer literals")) with
value zero
or a prvalue of type std::nullptr_t[.](#ptr-1.sentence-1)
A null pointer constant can be
converted to a pointer type; the
result is the null pointer value of that type ([[basic.compound]](basic.compound "6.9.4Compound types")) and is
distinguishable from every other value of
object pointer or function pointer
type[.](#ptr-1.sentence-2)
Such a conversion is called a [*null pointer conversion*](#def:conversion,null_pointer "7.3.12Pointer conversions[conv.ptr]")[.](#ptr-1.sentence-3)
The conversion of a null pointer constant to a pointer to
cv-qualified type is a single conversion, and not the sequence of a
pointer conversion followed by a qualification
conversion ([[conv.qual]](#qual "7.3.6Qualification conversions"))[.](#ptr-1.sentence-4)
A null pointer constant of integral type
can be converted to a prvalue of type std::nullptr_t[.](#ptr-1.sentence-5)
[*Note [1](#ptr-note-1)*:
The resulting prvalue is not a null pointer value[.](#ptr-1.sentence-6)
— *end note*]
[2](#ptr-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1045)
A prvalue of type “pointer to cv €, where T is an object type, can be converted to a prvalue of type “pointer tocv void”[.](#ptr-2.sentence-1)
The pointer value ([[basic.compound]](basic.compound "6.9.4Compound types")) is unchanged by this conversion[.](#ptr-2.sentence-2)
[3](#ptr-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1051)
A prvalue v of type “pointer to cv €, where D is a complete class type, can be converted to a prvalue of type “pointer tocv €, where B is a base class ([[class.derived]](class.derived "11.7Derived classes"))
of D[.](#ptr-3.sentence-1)
If B is an
inaccessible ([[class.access]](class.access "11.8Member access control")) or
ambiguous ([[class.member.lookup]](class.member.lookup "6.5.2Member name lookup")) base class of D, a program
that necessitates this conversion is ill-formed[.](#ptr-3.sentence-2)
If v is a null pointer value,
the result is a null pointer value[.](#ptr-3.sentence-3)
Otherwise,
if B is a virtual base class of D andv does not point to an object
whose type is similar ([[conv.qual]](#qual "7.3.6Qualification conversions")) to D and
that is
within its lifetime or
within its period of construction or destruction ([[class.cdtor]](class.cdtor "11.9.5Construction and destruction")),
the behavior is undefined[.](#ptr-3.sentence-4)
Otherwise,
the result is a pointer to the base class subobject of
the derived class object[.](#ptr-3.sentence-5)
### [7.3.13](#mem) Pointer-to-member conversions [[conv.mem]](conv.mem)
[1](#mem-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1075)
A [null pointer constant](#def:constant,null_pointer "7.3.12Pointer conversions[conv.ptr]") can be converted to a
pointer-to-member
type; the result is the [*null member pointer value*](#def:value,null_member_pointer "7.3.13Pointer-to-member conversions[conv.mem]") of that type and is distinguishable from any pointer to member not
created from a null pointer constant[.](#mem-1.sentence-1)
Such a conversion is called a [*null member pointer conversion*](#def:conversion,null_member_pointer "7.3.13Pointer-to-member conversions[conv.mem]")[.](#mem-1.sentence-2)
The conversion of a null pointer
constant to a pointer to member of cv-qualified type is a single
conversion, and not the sequence of a pointer-to-member conversion
followed by a qualification conversion ([[conv.qual]](#qual "7.3.6Qualification conversions"))[.](#mem-1.sentence-3)
[2](#mem-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1090)
A prvalue of type “pointer to member of B of type cvT”, where B is a class type, can be converted to
a prvalue of type “pointer to member of D of type cvT”, where D is a complete class derived ([[class.derived]](class.derived "11.7Derived classes"))
from B[.](#mem-2.sentence-1)
If B is an
inaccessible ([[class.access]](class.access "11.8Member access control")),
ambiguous ([[class.member.lookup]](class.member.lookup "6.5.2Member name lookup")), or virtual ([[class.mi]](class.mi "11.7.2Multiple base classes")) base
class of D, or a base class of a virtual base class ofD, a program that necessitates this conversion is ill-formed[.](#mem-2.sentence-2)
If class D does not contain the original member and
is not a base class of the class containing the original member,
the behavior is undefined[.](#mem-2.sentence-3)
Otherwise,
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[.](#mem-2.sentence-4)
The result refers to
the member in D's instance of B[.](#mem-2.sentence-5)
Since the result has
type “pointer to member of D of type cv €,
indirection through it with a D object is valid[.](#mem-2.sentence-6)
The result is the same
as if indirecting through the pointer to member of B with theB subobject of D[.](#mem-2.sentence-7)
The null member pointer value is
converted to the null member pointer value of the destination
type[.](#mem-2.sentence-8)[46](#footnote-46 "The rule for conversion of pointers to members (from pointer to member of base to pointer to member of derived) appears inverted compared 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 an object pointer or a function pointer and the rules for conversions of such pointers do not apply to pointers to members. In particular, a pointer to member cannot be converted to a void*.")
[46)](#footnote-46)[46)](#footnoteref-46)
The rule for conversion of pointers to members (from pointer to member
of base to pointer to member of derived) appears inverted compared to
the rule for pointers to objects (from pointer to derived to pointer to
base) ([[conv.ptr]](#ptr "7.3.12Pointer conversions"), [[class.derived]](class.derived "11.7Derived classes"))[.](#footnote-46.sentence-1)
This inversion is
necessary to ensure type safety[.](#footnote-46.sentence-2)
Note that a pointer to member is not
an object pointer or a function pointer
and the rules for conversions
of such pointers do not apply to pointers to members[.](#footnote-46.sentence-3)
In particular, a pointer to member cannot be converted to avoid*[.](#footnote-46.sentence-4)
### [7.3.14](#fctptr) Function pointer conversions [[conv.fctptr]](conv.fctptr)
[1](#fctptr-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1129)
A prvalue of type “pointer to noexcept function”
can be converted to a prvalue of type “pointer to function”[.](#fctptr-1.sentence-1)
The result is a pointer to the function[.](#fctptr-1.sentence-2)
A prvalue of type “pointer to member of type noexcept function”
can be converted to a prvalue of type “pointer to member of type function”[.](#fctptr-1.sentence-3)
The result designates the member function[.](#fctptr-1.sentence-4)
[*Example [1](#fctptr-example-1)*: void (*p)();void (**pp)() noexcept = &p; // error: cannot convert to pointer to noexcept functionstruct S { typedef void (*p)(); operator p(); };void (*q)() noexcept = S(); // error: cannot convert to pointer to noexcept function — *end example*]
### [7.3.15](#bool) Boolean conversions [[conv.bool]](conv.bool)
[1](#bool-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L1150)
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member
type can be converted to a prvalue of type bool[.](#bool-1.sentence-1)
A zero value, null
pointer value, or null member pointer value is converted to false; any
other value is converted to true[.](#bool-1.sentence-2)