如何使用alignas替代实用套件? [英] How to use alignas to replace pragma pack?

查看:44
本文介绍了如何使用alignas替代实用套件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解应该使用alignas的方法,我想知道它是否可以替代pragma pack,但我一直在努力验证它,但是没有运气.使用gcc 4.8.1( http://ideone.com/04mxpI ),对于以下STestAlignas,我总是会得到8个字节,而使用pragma pack则为5个字节.我想要的是使sizeof(STestAlignas)返回5.我尝试在clang 3.3( http://gcc.godbolt.org/),但出现错误:

I am trying to understand how alignas should be used, I wonder if it can be a replacement for pragma pack, I have tried hard to verify it but with no luck. Using gcc 4.8.1 (http://ideone.com/04mxpI) I always get 8 bytes for below STestAlignas, while with pragma pack it is 5 bytes. What I would like ot achive is to make sizeof(STestAlignas) return 5. I tried running this code on clang 3.3 (http://gcc.godbolt.org/) but I got error:

!!错误:请求的对齐方式小于"long"类型的最小对齐方式8-仅低于alignas用法.

!!error: requested alignment is less than minimum alignment of 8 for type 'long' - just below alignas usage.

那么alignas可能有一个最小的对齐值吗?

So maybe there is a minimum alignment value for alignas?

下面是我的测试代码:

#include <iostream>
#include <cstddef>
using namespace std;

#pragma pack(1)
struct STestPragmaPack {
  char c;
  long d;
} datasPP;
#pragma pack()

struct STestAttributPacked {
  char c;
  long d;
} __attribute__((packed)) datasAP;

struct STestAlignas {
  char c;
  alignas(char) long d;
} datasA;

int main() {
    cout << "pragma pack = " << sizeof(datasPP) << endl;
    cout << "attribute packed = " << sizeof(datasAP) << endl;
    cout << "alignas = " << sizeof(datasA) << endl;
}

gcc 4.8.1的结果:

results for gcc 4.8.1:

pragma pack = 5
attribute packed = 5
alignas = 8

[26.08.2019]

[26.08.2019]

此主题似乎有一些标准化运动.p1112提案-对类布局控件的语言支持-建议添加(以及其他) [[layout(smallest)]] 属性,该属性将对类成员进行重新排序,以使对齐成本尽可能小(这是程序员之间的常见技术-但它通常会破坏类定义的可读性).但这 等于编译指示(打包)的作用!

It appears there is some standardisation movement in this topic. p1112 proposal - Language support for class layout control - suggest adding (among others) [[layout(smallest)]] attribute which shall reorder class members so as to make the alignment cost as small as possible (which is a common technique among programmers - but it often kills class definition readability). But this is not equal to what pragma(pack) does!

推荐答案

alignas 无法替换 #pragma pack .

GCC接受 alignas 声明,但仍保持成员正确对齐:满足最严格的对齐要求(在这种情况下, long 的对齐)也满足要求您指定的.

GCC accepts the alignas declaration, but still keeps the member properly aligned: satisfying the strictest alignment requirement (in this case, the alignment of long) also satisfies the requirement you specified.

但是,GCC太宽容,因为该标准实际上在第7.6.2节第5段中明确禁止这样做:

However, GCC is too lenient as the standard actually explicitly forbids this in §7.6.2, paragraph 5:

声明中所有 alignment-specifiers 的组合效果所指定的对齐方式,其严格性不如所有 alignment-specifiers所声明的实体所要求的对齐方式严格被省略(包括其他声明中的内容).

The combined effect of all alignment-specifiers in a declaration shall not specify an alignment that is less strict than the alignment that would be required for the entity being declared if all alignment-specifiers were omitted (including those in other declarations).

这篇关于如何使用alignas替代实用套件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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