问题
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?
回答1:
No. References are not objects, and they do not exist or have addresses or offsets. A pointer to a member reference is illegal.
来源:https://stackoverflow.com/questions/13562766/taking-offset-of-a-reference-member-non-pods