#pragma pack, template typedefs, and struct alignment

天涯浪子 提交于 2020-01-04 04:26:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!