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

查看:1286
本文介绍了在哪里可以在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];

但是,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月)

EDIT (Apr 2013):

标准的相关文章为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)或使用register storage class specifier声明的变量。对齐说明符也可以应用于类或枚举类型的声明。具有省略号的对齐标识符是一个扩展包(14.5.3)。已经由Red XIII挖掘出来的

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).

。但是,我不够专业,不知道这对我上面的测试意味着什么。

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

如果clang接受我的属性意味着什么,这也许值得一提,当尝试使用使用指令而不是 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 :

From [dcl.align] (7.6.2)p1:


应用于变量或类数据成员[...]。 (7.1.6.3)或 class-head (第9条))以及枚举的声明或定义(在 opaque-enum-declaration
enum-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 声明或别名声明

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 中,但是当对齐从真实属性切换到另一种类型的 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天全站免登陆