i saw these 2 macros of offsetof:
#if defined(_msc_ver) && !defined(_crt_use_builtin_offsetof) #ifdef __cplusplus #define offsetof(s,m) ((size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m))) #else #define offsetof(s,m) ((size_t)&(((s*)0)->m)) #endif #else #define offsetof(s,m) __builtin_offsetof(s,m) #endif
what difference between :
((size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
and :
((size_t)&(((s*)0)->m))
?
operator&
can overloaded in c++. if there user defined overload operator&(const m&)
, m
decltype((s*)0)->m)
(i.e. type of member) , if overload not return address of object, macro without reinterpret cast not behave correctly.
char
reference used because char
, unsigned char
have special guarantees being allowed alias type , because operators cannot overloaded primitive types. const volatile
qualifiers there compiler not issue warning casting away qualifiers if member happens have them.
as t.c pointed out in comment, cast there protect against user defined overloads. c not have overloading, protective cast not necessary.
Comments
Post a Comment