The C ++ standard ensures that arrays are stored contiguously, without intermediate filling. If you want to make sure the floats are contiguous, and you don't want to rely on compiler-specific add-on directives, you can simply store your floats in an array:
private: float array_[4];
However, this does not necessarily guarantee that the class will be the size of four floats. But you can make sure that any operation that is specifically addressed to the internal array of floats will work in a continuous array.
But in general, maybe you should ask a question why you need this guarantee. Typically, the only reason you need a class memory layout is to treat it as a serialized byte array. In this case, it is usually better to create a special serialization / deserialization function, rather than relying on any specific memory layout.
source share