Short version: How to find out the size (in bits) of an individual field in a C ++ field?
To clarify, an example of the field I'm talking about:
struct Test {
unsigned field1 : 4;
unsigned field2 : 8;
unsigned field3 : 1;
unsigned field4 : 3;
unsigned field5 : 16;
int normal_member;
};
Test t;
t.field1 = 1;
t.field2 = 5;
To get the size of the entire test object easily, just say
sizeof(Test);
We can get the normal member of the structure through
sizeof(((Test*)0)->normal_member); // returns 4 (on my system)
I would like to know how to get the size of an individual field, for example Test :: field4. The above example for a regular structure element does not work. Any ideas? Or does someone know the reason why he cannot work? I'm pretty sure sizeof won't help, since it only returns size in bytes, but if someone knows otherwise, I'm all ears.
Thank!
source
share