I am looking for a way to get the orginal size of a struct/class before the compiler aligned it ( more clearly let's say the sum of the member fields of the struct/class ) like this :
struct Foo{
char c;
// the compiler will appen 3 bytes here
int i;
};
The sizeof(Foo) returns 8 but the orginal size is 5 .
suppose we want to write a template function or a macro named orig_sizeof()
template <class t>
size_t orig_sizeof(t)
{
// calculate the sum of the fields of t
}
Of course sizeof
is an internal macro , a keyword macro that has no source code .
A possible way to to it is by changing the structure aligment using __attribute__
or #pragma
than getting the size using sizeof after that resturing the aligment to default ( maybe c++11's decltype can be useful ! ), any ideas about a useful implentation ?
Edit : im trying to write an optimized library that doesn't accept as argument an not optimized struct/class , by comparing sizeof(type) with orig_sizeof(type) , and asking the user to realign feilds for better performance if possible