Стандарт C++ 11 (1119564), страница 41
Текст из файла (страница 41)
Pointerconversions (4.10) and qualification conversions (4.4) are performed on pointer operands (or on a pointeroperand and a null pointer constant, or on two null pointer constants, at least one of which is non-integral)to bring them to their composite pointer type. If one operand is a null pointer constant, the compositepointer type is std::nullptr_t if the other operand is also a null pointer constant or, if the other operandis a pointer, the type of the other operand.
Otherwise, if one of the operands has type “pointer to cv1 void,”then the other has type “pointer to cv2 T ” and the composite pointer type is “pointer to cv12 void,” wherecv12 is the union of cv1 and cv2. Otherwise, the composite pointer type is a pointer type similar (4.4) to thetype of one of the operands, with a cv-qualification signature (4.4) that is the union of the cv-qualificationsignatures of the operand types. [ Note: this implies that any pointer can be compared to a null pointerconstant and that any object pointer can be compared to a pointer to (possibly cv-qualified) void. — endnote ] [ Example:void *p;const int *q;int **pi;const int *const *pci;void ct() {p <= q;// Both converted to const void* before comparison§ 5.9© ISO/IEC 2011 – All rights reserved121ISO/IEC 14882:2011(E)pi <= pci;// Both converted to const int *const * before comparison}— end example ] Pointers to objects or functions of the same type (after pointer conversions) can be compared, with a result defined as follows:— If two pointers p and q of the same type point to the same object or function, or both point one pastthe end of the same array, or are both null, then p<=q and p>=q both yield true and p<q and p>qboth yield false.— If two pointers p and q of the same type point to different objects that are not members of the sameobject or elements of the same array or to different functions, or if only one of them is null, the resultsof p<q, p>q, p<=q, and p>=q are unspecified.— If two pointers point to non-static data members of the same object, or to subobjects or array elementsof such members, recursively, the pointer to the later declared member compares greater provided thetwo members have the same access control (Clause 11) and provided their class is not a union.— If two pointers point to non-static data members of the same object with different access control(Clause 11) the result is unspecified.— If two pointers point to non-static data members of the same union object, they compare equal (afterconversion to void*, if necessary).
If two pointers point to elements of the same array or one beyondthe end of the array, the pointer to the object with the higher subscript compares higher.— Other pointer comparisons are unspecified.3Pointers to void (after pointer conversions) can be compared, with a result defined as follows: If bothpointers represent the same address or are both the null pointer value, the result is true if the operator is<= or >= and false otherwise; otherwise the result is unspecified.4If two operands of type std::nullptr_t are compared, the result is true if the operator is <= or >=, andfalse otherwise.5If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yieldtrue if the specified relationship is true and false if it is false.5.10Equality operators[expr.eq]equality-expression:relational-expressionequality-expression == relational-expressionequality-expression != relational-expression1The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, andresult type as the relational operators except for their lower precedence and truth-value result.
[ Note: a<b== c<d is true whenever a<b and c<d have the same truth-value. — end note ] Pointers of the same type(after pointer conversions) can be compared for equality. Two pointers of the same type compare equal ifand only if they are both null, both point to the same function, or both represent the same address (3.9.2).2In addition, pointers to members can be compared, or a pointer to member and a null pointer constant.Pointer to member conversions (4.11) and qualification conversions (4.4) are performed to bring them to acommon type.
If one operand is a null pointer constant, the common type is the type of the other operand.Otherwise, the common type is a pointer to member type similar (4.4) to the type of one of the operands,with a cv-qualification signature (4.4) that is the union of the cv-qualification signatures of the operandtypes.
[ Note: this implies that any pointer to member can be compared to a null pointer constant. — endnote ] If both operands are null, they compare equal. Otherwise if only one is null, they compare unequal.§ 5.10122© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)Otherwise if either is a pointer to a virtual member function, the result is unspecified. Otherwise theycompare equal if and only if they would refer to the same member of the same most derived object (1.8)or the same subobject if they were dereferenced with a hypothetical object of the associated class type.[ Example:struct B {int f();};struct L : B { };struct R : B { };struct D : L, R { };int (B::*pb)() = &B::f;int (L::*pl)() = pb;int (R::*pr)() = pb;int (D::*pdl)() = pl;int (D::*pdr)() = pr;bool x = (pdl == pdr);// false— end example ]3If two operands of type std::nullptr_t are compared, the result is true if the operator is ==, and falseotherwise.4Each of the operators shall yield true if the specified relationship is true and false if it is false.5.11Bitwise AND operator[expr.bit.and]and-expression:equality-expressionand-expression & equality-expression1The usual arithmetic conversions are performed; the result is the bitwise AND function of the operands.
Theoperator applies only to integral or unscoped enumeration operands.5.12Bitwise exclusive OR operator[expr.xor]exclusive-or-expression:and-expressionexclusive-or-expression ˆ and-expression1The usual arithmetic conversions are performed; the result is the bitwise exclusiveoperands. The operator applies only to integral or unscoped enumeration operands.5.13Bitwise inclusive OR operatorORfunction of the[expr.or]inclusive-or-expression:exclusive-or-expressioninclusive-or-expression | exclusive-or-expression1The usual arithmetic conversions are performed; the result is the bitwise inclusive OR function of its operands.The operator applies only to integral or unscoped enumeration operands.5.14Logical AND operator[expr.log.and]logical-and-expression:inclusive-or-expressionlogical-and-expression && inclusive-or-expression§ 5.14© ISO/IEC 2011 – All rights reserved123ISO/IEC 14882:2011(E)1The && operator groups left-to-right.
The operands are both contextually converted to type bool (Clause 4).The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-rightevaluation: the second operand is not evaluated if the first operand is false.2The result is a bool. If the second expression is evaluated, every value computation and side effect associatedwith the first expression is sequenced before every value computation and side effect associated with thesecond expression.5.15Logical OR operator[expr.log.or]logical-or-expression:logical-and-expressionlogical-or-expression || logical-and-expression1The || operator groups left-to-right.
The operands are both contextually converted to bool (Clause 4). Itreturns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-rightevaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.2The result is a bool. If the second expression is evaluated, every value computation and side effect associatedwith the first expression is sequenced before every value computation and side effect associated with thesecond expression.5.16Conditional operator[expr.cond]conditional-expression:logical-or-expressionlogical-or-expression ? expression : assignment-expression1Conditional expressions group right-to-left.
The first expression is contextually converted to bool (Clause 4).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. Only one of the second and third expressions is evaluated. Every valuecomputation and side effect associated with the first expression is sequenced before every value computationand side effect associated with the second or third expression.2If either the second or the third operand has type void, then the lvalue-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, and oneof the following shall hold:— The second or the third operand (but not both) is a throw-expression (15.1); the result is of the typeof the other and is a prvalue.— Both the second and the third operands have type void; the result is of type void and is a prvalue.[ Note: This includes the case where both operands are throw-expressions.