Offsetof
You can use Offsetof marco contributed by the cstddef library. Here I get the offset first, then I use the operator of the simulation module to check if it is a multiple of 8. Then, if the remainder is 0, the offset is really a multiple of 8 bytes.
// Offset.cpp
Demo here
Offsetof with static_assert
Or in the context of this question, the goal is to have static_assert . Well, it's almost the same:
// OffsetAssert.cpp
Demo here
Type of use
I use the type std::size_t , because the type that you usually use to store the sizes of variables, objects, etc. And also because it expands to the expression std::size_t according to cppreference.com:
The Offsetof macro expands to an integer constant expression of the type std::size_t , whose value is the offset in bytes from the beginning of the object of the specified type to its specified member, including the addition, if any.
References
cpprefrence
cplusplus
source share