template<class T, typename U> ptrdiff_t foo(T U::* m)
{
// return offset
}
How I can get the offset of the field 'm' in this context? I would prefer to use am compile-time expression.
Thanks in advance for any help. Best regards
@Michael J
Thanks for your answer. This wasn't exactly what I was looking for, but it gives me the inspiration to doing that:
template<class T, typename U>
std::ptrdiff_t member_offset(U T::* member)
{
return reinterpret_cast<std::ptrdiff_t>(
&(reinterpret_cast<T const volatile*>(NULL)->*member)
);
}
Sounds like you're looking for the offsetof() macro.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With