225 lines
9.6 KiB
Markdown
225 lines
9.6 KiB
Markdown
[expr.cond]
|
||
|
||
# 7 Expressions [[expr]](./#expr)
|
||
|
||
## 7.6 Compound expressions [[expr.compound]](expr.compound#expr.cond)
|
||
|
||
### 7.6.16 Conditional operator [expr.cond]
|
||
|
||
[conditional-expression:](#nt:conditional-expression "7.6.16 Conditional operator [expr.cond]")
|
||
[*logical-or-expression*](expr.log.or#nt:logical-or-expression "7.6.15 Logical OR operator [expr.log.or]")
|
||
[*logical-or-expression*](expr.log.or#nt:logical-or-expression "7.6.15 Logical OR operator [expr.log.or]") ? [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]") : [*assignment-expression*](expr.assign#nt:assignment-expression "7.6.19 Assignment and compound assignment operators [expr.assign]")
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7798)
|
||
|
||
Conditional expressions group right-to-left[.](#1.sentence-1)
|
||
|
||
The first expression is
|
||
contextually converted to bool ([[conv]](conv "7.3 Standard conversions"))[.](#1.sentence-2)
|
||
|
||
It is
|
||
evaluated and if it is true, the result of the conditional
|
||
expression is the value of the second expression, otherwise that of the
|
||
third expression[.](#1.sentence-3)
|
||
|
||
Only one of the second and third expressions is
|
||
evaluated[.](#1.sentence-4)
|
||
|
||
The first expression is sequenced before
|
||
the second or third expression ([[intro.execution]](intro.execution "6.10.1 Sequential execution"))[.](#1.sentence-5)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7809)
|
||
|
||
If either the second or the third operand has type void,
|
||
one of the following shall hold:
|
||
|
||
- [(2.1)](#2.1)
|
||
|
||
The second or the third operand (but not both) is a (possibly
|
||
parenthesized) [*throw-expression*](expr.throw#nt:throw-expression "7.6.18 Throwing an exception [expr.throw]") ([[expr.throw]](expr.throw "7.6.18 Throwing an exception")); the result
|
||
is of the type and value category of the other[.](#2.1.sentence-1)
|
||
The [*conditional-expression*](#nt:conditional-expression "7.6.16 Conditional operator [expr.cond]") is a bit-field if that operand is a bit-field[.](#2.1.sentence-2)
|
||
|
||
- [(2.2)](#2.2)
|
||
|
||
Both the second and the third operands have type void; the
|
||
result is of type void and is a prvalue[.](#2.2.sentence-1)
|
||
[*Note [1](#note-1)*:
|
||
This
|
||
includes the case where both operands are [*throw-expression*](expr.throw#nt:throw-expression "7.6.18 Throwing an exception [expr.throw]")*s*[.](#2.2.sentence-2)
|
||
â *end note*]
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7829)
|
||
|
||
Otherwise, if the second and third operand are glvalue bit-fields
|
||
of the same value category and
|
||
of types *cv1* T and *cv2* T, respectively,
|
||
the operands are considered to be of type cv T for the remainder of this subclause,
|
||
where cv is the union of *cv1* and *cv2*[.](#3.sentence-1)
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7837)
|
||
|
||
Otherwise, if the second and third operand have different types and
|
||
either has (possibly cv-qualified) class type, or if both
|
||
are glvalues of the same value category and the same type except for
|
||
cv-qualification, an attempt is made to
|
||
form an [implicit conversion sequence](over.best.ics "12.2.4.2 Implicit conversion sequences [over.best.ics]") from
|
||
each of those operands to the type of the other[.](#4.sentence-1)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
Properties such as access, whether an operand is a bit-field, or whether
|
||
a conversion function is deleted are ignored for that determination[.](#4.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
Attempts are made to form an implicit conversion sequence
|
||
from an operand expression E1 of type T1 to a target type related to the type T2 of the operand expression E2 as follows:
|
||
|
||
- [(4.1)](#4.1)
|
||
|
||
If E2 is an lvalue, the target type is
|
||
âlvalue reference to T2â,
|
||
but an implicit conversion sequence can only be formed
|
||
if the reference would bind directly ([[dcl.init.ref]](dcl.init.ref "9.5.4 References"))
|
||
to a glvalue[.](#4.1.sentence-1)
|
||
|
||
- [(4.2)](#4.2)
|
||
|
||
If E2 is an xvalue, the target type is
|
||
ârvalue reference to T2â,
|
||
but an implicit conversion sequence can only be formed
|
||
if the reference would bind directly[.](#4.2.sentence-1)
|
||
|
||
- [(4.3)](#4.3)
|
||
|
||
If E2 is a prvalue or if neither of the conversion sequences above can be
|
||
formed and at least one of the operands has (possibly cv-qualified) class type:
|
||
* [(4.3.1)](#4.3.1)
|
||
|
||
if T1 and T2 are the same class type
|
||
(ignoring cv-qualification):
|
||
+
|
||
[(4.3.1.1)](#4.3.1.1)
|
||
if T2 is at least as cv-qualified as T1,
|
||
the target type is T2,
|
||
|
||
+
|
||
[(4.3.1.2)](#4.3.1.2)
|
||
otherwise, no conversion sequence is formed for this operand;
|
||
|
||
* [(4.3.2)](#4.3.2)
|
||
|
||
otherwise, if T2 is a base class of T1,
|
||
the target type is *cv1* T2, where *cv1* denotes the cv-qualifiers of T1;
|
||
|
||
* [(4.3.3)](#4.3.3)
|
||
|
||
otherwise, the target type is the type that E2 would have
|
||
after applying the[lvalue-to-rvalue](conv.lval "7.3.2 Lvalue-to-rvalue conversion [conv.lval]"),[array-to-pointer](conv.array "7.3.3 Array-to-pointer conversion [conv.array]"), and[function-to-pointer](conv.func "7.3.4 Function-to-pointer conversion [conv.func]") standard conversions[.](#4.3.sentence-1)
|
||
|
||
Using this process, it is determined whether an implicit conversion
|
||
sequence can be formed from the second operand
|
||
to the target type determined for the third operand, and vice versa,
|
||
with the following outcome:
|
||
|
||
- [(4.4)](#4.4)
|
||
|
||
If both sequences can be formed, or one can be formed but it is the
|
||
ambiguous conversion sequence, the program is ill-formed[.](#4.4.sentence-1)
|
||
|
||
- [(4.5)](#4.5)
|
||
|
||
If no conversion sequence can be formed, the operands are left unchanged
|
||
and further checking is performed as described below[.](#4.5.sentence-1)
|
||
|
||
- [(4.6)](#4.6)
|
||
|
||
Otherwise, if exactly one conversion sequence can be formed,
|
||
that conversion is applied to the chosen operand
|
||
and the converted operand is used in place of the original operand for
|
||
the remainder of this subclause[.](#4.6.sentence-1)
|
||
[*Note [3](#note-3)*:
|
||
The conversion might be ill-formed even if an implicit conversion
|
||
sequence could be formed[.](#4.6.sentence-2)
|
||
â *end note*]
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7909)
|
||
|
||
If the second and third operands are glvalues of the same value category
|
||
and have the same type, the
|
||
result is of that type and value category and it is a bit-field if the
|
||
second or the third operand is a bit-field, or if both are bit-fields[.](#5.sentence-1)
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7915)
|
||
|
||
Otherwise, the result is a prvalue[.](#6.sentence-1)
|
||
|
||
If the second and third operands do
|
||
not have the same type, and either has (possibly cv-qualified) class
|
||
type, overload resolution is used to determine the conversions (if any)
|
||
to be applied to the operands ([[over.match.oper]](over.match.oper "12.2.2.3 Operators in expressions"), [[over.built]](over.built "12.5 Built-in operators"))[.](#6.sentence-2)
|
||
|
||
If the overload resolution fails, the program is ill-formed[.](#6.sentence-3)
|
||
|
||
Otherwise,
|
||
the conversions thus determined are applied, and the converted operands
|
||
are used in place of the original operands for the remainder of this
|
||
subclause[.](#6.sentence-4)
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L7925)
|
||
|
||
[Array-to-pointer](conv.array "7.3.3 Array-to-pointer conversion [conv.array]") and [function-to-pointer](conv.func "7.3.4 Function-to-pointer conversion [conv.func]") standard conversions are
|
||
performed on the second and third operands[.](#7.sentence-1)
|
||
|
||
After those conversions, one
|
||
of the following shall hold:
|
||
|
||
- [(7.1)](#7.1)
|
||
|
||
The second and third operands have the same type; the result is of
|
||
that type and the result is copy-initialized using the selected operand[.](#7.1.sentence-1)
|
||
|
||
- [(7.2)](#7.2)
|
||
|
||
The second and third operands have arithmetic or enumeration type;
|
||
the [usual arithmetic conversions](expr.arith.conv "7.4 Usual arithmetic conversions [expr.arith.conv]") are performed to bring them to a common
|
||
type, and the result is of that type[.](#7.2.sentence-1)
|
||
|
||
- [(7.3)](#7.3)
|
||
|
||
One or both of the second and third operands have pointer type;[lvalue-to-rvalue](conv.lval "7.3.2 Lvalue-to-rvalue conversion [conv.lval]"),[pointer](conv.ptr "7.3.12 Pointer conversions [conv.ptr]"),[function pointer](conv.fctptr "7.3.14 Function pointer conversions [conv.fctptr]"), and[qualification conversions](conv.qual "7.3.6 Qualification conversions [conv.qual]") are performed to bring them to their[composite pointer type](expr.type#def:composite_pointer_type "7.2.2 Type [expr.type]")[.](#7.3.sentence-1)
|
||
The result is of the composite
|
||
pointer type[.](#7.3.sentence-2)
|
||
|
||
- [(7.4)](#7.4)
|
||
|
||
One or both of the second and third operands have pointer-to-member type;
|
||
lvalue-to-rvalue ([[conv.lval]](conv.lval "7.3.2 Lvalue-to-rvalue conversion")),
|
||
pointer to member ([[conv.mem]](conv.mem "7.3.13 Pointer-to-member conversions")),
|
||
function pointer ([[conv.fctptr]](conv.fctptr "7.3.14 Function pointer conversions")), and
|
||
qualification conversions ([[conv.qual]](conv.qual "7.3.6 Qualification conversions"))
|
||
are performed to bring them to their composite
|
||
pointer type ([[expr.type]](expr.type "7.2.2 Type"))[.](#7.4.sentence-1)
|
||
The result is of the composite pointer type[.](#7.4.sentence-2)
|
||
|
||
- [(7.5)](#7.5)
|
||
|
||
Both the second and third operands have type std::nullptr_t or one has
|
||
that type and the other is a null pointer constant[.](#7.5.sentence-1)
|
||
The result is of typestd::nullptr_t[.](#7.5.sentence-2)
|