#pragma pack,模板类型定义和结构对齐 [英] #pragma pack, template typedefs, and struct alignment

查看:138
本文介绍了#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'd模板类,编译包是在typedef的地方还是在变量定义的地方生效?如果是后者,有什么方法可以获得跨文件一致对齐的类型?

解决方案

在您的代码中, #pragma pack 将不起作用。只有当它围绕结构或类的定义生效时,它才会做任何事情,而不是围绕typedef或任何东西。它也不会影响变量定义。



你可以在这里看到这个用法: http://msdn.microsoft.com/en-us/library/2e70t5y1(v = VS.100).aspx


pack在第一个struct,union,或在看到编译指示后的类声明。包对定义没有影响。



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?

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.

这篇关于#pragma pack,模板类型定义和结构对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆