0xb*_*00d 5 c++ templates member-pointers
template<class T, typename U> ptrdiff_t foo(T U::* m)
{
// return offset
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何获得字段'm'的偏移量?我更喜欢使用am编译时表达式.
在此先感谢您的帮助.最好的祝福
@Michael J.
感谢您的回答.这不是我想要的,但它给了我这样做的灵感:
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)
);
}
Run Code Online (Sandbox Code Playgroud)
听起来您正在寻找offsetof()宏。