offsetof at compile time

The offsetof() macro is a compile-time construct. There is no standard-compliant way to define it, but every compiler must have some way of doing it. One example would be: #define offsetof( type, member ) ( (size_t) &( ( (type *) 0 )->member ) ) While not being a compile-time construct technically (see comments by user … Read more

Does the ‘offsetof’ macro from invoke undefined behaviour?

Where the language standard says “undefined behavior”, any given compiler can define the behavior. Implementation code in the standard library typically relies on that. So there are two questions: (1) Is the code UB with respect to the C++ standard? That’s a really hard question, because it’s a well known almost-defect that the C++98/03 standard … Read more