SunStudio C++ 中的对齐编译器
我需要为按 4 字节对齐的 2 字节变量声明类型别名。
在 GCC、XL C/C++ (AIX)、aCC (HP-UX) 中,我可以使用以下代码:
typedef uint16_t AlignedType __attribute__ ((aligned (4)));
在 Windows 中,我可以使用:
typedef __declspec(align(4)) unsigned __int16 AlignedType;
How can I statements in SunStudio C++ 11?
“pragmaalign”不适合,因为它仅适用于全局或静态变量,并且需要变量名称。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 Sun C 5.9 (Sun ONE Studio 12) 开始,支持对齐属性:
不幸的是,C++ 不支持此属性(至少在 Sun C++ 5.10 中)。
As of Sun C 5.9 (Sun ONE Studio 12), the aligned attribute is supported:
Unfortunately this attribute is not supported in C++ (at least through Sun C++ 5.10).
至少值得尝试:
这当然会让访问变得更加痛苦,并且会终止直接赋值,因此可能会破坏整个代码库。此外,它纯粹基于这样的假设:包含更大的类型(由于其大小而被假定具有 32 位的“本机对齐”)使得
union
作为一个整体在 32 位上对齐。It might be worth at least trying:
This of course makes accessing a bit more painful, and kills direct assignment so it might break your entire code base. Also, it's purely based on the assumption that including a larger type, which is assumed to have "native alignment" of 32 bits due to being of that size, makes the
union
as a whole align on 32 bits.为了便于将来参考,当编译器赶上时,C++11 具有标准对齐属性,请参阅
alignas
(N3242)。For future references, when the compilers catch up, C++11 has standard alignment attributes, see
alignas
([dcl.align] in N3242).从 Sun C++ 5.12 SunOS_sparc 2011/11/16 开始,根据 DRH 的响应,C++ 似乎支持 gcc 语法:
输出为:
8 4 2
As of Sun C++ 5.12 SunOS_sparc 2011/11/16, the gcc syntax appears to be supported for C++ as per DRH's response:
The output is:
8 4 2