#pragma pack、模板类型定义和结构对齐
使用 Visual Studio 或 gcc,如果我有的
#pragma pack(push, 16)
typedef std::map<uint32_t, uint32_t> MyIntMap;
#pragma pack(pop)
话:
#pragma pack(push, 8)
MyIntMap thisInstance;
#pragma pack(pop)
thisInstance 的结构对齐是什么?也就是说,对于 typedef 的模板类,pragma pack 是在 typedef 处生效还是在变量定义处生效?如果是后者,什么是获得跨文件一致对齐的类型的好解决方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的代码中,
#pragma pack
将不起作用。它仅在围绕结构或类的定义有效时才执行任何操作,而不是围绕 typedef 或任何其他内容。它对变量定义也没有任何影响。您可以在此处查看用法: http://msdn .microsoft.com/en-us/library/2e70t5y1(v=VS.100).aspx
具体来说:
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: