English 中文(简体)
1. 结构成员与_Alignas
原标题:Structure member alignment with _Alignas

I was wondering about the following: is the new _Alignas alignment specifier in C11 applicable to structure members?

I ve always assumed that much, but a thorough reading of the N1570 public draft seems to indicate that an alignment-specifier cannot appear in a specifier-qualifier-list, which is where I d expect it to be, if it were supported. I ve read the grammar a couple of times but can t figure out how _Alignas is supposed to be permitted in a structure member declaration.

However, it seems to me that the intent of the standard is that _Alignas should be applicable to structure members as the paragraph on _Alignas (§ 6.7.5) states that "an alignment attribute shall not be specified in a declaration of [...] a bit-field". Given that the term "bit-field" is defined in § 6.7.2.1 to be a structure member (precise wording: "such a member is called a bit-field"), I had always interpreted that sentence to implicitly mean alignment specifiers were allowed for non-bit-field members.

Checking against existing implementations shows that both Clang 3.0 and GCC 4.7 support _Alignas on structure members without complaining (with -pedantic). The Clang source code reproduces the same grammar from N1570, except Parser::ParseSpecifierQualifierList allows alignment specifiers; the function does contain a TODO element, though, that reads:

/// TODO: diagnose attribute-specifiers and alignment-specifiers.

The GCC C parser code appears to be similar, i.e. even though it quotes the standard grammar, it allows alignment specifiers in specifier-qualifier lists.

I ve also checked the list of known defects, as well as comp.lang.c and comp.std.c, to see if the topic had been raised there but it doesn t appear to be the case. Hence, my question: are alignment specifiers supposed to be allowed on structure members?

EDIT:相关的图表规则是:

// Compare this...
(6.7) declaration-specifiers:
           storage-class-specifier declaration-specifiers_opt
           type-specifier declaration-specifiers_opt
           type-qualifier declaration-specifiers_opt
           function-specifier declaration-specifiers_opt
           // This seems to be the only place that mentions
           // alignment-specifier on the rhs of a rule.
           alignment-specifier declaration-specifiers_opt

(6.7.2.1) struct-or-union-specifier:
           struct-or-union identifier_opt { struct-declaration-list }
           struct-or-union identifier

(6.7.2.1) struct-declaration-list:
           struct-declaration
           struct-declaration-list struct-declaration

(6.7.2.1) struct-declaration:
           specifier-qualifier-list struct-declarator-list_opt ;
           static_assert-declaration

// ...to this.
(6.7.2.1) specifier-qualifier-list:
           type-specifier specifier-qualifier-list_opt
           type-qualifier specifier-qualifier-list_opt
           // Missing alignment-specifier specifier-qualifier-list_opt?

(6.7.5) alignment-specifier:
          _Alignas ( type-name )
          _Alignas ( constant-expression )
最佳回答

第6.7.5节,第6页明确规定,这也涉及成员的统一。

The alignment requirement of the declared object or member is taken to be the specified alignment.

因此,意图的语义是这样。 如果你说,对图表的正式说明表明,比照(我只字)是一种缺陷,你应该报告。

<><>Edit>: 关于图表,在我看来,在6.7.2.1号文件中没有在<代码>上添加<编码>字母-光谱,并且第14段的文字解释也符合规定。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签