Is subtraction of two nullptr values ​​guaranteed to be zero?

Is this guaranteed by the C ++ standard that if I have two pointers of the same type whose value is nullptr, then the difference between these pointers is 0?

Is the predicate true in pseudo-mathematical notation?

ForAll x ForAll y (x == nullptr) ^ (y == nullptr) → (x - y == 0)

The simplest code example I can imagine:

int* x = nullptr;
int* y = nullptr;
assert(x - y == 0);

I suppose this boils down to: is it possible to have a proper implementation of the C ++ standard for which there are several representations of the nullptr bits that are compared only as equal because the equality operator does some magic?

+4
source share
1 answer

, . undefined C, ++ - .

5.7 [expr.add]

7 0 , . , , , 0, std::ptrdiff_t.

+11

Source: https://habr.com/ru/post/1569242/


All Articles