Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It cannot return a <em>lvalue</em> since it will have to implicitly promote the type of <code>x</code> to match the type of <code>y</code> (since both sides of <code>:</code> are not of the same type), and with that it has to create a temporary.</p> <hr> <h2>What does the standard say? (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf" rel="noreferrer">n1905</a>)</h2> <p><strong>Expressions 5.17 Assignment and compound assignment operators</strong></p> <blockquote> <p><strong>5.17/3</strong></p> <p>If the second and third operand have different types, and either has (possibly cv-qualified) class type, an attempt is made to convert each of those operands to the type of the other. The process for determining whether an operand expression E1 of type T1 can be converted to match an operand expression E2 of type T2 is defined as follows:</p> <p>— If E2 is an lvalue: E1 can be converted to match E2 if E1 can be implicitly converted (clause 4) to the type “reference to T2”, subject to the constraint that in the conversion the reference must bind directly (8.5.3) to E1.</p> <p>— If E2 is an rvalue, or if the conversion above cannot be done:</p> <p>— if E1 and E2 have class type, and the underlying class types are the same or one is a base class of the other: E1 can be converted to match E2 if the class of T2 is the same type as, or a base class of, the class of T1, and the cv-qualification of T2 is the same cv-qualification as, or a greater cv-qualification than, the cv-qualification of T1. If the conversion is applied, E1 is changed to an rvalue of type T2 that still refers to the original source class object (or the appropriate subobject thereof). [<em>Note: that is, no copy is made. — end note</em>] by copy-initializing a temporary of type T2 from E1 and using that temporary as the converted operand.</p> <p>Otherwise (i.e., if <code>E1</code> or E2 has a non class type, or if they both have class types but the underlying classes are not either the same or one a base class of the other): E1 can be converted to match E2 if E1 can be implicitly converted to the type that expression E2 would have if E2 were converted to an rvalue (or the type it has, if E2 is an rvalue).</p> <p>Using this process, It is determined whether the second operand can be converted to match the third operand, and whether the third operand can be converted to match the second operand. If both can be converted, or one can be converted but the conversion is ambiguous, the program is ill-formed. If neither can be converted, the operands are left unchanged and further checking is performed as described below. If exactly one conversion is possible, 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 section.</p> </blockquote> <hr> <blockquote> <p><strong>5.17/4</strong></p> <p>If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.</p> </blockquote> <hr> <blockquote> <p><strong>5.17/5</strong></p> <p>Otherwise, the result is an rvalue. 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 (13.3.1.2, 13.6). If the overload resolution fails, the program is ill-formed. Otherwise, the conversions thus determined are applied, and the converted operands are used in place of the original operands for the remainder of this section.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload