Lets assume I have a structure:
struct A {
uint16_t a;
uint64_t b;
};
is there a way to get the size of A w/o padding ? i.e.: The sum of sizeof of all the members (even if it is not recursive).
Normally sizeof(A) == 16.
I would like __GCC_sizeof__(A) == 10.
I want it in a test code w/o affecting the actual code, which means no "#pragma"s and no "__attribute__" in the structure definition.
(Though it can be done with #ifdef TEST, but it is very ugly).
It doesn’t have to be portable, GCC is enough.
Thanks!
It would have been better had you ask this first…
And the answer is yes, there are ways, but not by
#includethe file; you should use something that is able to get the AST/ABT structure and lists the fields then compare it against a pre-registered list. This is something possible with Clang, for example.But now, let’s go one step further. Why would you want to test that ? It’s brittle!
It would be better to test functionality, what’s hidden is hidden for a reason.
If each functionality is tested correctly, then it matters not whether you know the list of fields as long as the tests pass.