static_assert in structure and union definitionsAuthors: Jay Ghiron
Date: 2026-02-17
Submitted against: C23
Status: Open
The standard syntactically allows the following structure definition:
struct S{int x;static_assert(sizeof(int)==4);};
However, it appears to violate the following constraint (C23 6.7.3.2 "Structure and union specifiers" paragraph 2):
A member declaration that does not declare an anonymous structure or anonymous union shall contain a member declarator list.
It seems unintended to syntactically allow static_assert while
forbidding it. A second issue with this constraint is that it does not
forbid:
struct A{int y;struct B{struct{int z;};};};
Since the member declaration struct B{struct{int z;};}; does declare
an anonymous structure, just not one within A. I assume that this
is not intended, however.
Considering "The optional attribute specifier sequence in a member declaration appertains to each of the members declared by the member declarator list; it shall not appear if the optional member declarator list is omitted." (C23 6.7.3.2 paragraph 9) and "Except where the type is inferred (6.7.10), at least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each member declaration and type name." (C23 6.7.3.1 paragraph 2) also use the phrase "member declaration" to not include static_assert declarations, a correction can separate static_assert-declaration from member-declaration instead of changing each of the uses of "member declaration".
A correction to the second issue should be done similar to C23 6.7.1 paragraph 2, except that it requires the absence of a tag. That wording is not perfect either, adding examples to clarify the intent would be useful in 6.7.3.2 as well.