There are no fundamental errors in the code.
If A not the usual old, then it is above UB (before C ++ 17) and conditionally supported (after C ++ 17).
You might want to replace char* and int* with auto* , but that's the style.
Note that member pointers do this in exactly the same way in a safe manner. Most compilers implement a member pointer ... as a member offset in a type. However, they work everywhere even on non-substructures.
In addition: I do not see a guarantee that offsetof constexpr in the standard .;)
Replace anyway:
static constexpr auto off_c = offsetof(A, c);
with
static constexpr auto off_c = &A::c;
and
auto* a_storage = static_cast<char *>(a); auto* c = reinterpret_cast<int *>(a_storage + off_c));
with
auto* c = &(a->*off_c);
to do it in a C ++ way.
source share