I would like to get the offset of a standard layout member variable when provided with a poiner to that variable. I cannot use offsetof
since I have a pointer and n
How about:
template
struct {
ptrdiff_t get_offset( int (T::*mem) )
{
union {
int used;
T unused;
} dummy;
return reinterpret_cast(&(dummy.unused.*mem))
- reinterpret_cast(&dummy.unused);
}
};
The address of a union member doesn't depend on the union member being constructed. Works already in C++03, but then only for PODs.