I understand the reasoning behind expr.add#4.2, restricting +
and -
to pointers targeting elements of the same array, which was explained in several comments to my question regarding an offset pointer class. However, the very same could be accomplished by both pointer and pointer plus offset targets being within the same (nested) composition, not necessarily an array. If these differences could be anything the offsetof macro would be pointless.
Example:
#include <iostream>
struct Inner
{
int i1;
int i2;
};
struct Outer
{
int i1;
int i2;
Inner inner;
};
int main(int argc, char* argv[])
{
Outer outer;
std::cout << "&outer.i2 - &outer.i1: " << &outer.i2 - &outer.i1 << std::endl;
std::cout << "&outer.inner.i2 - &outer.i1: " << &outer.inner.i2 - &outer.i1 << std::endl;
return 0;
}
returns
&outer.i2 - &outer.i1: 1
&outer.inner.i2 - &outer.i1: 3
All involved pointer targets are within the object outer
, but are not the same object, and no array is involved. Undefined behaviour?
expr.add#4.2 talks about “(possibly-hypothetical) array element“. Would that be met within the mentioned composition, or what else does “possibly-hypothetical” mean?