This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 112e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-12-02


959. Alignment attribute for class and enumeration types

Section: 9.12.2  [dcl.align]     Status: CD2     Submitter: Daveed Vandevoorde     Date: 31 August, 2009

[Voted into WP at March, 2010 meeting.]

According to 9.12.2 [dcl.align] paragraph 1, an alignment attribute can be specified only for a variable or a class data member. The corresponding Microsoft and GNU attributes can be also specified for a class type, and this usage seems to be widespread. It should be permitted with the standard attribute and there seems no good reason not to do so for enumeration types, as well.

Notes from the October, 2009 meeting:

Although there was initial concern for how to integrate the suggested change into the type system, it was observed that current practice is to have the attribute affect only the layout, not the type.

Proposed resolution (February, 2010):

  1. Change 9.12.2 [dcl.align] paragraphs 1-2 as follows:

  2. ...The attribute can be applied to a variable that is neither a function parameter nor declared with the register storage class specifier and to a class data member that is not a bit-field. The attribute can also be applied to the declaration of a class or enumeration type.

    When the alignment attribute is of the form align(assignment-expression):

  3. Change 9.12.2 [dcl.align] paragraphs 4-6 as follows:

  4. When multiple alignment attributes are specified for an object entity, the alignment requirement shall be set to the strictest specified alignment.

    The combined effect of all alignment attributes in a declaration shall not specify an alignment that is less strict than the alignment that would otherwise be required for the object entity being declared.

    If the defining declaration of an object entity has an alignment attribute, any non-defining declaration of that object entity shall either specify equivalent alignment or have no alignment attribute. Conversely, if any declaration of an entity has an alignment attribute, every defining declaration of that entity shall specify an equivalent alignment. No diagnostic is required if declarations of an object entity have different alignment attributes in different translation units.

  5. Insert the following as a new paragraph after 9.12.2 [dcl.align] paragraph 6:

  6. [Example:

      // Translation unit #1:
      struct S { int x; } s, p = &s;
    
      // Translation unit #2:
      struct [[align(16)]] S;   // error: definition of S lacks alignment; no
      extern S* p;              //            diagnostic required
    

    end example]

  7. Delete 9.12.2 [dcl.align] paragraph 8:

  8. [Note: the alignment of a union type can be strengthened by applying the alignment attribute to any non-static data member of the union. —end note]