Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.rel] and unrelated pointers #435

Closed
zygoloid opened this issue Jan 26, 2015 · 1 comment
Closed

[expr.rel] and unrelated pointers #435

zygoloid opened this issue Jan 26, 2015 · 1 comment
Assignees

Comments

@zygoloid
Copy link
Member

From c++std-core-26811:

I believe p3 and p4 fail to adequately describe that relationally comparing two unrelated
pointers is unspecified behavior. For reference:

3 Comparing pointers to objects is defined as follows:
— If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript compares greater.
— If one pointer points to an element of an array, or to a subobject thereof, and another pointer points one past the last element of the array, the latter pointer compares greater.
— If two pointers point to different non-static data members of the same object, or to subobjects of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control (Clause 11) and provided their class is not a union.

4 If two operands p and q compare equal (5.10), p<=q and p>=q both yield true and pq 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.

p3 makes no mention of what the relationship would mean, and p4 makes the assumption the relationship is defined when it isn't.

I think the correct solution is to add a bullet to p3 that says "Otherwise, the result of the comparison is unspecified." and remove that statement from p4.

The suggestion is not an editorial change; given:

int x;
int y;

... if we have &x == &y, the current wording gives (&x <= &y) == true, and the proposal would make it unspecified.

For an editorial fix, adding a bullet to p3 saying "Otherwise, neither pointer compares greater than the other." seems appropriate, and we should keep the statement in p4. It would also help if the introductory text to p3 said more clearly that we're defining the term "compares greater", not defining how to compare pointers to objects.

@jensmaurer
Copy link
Member

I'm adding the "Otherwise" for p3, but I'm leaving the intro text in p3 alone. We have a similar structure in 5.10 for "compare equal". Further, as long as the definition of a relationship is proper, we don't really have to say what it means if we just use it to define the result of an operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants