问题
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: The relevant grammar rules are:
// 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 )
回答1:
Section 6.7.5, p 6, clearly specifies that this also concerns alignment of members
The alignment requirement of the declared object or member is taken to be the specified alignment.
So the intended semantic is that one. If as you say the formal specification of the grammar misses that bit (I didn't check), this is a defect, and you should report it.
Edit: Looking into the grammar, it seems to me that an addition of alignment-specifier
in 6.7.2.1 is missing in the cases of specifier-qualifier-list
, and also that a textual explanation in para 14 would be in order.
来源:https://stackoverflow.com/questions/10676827/structure-member-alignment-with-alignas