N3624
revision of N3498
Jens Maurer
2013-04-15

Core Issue 1512: Pointer comparison vs qualification conversions (revision 3)

Changes compared to N3478

Changes compared to N3498

Introduction

This paper presents the modifications to the Working Draft necessary to resolve core issues 583 and 1512. In particular, it makes
void f(char * p)
{
  if (p > 0) { ... }
  if (p > nullptr) { ... }
}
ill-formed (both cases) and
void g(int **p1, const int**p2)
{
   if (p1 == p2) { ... }
}
well-formed.

Explanation of the changes

The changes below essentially replace all of 5.9 expr.rel and 5.10 expr.eq. The current description in the Working Paper mixes semantic constraints and results of relational operators with those of equality operators. Furthermore, the attempt at type unification for similar types fails spectacularly.

The changes below define a generic composite pointer type that is applicable to relational, equality, and conditional operators. The corresponding description is moved from section 5.9 expr.rel to clause 5 expr.

Furthermore, the semantic specifications of the relational and equality operators are now phrased in terms of "compares greater" and "compares equal"; the result of the operators is derived from these specifications. This avoids repetition and makes it possible to refer to 5.10 expr.eq in order to determine the results of the <= and >= operators for some cases.

For the equality operators, 5.10 expr.eq no longer refers to 5.9 expr.rel. Instead, the relevant semantic constraints are specified again (grouping, permissible types, result type). Each paragraph then enumerates one of the cases of types of operands (pointer, pointer to member, std::nullptr_t, arithmetic or enumeration type).

The special wording about union members was phrased in terms of "same address" and moved to 9.5 class.union, where layout of union members is discussed.

Finally, the overload descriptions for built-in operators were adjusted, because objects of type std::nullptr_t cannot be used with relational operators any more.

Changes to the Working Paper

Add a new paragraph at the end of 5 expr:
The cv-combined type of two types T1 and T2 is a type T3 similar to T1 whose cv-qualification signature (4.4 conv.qual) is: [ Note: Given similar types T1 and T2, this construction ensures that both can be converted to T3. ] The composite pointer type of two operands p1 and p2 having types T1 and T2, respectively, where at least one is a pointer or pointer to member type or std::nullptr_t, is: [ Example:
typedef void *p;
typedef const int *q;
typedef int **pi;
typedef const int **pci;
The composite pointer type of p and q is "pointer to const void"; the composite pointer type of pi and pci is "pointer to const pointer to const int". ]
The following also resolves core issue 583.

Change in 5.9 expr.rel paragraphs 1 to 5:

... The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t.

The usual arithmetic conversions are performed on operands of arithmetic or enumeration type. If both operands are pointers, pointer Pointer conversions (4.10 conv.ptr) and qualification conversions (4.4 conv.qual) are performed on pointer operands (or on a pointer operand 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 composite pointer type (clause 5 expr). If one operand is a null pointer constant, the composite pointer type is std::nullptr_t if the other operand is also a null pointer constant or, if the other operand is 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," where cv12 is the union of cv1 and cv2. Otherwise, the composite pointer type is a pointer type similar (4.4 conv.qual) to the type of one of the operands, with a cv-qualification signature (4.4 conv.qual) that is the union of the cv-qualification signatures of the operand types. [ Note: this implies that any pointer can be compared to a null pointer constant and that any object pointer can be compared to a pointer to (possibly cv-qualified) void. -- end note ] [ Example:

void *p;
const int *q;
int **pi;
const int *const *pci;
void ct() {
  p <= q;           // Both converted to const void* before comparison
  pi <= pci;        // Both converted to const int *const * before comparison
}
-- end example ]

After conversions, the operands shall have the same type.

Pointers to objects or functions of the same type (after pointer conversions) can be compared, with a result Comparing pointers to objects is defined as follows:

If two operands p and q compare equal (5.10 expr.eq), p<=q and p>=q both yield true and p<q and p>q both yield false. Otherwise, if a pointer p compares greater than a pointer q, p>=q, p>q, q<=p, and q<p all yield true and p<=q, p<q, q>=p, and q>p all yield false. Otherwise, the result of each of the operators is unspecified.

Pointers to void (after pointer conversions) can be compared, with a result defined as follows: If both pointers 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.

If two operands of type std::nullptr_t are compared, the result is true if the operator is <= or >=, and false otherwise.

If both operands (after conversions) are of arithmetic or enumeration type, each of the operators shall yield true if the specified relationship is true and false if it is false.

Change in 5.10 expr.eq paragraphs 1 to 4:

The == (equal to) and the != (not equal to) operators group left-to-right. The operands shall have arithmetic, enumeration, pointer, or pointer to member type, or type std::nullptr_t. The operators == and != both yield true or false, i.e. a result of type bool. have the same semantic restrictions, conversions, and result 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 ] In each case below, the operands shall have the same type after the specified conversions have been applied.

If at least one of the operands is a pointer, pointer conversions (4.10 conv.ptr) and qualification conversions (4.4 conv.qual) are performed on both operands to bring them to their composite pointer type (clause 5 expr). Comparing pointers is defined as follows: Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9.2 basic.compound), otherwise they compare unequal.

If at least one of the operands is a pointer to member, pointer In addition, pointers to members can be compared, or a pointer to member and a null pointer constant. Pointer to member conversions (4.11 conv.mem) and qualification conversions (4.4 conv.qual) are performed on both operands to bring them to a common type their composite pointer type (clause 5 expr). 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 conv.qual) to the type of one of the operands, with a cv-qualification signature (4.4 conv.qual) that is the union of the cv-qualification signatures of the operand types. [ Note: this implies that any pointer to member can be compared to a null pointer constant. -- end note ] Comparing pointers to member is defined as follows:

If two Two operands of type std::nullptr_t or one operand of type std::nullptr_t and the other a null pointer constant compare equal are compared, the result is true if the operator is ==, and false otherwise.

If two operands compare equal, the result is true for operator== and false for operator!=. If two operands compare unequal, the result is false for operator== and true for operator!=. Otherwise, the result of each of the operators is unspecified.

If both operands are of arithmetic or enumeration type, the usual arithmetic conversions are performed on both operands; each Each of the operators shall yield true if the specified relationship is true and false if it is false.

Change in 5.16 expr.cond paragraph 6:
Change in 9.5 class.union paragraph 1:
... Each non-static data member is allocated as if it were the sole member of a struct. All non-static data members of a union object have the same address. ...
Change in 13.6 over.built paragraphs 15 and 16:
For every T , where T is an enumeration type, or a pointer type, or std::nullptr_t, there exist candidate operator functions of the form
  bool     operator<(T , T );
  bool     operator>(T , T );
  bool     operator<=(T , T );
  bool     operator>=(T , T );
  bool     operator==(T , T );
  bool     operator!=(T , T );
For every pointer to member type T or type std::nullptr_t there exist candidate operator functions of the form
  bool     operator==(T , T );
  bool     operator!=(T , T );
Change in 20.7.2.2.7 util.smartptr.shared.cmp paragraph 2:
Returns: less<V>()(a.get(), b.get()), where V is the composite pointer type (5.9 expr.rel) (5 expr) of T* and U*.