Стандарт C++ 11 (1119564), страница 42
Текст из файла (страница 42)
— end note ]3Otherwise, if the second and third operand have different types and either has (possibly cv-qualified) classtype, or if both are glvalues of the same value category and the same type except for cv-qualification, anattempt is made to convert each of those operands to the type of the other. The process for determiningwhether an operand expression E1 of type T1 can be converted to match an operand expression E2 of typeT2 is defined as follows:— If E2 is an lvalue: E1 can be converted to match E2 if E1 can be implicitly converted (Clause 4) to thetype “lvalue reference to T2”, subject to the constraint that in the conversion the reference must binddirectly (8.5.3) to an lvalue.— If E2 is an xvalue: E1 can be converted to match E2 if E1 can be implicitly converted to the type“rvalue reference to T2”, subject to the constraint that the reference must bind directly.§ 5.16124© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)— If E2 is an rvalue or if neither of the conversions above can be done and at least one of the operandshas (possibly cv-qualified) class type:— if E1 and E2 have class type, and the underlying class types are the same or one is a base classof the other: E1 can be converted to match E2 if the class of T2 is the same type as, or a baseclass of, the class of T1, and the cv-qualification of T2 is the same cv-qualification as, or a greatercv-qualification than, the cv-qualification of T1.
If the conversion is applied, E1 is changed to aprvalue of type T2 by copy-initializing a temporary of type T2 from E1 and using that temporaryas the converted operand.— Otherwise (i.e., if E1 or E2 has a nonclass type, or if they both have class types but the underlyingclasses are not either the same or one a base class of the other): E1 can be converted to match E2if E1 can be implicitly converted to the type that expression E2 would have if E2 were convertedto a prvalue (or the type it has, if E2 is a prvalue).Using this process, it is determined whether the second operand can be converted to match the thirdoperand, and whether the third operand can be converted to match the second operand.
If both canbe 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 asdescribed below. If exactly one conversion is possible, that conversion is applied to the chosen operandand the converted operand is used in place of the original operand for the remainder of this section.4If the second and third operands are glvalues of the same value category and have the same type, the resultis of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or ifboth are bit-fields.5Otherwise, the result is a prvalue. If the second and third operands do not have the same type, and eitherhas (possibly cv-qualified) class type, overload resolution is used to determine the conversions (if any) to beapplied 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 originaloperands for the remainder of this section.6Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the second and third operands.
After those conversions, one of the following shall hold:— The second and third operands have the same type; the result is of that type. If the operands haveclass type, the result is a prvalue temporary of the result type, which is copy-initialized from eitherthe second operand or the third operand depending on the value of the first operand.— The second and third operands have arithmetic or enumeration type; the usual arithmetic conversionsare performed to bring them to a common type, and the result is of that type.— The second and third operands have pointer type, or one has pointer type and the other is a nullpointer constant, or both are null pointer constants, at least one of which is non-integral; pointerconversions (4.10) and qualification conversions (4.4) are performed to bring them to their compositepointer type (5.9).
The result is of the composite pointer type.— The second and third operands have pointer to member type, or one has pointer to member type and theother is a null pointer constant; pointer to member conversions (4.11) and qualification conversions (4.4)are performed to bring them to a common type, whose cv-qualification shall match the cv-qualificationof either the second or the third operand. The result is of the common type.5.171Assignment and compound assignment operators[expr.ass]The assignment operator (=) and the compound assignment operators all group right-to-left. All require a§ 5.17© ISO/IEC 2011 – All rights reserved125ISO/IEC 14882:2011(E)modifiable lvalue as their left operand and return an lvalue referring to the left operand. The result in allcases is a bit-field if the left operand is a bit-field.
In all cases, the assignment is sequenced after the valuecomputation of the right and left operands, and before the value computation of the assignment expression.With respect to an indeterminately-sequenced function call, the operation of a compound assignment isa single evaluation. [ Note: Therefore, a function call shall not intervene between the lvalue-to-rvalueconversion and the side effect associated with any single compound assignment operator. — end note ]assignment-expression:conditional-expressionlogical-or-expression assignment-operator initializer-clausethrow-expressionassignment-operator: one of= *= /= %= += -= >>= <<= &= ˆ= |=2In simple assignment (=), the value of the expression replaces that of the object referred to by the leftoperand.3If the left operand is not of class type, the expression is implicitly converted (Clause 4) to the cv-unqualifiedtype of the left operand.4If the left operand is of class type, the class shall be complete.
Assignment to objects of a class is definedby the copy/move assignment operator (12.8, 13.5.3).5[ Note: For class objects, assignment is not in general the same as initialization (8.5, 12.1, 12.6, 12.8). — endnote ]6When the left operand of an assignment operator denotes a reference to T, the operation assigns to theobject of type T denoted by the reference.7The behavior of an expression of the form E1 op = E2 is equivalent to E1 = E1 op E2 except that E1 isevaluated only once. In += and -=, E1 shall either have arithmetic type or be a pointer to a possiblycv-qualified completely-defined object type. In all other cases, E1 shall have arithmetic type.8If the value being stored in an object is accessed from another object that overlaps in any way the storage ofthe first object, then the overlap shall be exact and the two objects shall have the same type, otherwise thebehavior is undefined.
[ Note: This restriction applies to the relationship between the left and right sides ofthe assignment operation; it is not a statement about how the target of the assignment may be aliased ingeneral. See 3.10. — end note ]9A braced-init-list may appear on the right-hand side of— an assignment to a scalar, in which case the initializer list shall have at most a single element. Themeaning of x={v}, where T is the scalar type of the expression x, is that of x=T(v) except that nonarrowing conversion (8.5.4) is allowed. The meaning of x={} is x=T().— an assignment defined by a user-defined assignment operator, in which case the initializer list is passedas the argument to the operator function.[ Example:complex<double> z;z = { 1,2 };z += { 1, 2 };int a, b;a = b = { 1 };a = { 1 } = b;// meaning z.operator=({1,2})// meaning z.operator+=({1,2})// meaning a=b=1;// syntax error— end example ]§ 5.17126© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)5.181Comma operator[expr.comma]The comma operator groups left-to-right.expression:assignment-expressionexpression , assignment-expressionA pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discardedvalue expression (Clause 5).83 Every value computation and side effect associated with the left expressionis sequenced before every value computation and side effect associated with the right expression.
The typeand value of the result are the type and value of the right operand; the result is of the same value categoryas its right operand, and is a bit-field if its right operand is a glvalue and a bit-field.2In contexts where comma is given a special meaning, [ Example: in lists of arguments to functions (5.2.2)and lists of initializers (8.5) — end example ] the comma operator as described in Clause 5 can appear onlyin parentheses. [ Example:f(a, (t=3, t+2), c);has three arguments, the second of which has the value 5. — end example ]5.191Constant expressions[expr.const]Certain contexts require expressions that satisfy additional requirements as detailed in this sub-clause; othercontexts have different semantics depending on whether or not an expression satisfies these requirements.Expressions that satisfy these requirements are called constant expressions. [ Note: Constant expressionscan be evaluated during translation.