Search code examples
c++pointerslanguage-lawyerundefined-behaviorcomparison-operators

"is not required" == undefined behavior?


My question is mainly about terminology and how to interpret the standard.

[expr.rel]#4:

The result of comparing unequal pointers to objects is defined in terms of a partial order consistent with the following rules:

(4.1) If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript is required to compare greater.

(4.2) 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 is required to compare greater provided the two members have the same access control ([class.access]), neither member is a subobject of zero size, and their class is not a union.

(4.3) Otherwise, neither pointer is required to compare greater than the other.

I am little confused as how to interpret (4.3). Does that mean that this

#include <iostream>
int main() {
    int x;
    int y;
    std::cout << (&x < &y);
    std::cout << (&x < &y);
}

is...

  • valid C++ code and the output is either 11 or 00.
  • invalid code, because it has undefined behaivour

?

In other words, I know that (4.3) does apply here, but I am not sure about the implications. When the standard says "it can be either A or B" is this the same as saying "it is undefined" ?


Solution

  • The wording has changed in various editions of the C++ standard, and in the recent draft cited in the question. (See my comments on the question for the gory details.)

    C++11 says:

    Other pointer comparisons are unspecified.

    C++17 says:

    Otherwise, neither pointer compares greater than the other.

    The latest draft, cited in the question, says:

    Otherwise, neither pointer is required to compare greater than the other.

    That change was made in response to an issue saying ""compares greater" term is needlessly confusing".

    If you look at the surrounding context in the draft standard, it's clear that in the remaining cases the result is unspecified. Quoting from [expr.rel] (text in italics is my summary):

    The result of comparing unequal pointers to objects is defined in terms of a partial order consistent with the following rules:

    • [pointers to elements of the same array]

    • [pointers to members of the same object]

    • [remaining cases] Otherwise, neither pointer is required to compare greater than the other.

    If two operands p and q compare equal, 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.

    So the result of the < operator in such cases is unspecified, but it does not have undefined behavior. It can be either true or false, but I don't believe it's required to be consistent. The program's output could be any of 00, 01, 10, or 11.