Pointers to class member variables can be compared and the results depend on the declaration sequence. See this in the spec For instance this example in compiler explorer is valid and returns true (1):
struct A {
int a0 = 1;
int a1 = 2;
};
consteval int foo()
{
A a;
int* p1 = &a.a0;
int* p2 = &a.a1;
return p2 > p1;
}
int main()
{
return foo();
}
So one would expect that p2-p1
would return the distance, in int objects, between the pointers. And it does at runtime. compiler explorer
struct A {
int a0 = 1;
int a1 = 2;
};
int foo()
{
A a;
int* p1 = &a.a0;
int* p2 = &a.a1;
return p2 - p1;
}
int main()
{
return foo();
}
But not at compile time. GCC, CLANG, and MSVC all behave differently. GCC compiles and returns the expected value, CLANG complains UB, and MSVC compiles but returns 4, not 1, the distance in bytes! Compiler Explorer
CLANG: note: subtracted pointers are not elements of the same array
Which is correct?