Compiling this header:
// myheader.h class MyClass { MyClass(); ~MyClass(); unsigned int mMyUint; bool bMyBool; std::string sMyString; }; with Visual Studio 2010 /W4 (I was told to do so), target x64, gives me the following warning C4121:
'MyClass' : alignment of a member was sensitive to packing, referring to line where the std::string sMyString is. The official help of Microsoft proposes to use a #pragma pack(x), where x be 1, 2, 4 dependent on the members one uses inside its struct / class.
No I wonder which alignment to use for std::string? My idea was that std::string is nor compiler neither platform independent. Is there any way to resolve that issue especially considering the other members which may have any other aligment?
Best would be a compiler-independent (and even portable) solution.
Update: Concerning the very useful answer provided by jalf: No, in this file I am not using a #pragma pack(x). I used it in another struct where I use Bit-fields which I have seen are padded when not using the #pragma pack(x). Now I was just alarmed to run into the same trap because I did not know how the Microsoft compiler resolves std::string.
#pragma pack(push), with no correspondingpop.