Here is the code snippet
#include <iostream>
struct Z
{
Z():x(0),y(0),z(x){}
~Z(){}
int x;
int y;
int &z; // Reference member
};
template <typename Type, typename C, typename M>
size_t Offsetof (M C::* ptr_to_member)
{
Type type;
return reinterpret_cast<char*> (&(type.*ptr_to_member)) - reinterpret_cast<char*> (&type);
}
int main()
{
std::cout << Offsetof<Z>(&Z::x); // works
std::cout << Offsetof<Z>(&Z::y); // works
std::cout << Offsetof<Z>(&Z::z); // doesn't work
}
We cannot create pointer to reference so the function Offsetof
won't work for z
.
Is there any way to take offset of a reference data member for non PODs?