问题
Using Visual Studio or gcc, if I've got
#pragma pack(push, 16)
typedef std::map<uint32_t, uint32_t> MyIntMap;
#pragma pack(pop)
then later:
#pragma pack(push, 8)
MyIntMap thisInstance;
#pragma pack(pop)
What is the structure alignment of thisInstance? That is, for a typedef'd template class, does pragma pack take effect at the place of the typedef or at the place of a variable definition? If it's the latter, what's a good workaround to get a type with consistent alignment across files?
回答1:
In your code, the #pragma pack
will have no effect. It only does anything when it's in effect around the definition of a struct or class, not around a typedef or anything. Neither does it have any effect around that variable definition.
You can see the usage here: http://msdn.microsoft.com/en-us/library/2e70t5y1(v=VS.100).aspx
Specifically:
pack takes effect at the first struct, union, or class declaration after the pragma is seen. pack has no effect on definitions.
来源:https://stackoverflow.com/questions/8075437/pragma-pack-template-typedefs-and-struct-alignment