A few simple questions.
const int gFirst;
const int gSecond;
struct Data
{
static int First;
static int Second;
int first;
int second;
};
Data data;
Is it guaranteed that the following statements are true?
&gFirst < &gSecond&Data::First < &Data::Second&data.first < &data.second
1) This result is unspecified.
2) This result is unspecified.*
3) Yes.
The relevant section in the standard is §5.9/2. Relational comparisons between the pointers
pandqare only specified when:pandqpoint to the same object or function, point to one past the end of the same array, or both are null. In this case,p <= qandp >= qare true, andp < qandp > qare false.pandqpoint to nonstatic data members of the same object, the pointer to the later declared member compares greater. (Note, this comparison cannot be between access specifiers.)pandqpoint to elements within the same array or one past the end of the array, the pointer to the element with the higher subscript or to one past the end of the array compares greater.pandqpoint to data members of the same union object, in which case they compare equal.In all other cases, the result is not specified.
*Because they are static, they (obviously) do not get the “nonstatic member” rules. They will be defined in some translation unit, and therefore are just like any other pointer. (Unspecified.)
Note! There is a way to get total ordering, and that is via
std::less<void*>(and all the other comparative function objects.)This is in §20.3.3/8:
So while you don’t know if
std::less<void*>(&gFirst, &gSecond)istrueorfalse, you are guaranteed:Which can prove useful.