我在哪里可以在 C++11 中使用 alignas()? [英] Where can I use alignas() in C++11?

查看:27
本文介绍了我在哪里可以在 C++11 中使用 alignas()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了标准化我的代码并使其更具可移植性,我替换了

In an effort to standardize my code and make it more portable, I replaced

#ifdef __GNUC__
typedef __attribute__((aligned(16))) float aligned_block[4];
#else
typedef __declspec(align(16)) float aligned_block[4];
#endif

typedef float alignas(16) aligned_block[4];

在 C++11 中.然而,gnu (4.8) 不喜欢那样,而是抱怨

in C++11. However, gnu (4.8) doesn't like that but complains

test.cc:3:9: warning: attribute ignored [-Wattributes]
  typedef float alignas(16) aligned_block[4];
                ^
test.cc:3:9: note: an attribute that appertains to a type-specifier is ignored

而 clang 3.2 不会产生警告(即使使用 -Weverything -Wno-c++98-compat -pedantic).所以我想知道我上面的代码是否正确,更一般地说,alignas() 可以和不可以放置在哪里.

whereas clang 3.2 creates no warning (even with -Weverything -Wno-c++98-compat -pedantic). So I wonder whether my code above is correct and, more generally, where alignas() can and cannot be placed.

编辑(2013 年 4 月):

标准中的相关文章是7.6.2,特别是7.6.2.1

The relevant article from the standard is 7.6.2, in particular 7.6.2.1

对齐说明符可以应用于变量或类数据成员,但不能应用于位域、函数参数、catch 子句 (15.3) 的形式参数或变量用寄存器存储类说明符声明.对齐说明符也可以应用于类或枚举类型的声明.带有省略号的对齐说明符是包扩展 (14.5.3).

An alignment-specifier may be applied to a variable or to a class data member, but it shall not be applied to a bit-field, a function parameter, the formal parameter of a catch clause (15.3), or a variable declared with the register storage class specifier. An alignment-specifier may also be applied to the declaration of a class or enumeration type. An alignment-specifier with an ellipsis is a pack expansion (14.5.3).

正如 Red XIII 已经挖掘出来的那样.但是,我不够专业,不知道这对我上面的测试意味着什么.

as already dug out by Red XIII. However, I'm not expert enough to know what this means for my test above.

如果 clang 接受我的属性这一事实意味着什么,那么也许值得一提的是,当尝试使用 using 指令而不是 typedef 时,clang 也会抱怨.此外,与此问题早期版本中的声明相反,gcc 不仅警告,而且确实忽略了我对对齐的希望.

If the fact that clang accepts my attribute means anything, it's perhaps worth mentioning that when trying to use a using directive instead of a typedef, clang also complains. Also, contrary to a statement in an earlier version of this question, gcc does not only warn, but indeed ignores my wish for alignment.

推荐答案

您不能对 typedef 应用对齐方式.在对齐说明符的 C++ 模型中,对齐是类型本身不可分割的一部分,并且 typedef 不会创建新类型(它只为现有类型提供新名称),因此它是在 typedef 声明中应用对齐说明符没有意义.

You cannot apply an alignment to a typedef. In the C++ model of alignment specifiers, the alignment is an inseparable part of the type itself, and a typedef does not create a new type (it only provides a new name for an existing type) so it is not meaningful to apply an alignment specifier in a typedef declaration.

来自[dcl.align] (7.6.2)p1:

对齐说明符可以应用于变量或类数据成员 [...].alignment-specifier 也可以应用于类的声明或定义(在 elaborated-type-specifier (7.1.6.3) 或 class-head(分别为第 9 条)和枚举的声明或定义(在 opaque-enum-declarationenum-head,分别为 (7.2)).

An alignment-specifier may be applied to a variable or to a class data member [...]. An alignment-specifier may also be applied to the declaration or definition of a class (in an elaborated-type-specifier (7.1.6.3) or class-head (Clause 9), respectively) and to the declaration or definition of an enumeration (in an opaque-enum-declaration or enum-head, respectively (7.2)).

这些是标准规定可以应用对齐说明符 (alignas(...)) 的唯一地方.请注意,这包括 typedef 声明,也不包括 alias-declarations.

These are the only places where the standard says an alignment-specifier (alignas(...)) may be applied. Note that this does not include typedef declarations nor alias-declarations.

[dcl.attr.grammar] (7.6.1)p4:

如果属于某个实体或语句的 attribute-specifier-seq 包含不允许应用于该实体或语句的 属性,则程序有问题-形成.

If an attribute-specifier-seq that appertains to some entity or statement contains an attribute that is not allowed to apply to that entity or statement, the program is ill-formed.

此措辞旨在应用于 alignas 以及可能出现在 attribute-specifier-seq 中的其他形式的属性,但在以下情况下未正确更新对齐从真实"属性转变为另一种属性说明符序列.

This wording was intended to apply to alignas as well as the other forms of attribute that may appear within an attribute-specifier-seq, but was not correctly updated when alignment switched from being a "real" attribute to being a different kind of attribute-specifier-seq.

所以:您使用 alignas 的示例代码应该是格式错误的.C++ 标准目前没有明确说明这一点,但它也不允许使用,因此目前它会导致未定义的行为(因为标准没有为其定义任何行为).

So: your example code using alignas is supposed to be ill-formed. The C++ standard does not currently explicitly say this, but it also does not permit the usage, so instead it currently would result in undefined behavior (because the standard does not define any behavior for it).

这篇关于我在哪里可以在 C++11 中使用 alignas()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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