Minimal code reproducible code:
struct Struct
{
int value;
int* a;
};
template <typename T>
void Print(T value)
{
int size = sizeof(value);
printf("%d %d\n", value, size);
}
int main()
{
auto aAddress = &Struct::a;
Print(aAddress);
return 0;
}
In function main()
, I take the address of a non-static field a
through Struct
type name without instantiating it. A few questions:
- What is the resulting type of
aAddress
? Clang and GCC both reports its size to besizeof(void*)
(4 bytes when compiled to 32-bit code, and 8 bytes when compiled to 64-bit code), whereas MSVC reports its size to be 4 bytes no matter whether you compile code as 32-bit or 64-bit. - What is the value of
aAddress
supposed to be? On all three compilers (Clang, GCC, MSVC) it seems to be equal tooffsetof(Struct, a)
. Is this a mere coincidence (and the actual result is undefined) or is it indeed equal to offset of that field from the beginning of the struct?