Authors: Jay Ghiron
Date: 2026-04-02
Submitted against: C23
Status: Open
The definition of common initial sequences fails to mention alignment:
One special guarantee is made to simplify the use of unions: if a union contains several structures that share a common initial sequence (see following sentence), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members.
(C23 6.5.3.4 "Structure and union members" paragraph 6.)
Meaning that currently, the following two structures have all of their members in the common initial sequence:
struct A{
int x;
int y;
};
struct B{
int x;
alignas(alignof(int)*2)int y;
};
That is, alignas cannot change how padding is inserted within a
structure. No compilers actually follow this, however. The
definition of type compatibility requires alignments to be the same:
Moreover, two complete structure, union, or enumerated types declared with the same tag are compatible if members satisfy the following requirements:
...
- if one member of the pair is declared with an alignment specifier, the other is declared with an equivalent alignment specifier;
(C23 6.2.7 "Compatible type and composite type" paragraph 1.)
It is not clear what "equivalent alignment specifier" means. Consider the following four translation units:
struct S{int x;};/* TU 1 */
struct S{alignas(0)int x;};/* TU 2 */
struct S{alignas(int)int x;};/* TU 3 */
struct S{alignas(alignof(int))int x;};/* TU 4 */
GCC considers all four structure types to be compatible, even though
it seems clear that the first structure type should not be compatible
with the third structure type or fourth structure type. The first two
structure types being compatible is less clear, the second has an
alignment specifier and the first does not but alignas(0) is
described as having no effect.
Is it intended for common initial sequences to consider alignment
requirements specified by alignas?
What does "equivalent alignment specifier" mean? What if there are multiple alignment specifiers? Should an alignment specifier that has no effect or does not alter the alignment requirements change type compatibility? See also https://github.com/cplusplus/CWG/issues/867.