I have a struct Foo which contains a nested struct Bar. These structs only contain POD types. My problem is that the size of Foo is larger than it would have been if I copied all the members of Bar:
#include <iostream>
#include <string>
struct Bar {
double a;
long long b;
int c;
};
struct Foo {
Bar b;
int d;
};
struct Foo2 {
double a;
long long b;
int c;
int d;
};
int main()
{
std::cout << sizeof(Foo) << std::endl;
std::cout << sizeof(Bar) << std::endl;
std::cout << sizeof(Foo2) << std::endl;
}
32 24 24
I understand that this is happening because Bar gets padded to 24 bytes so Foo adds a single int which then gets padded to 32 bytes. I don't want to mark Bar or Foo as packed. I was wondering if there is any way to tell the compiler to not store Bar as a struct but to just include its members? Something like:
struct Foo {
inline Bar b; // Keyword inline doesn't work here.
int d;
};