C and C++ Alignment Compatibility

ISO/IEC JTC1 SC22 WG14 N1447 - 2010-03-22
ISO/IEC JTC1 SC22 WG21 N3093 = 10-0083 - 2010-03-22

Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org

Introduction
    Fundamental Alignment
    Alignof Operator
    Declaring Alignment
    Alignment Semantics
    Aligned Allocation
Proposed Wording for C
    4. Conformance
    6.2.8 Alignment of objects
    6.4.1 Keywords
    6.7.5 Alignment specifier
    7.1.2 Standard headers
    7.new Alignment <stdalign.h>
    A.1.2 Keywords
    A.2.2 Declarations
    B.new Alignment <stdalign.h>
Proposed Wording for C++
    1.1 Scope [intro.scope]
    2.12 Keywords [lex.key]
    3.2 One definition rule [basic.def.odr]
    3.11 Alignment [basic.align]
    7.1 Specifiers [dcl.spec]
    7.1.7 Alignment specifiers [dcl.align]
    7.6.2 Alignment attribute [dcl.align]
    17.6.1.2 Headers [headers]
    18.1 General [support.general]
    18.10 Other runtime support [support.runtime]
    20.7.6.6 Other transformations [meta.trans.other]
    D.5 C standard library headers [depr.c.headers]

Introduction

The compatibility between the C and C++ alignment facilities is important to many members of the respective communities. This paper analyses the compatibility between the draft standards, WG14 N1425. and N3035, WG21 N3035. with respect to alignment.

This document presents what we believe to be the consensus proposal of the C and C++ compatibility mailing list. It incorporates detailed proposed wording for both standards.

Fundamental Alignment

Both languages have the same notion of fundamental alignment (<= alignof(max_align_t)) and extended alignment (> alignof(max_align_t)). Extended alignment support is implementation-defined in both languages.

Both languages intend to constrain the alignments to powers of two, but the wording to do so is confusing, and likely subject to misinterpretation. In particular, the 'weaker' constraint does not seem to be satisfiable unless the alignments are restricted to powers of two. Explicitly restricting alignment to powers of two would improve both languages.

Proposal

Explicitly constrain alignments to powers of two.

Alignof Operator

Both C and C++ define an alignof operator. These have compatible syntax and semantics:

unary-expression:
alignof ( type-name/id )

Neither language provides an alignof rule for expressions.

unary-expression:
alignof unary-expression

In both languages, the result is of type size_t, and the results are meaningfully comparable.

Declaring Alignment

It is in the declaration of alignment that C and C++ differ significantly.

C adds alignment-specifiers to its grammar.

declaration-specifiers:
alignment-specifier declaration-specifiersopt
alignment-specifier:
_Align ( type-name )
_Align ( constant-expression )

The original C++ proposal did essentially the same, but with a different keyword.

decl-specifier:
alignment-specifier
alignment-specifier:
alignas ( type-id )
alignas ( constant-expression )

However, the current C++ working draft uses attributes.

simple-declaration:
attribute-specifieropt declaration-specifiersopt attribute-specifieropt init-declarator-listopt ;

where the alignment attribute is one of:

attribute:
align ( type-name )
align ( assignment-expression )

and where the assignment-expression shall be an integral constant expression.

Proposal

Spell the C1x keyword as "_Alignas", and revert the C++ proposal to a keyword:

decl-specifier:
alignment-specifier
alignment-specifier:
alignas ( type-id )
alignas ( constant-expression )

Introduce a system header, <stdalign.h>, analagous to <stdbool.h>, that creates an identical spelling in both C and C++ for the keyword. Example:

#ifndef __cplusplus
#define alignas _Alignas
#define __alignas_is_defined 1
#endif

Alignment Semantics

Fortunately, C and C++ have very similar semantics for the effect of declaring alignment.

However, there are some differences.

Proposal

Define each individual alignment to zero to have no effect. That is, when there are multiple alignments specified, only alignments to zero have no effect.

A portable program cannot make use of relaxed alignment, so we propose that it remains disallowed in C and in C++.

Require that a definition or a tentative definition specifies the alignment; other declarations shall have the same alignment or no alignment.

Aligned Allocation

C defines an aligned_alloc function. C++ appears to add nothing for aligned allocation, though does leave some support implementation-defined.

Proposal

Update the C++ working draft section 1.2 [intro.refs] to refer to the 2011 C standard. This date may need to change.

Proposed Wording for C

These edits are relative to working draft WG14 N1425.

4. Conformance

Add <stdalign.h> to the list of headers for freestanding implementations in paragraph 6.

Add <stdalign.h> to the list of forward references.

6.2.8 Alignment of objects

Edit paragraph 1 as follows.

Object types have alignment requirements which place restrictions on the addresses at which objects of that type may be allocated. An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated. An object type imposes an alignment requirement on every object of that type: stricter alignment can be requested using the _Align _Alignas keyword.

Edit paragraph 3 as follows.

An extended alignment is represented by an alignment greater than alignof(max_align_t). It is implementation-defined whether any extended alignments are supported and the contexts in which they are supported. A type having an extended alignment requirement is an over-aligned type. [Footnote: It is intended that every valid alignment value is an integral power of two. —end footnote] Every alignment value shall be an integral power of two.

6.4.1 Keywords

In paragraph 1, change _Align to _Alignas.

6.7.5 Alignment specifier

In paragraph 1, change _Align to _Alignas.

In paragraph 5, change _Align to _Alignas.

Edit paragraph 6 as follows.

The alignment requirement of the declared object or field is taken to be the specified alignment. An alignment specification of zero has no effect. [Footnote: An alignment specification of zero also does not affect other aligment specifications in the same declaration. —end footnote:] When multiple alignment specifiers occur in a declaration, the effective alignment requirement is the strictest specified alignment.

Edit paragraph 7 as follows.

If the definition of an object has an alignment specifier, any other declaration of that object shall either specify equivalent alignment or have no alignment specifier. If the definition of an object does not have an alignment specifier, any other declaration of that object shall have no alignment specifier. If declarations of an object in different translation units have different alignment specifiers, the behavior is undefined.

7.1.2 Standard headers

Add <stdalign.h> to the list of standard headers in paragraph 2.

7.new Alignment <stdalign.h>

Add a new section after "7.17 Boolean type and values <stdbool.h>.

Add a new paragraph 1 as follows.

The header <stdalign.h> defines two macros.

Add a new paragraph 2 as follows.

The macro

alignas

expands to _Alignas.

Add a new paragraph 3 as follows.

The remaining macro is suitable for use in #if preprocessing directives. It is

__alignas_is_defined

which expands to the integer constant 1.

A.1.2 Keywords

In paragraph (6.4.1), change _Align to _Alignas.

A.2.2 Declarations

In paragraph (6.7.5), change _Align to _Alignas.

B.new Alignment <stdalign.h>

Add a new section as follows.

alignas
__alignas_is_defined

Proposed Wording for C++

The proposed wording mostly reverts the alignment specification to that of WG21 N2341, which was adopted in July 2007. These edits are relative to working draft WG21 N3035.

1.1 Scope [intro.scope]

Edit paragraph 2 as follows. Note that this edit may need to be delayed until the C standard appears.

C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:19992011 Programming languages — C (hereinafter referred to as the C standard ). In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, name spaces, inline functions, operator overloading, function name overloading, references, free store management operators, and additional library facilities.

2.12 Keywords [lex.key]

Add the keyword alignas before the keyword alignof.

3.2 One definition rule [basic.def.odr]

Edit within paragraph 4 as follows.

3.11 Alignment [basic.align]

Edit paragraph 1 as follows. Note the inserted comma.

Object types have alignment requirements (3.9.1, 3.9.2) which place restrictions on the addressses at which an object of that type may be allocated. An aligment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated. An object type imposes an alignment requirement on every object of that type; stricter alignment can be requested using the alignment attribute (7.6.2) alignas ([dcl.align]).

Edit paragraph 4 as follows.

Alignments are represented as values of the type std::size_t. Valid alignments include only those values returned by an alignof expression for the fundamental types plus an additional implementation-defined set of values, which may be empty. [Footnote: It is intended that every valid alignment value be an integral power of two. —end footnote] Every alignment value shall be an integral power of two.

7.1 Specifiers [dcl.spec]

Edit paragraph 1 as follows.

The specifiers that can be used in a declaration are

decl-specifier:
storage-class-specifier
type-specifier
function-specifier
alignment-specifier
friend
typedef
constexpr
decl-specifier-seq:
decl-specifier-seqopt decl-specifier

7.1.7 Alignment specifiers [dcl.align]

Add a new section as follows. Editorially, this section might better appear after 7.1.2 Function specifiers [decl.fct.spec] in keeping with its position in the grammar.

Add paragraph 1 as follows.

The alignment specifier has the form

alignment-specifier:
alignas ( type-id )
alignas ( constant-expression )

Add paragraph 2 as follows.

When the alignment specifier is of the form alignas(constant-expression):

Add paragraph 3 as follows.

When the alignment specifier is of the form alignas(type-id), it shall have the same effect as alignas(alignof(type-id)) ([expr.alignof]).

Add paragraph 4 as follows.

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

Add paragraph 5 as follows.

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

Add paragraph 6 as follows.

An alignment specifier shall not be specified in a declaration of a typedef, or a bit-field, or a reference, or a function parameter or return type, or an object declared with the register storage-class specifier. [Note: In short, the specifier can be used on automatic variables, namespace scope variables, and members of class types (as long as they are not bit-fields). In other words, it cannot be used in contexts where it would become part of a type and therefore would affect name mangling, name lookup or ordering of function templates. —end note]

Add paragraph 7 as follows.

If the defining declaration of an object has an alignment specifier, any non-defining declaration of that object shall either specify equivalent alignment or have no alignment specifier. If the defining declaration of an object does not have an alignment specifier, any non-defining declaration of that object shall have no alignment specifier. No diagnostic is required if declarations of an object have different alignment specifiers in different translation units.

Add paragraph 8 as follows.

[Example: An aligned buffer, with an alignment requirement of A and an element type T other than char, signed char or unsigned char, can be declared as:

T alignas(T) alignas(A) buffer[N];
// where N is the number of T elements making up the buffer

Specifying alignas(T) in the alignment specifier list will ensure that the final requested alignment will not be weaker than alignof(T), and therefore the program will not be ill-formed. —end example]

Add paragraph 9 as follows.

[Note: The alignment of a union type can be strengthened by applying the alignment specifier to any member of the union. —end note] [Note: The aligned_union template ([meta.type.synop]) can be used to create a union containing a type with a non-trivial constructor or destructor. —end note]

Add paragraph 10 as follows.

[Example:

alignas(double) void f(); // error: alignment applied to function
alignas(double) unsigned char a[sizeof(double)]; // array of characters, suitably aligned for a double
extern unsigned char a[sizeof(double)]; // no alignas necessary
extern alignas(float) unsigned char a[sizeof(double)]; // error: different alignment in declaration

end example]

7.6.2 Alignment attribute [dcl.align]

Remove this section.

17.6.1.2 Headers [headers]

Add <cstdalign> to "Table 14 — C++ headers for C library facilities".

18.1 General [support.general]

Add <cstdalign> to "Table 16 — Language support library summary" under "18.10 Other runtime support".

18.10 Other runtime support [support.runtime]

Add a new table after "Table 26 — Header <cstdbool> synopsis".

Table ?? — Header <cstdalign> synopsis".
Type Names(s)
Macro: __alignas_is_defined

Add a new paragraph after paragraph 6.

The header <cstdalign> and the header <stdalign.h> shall not define a macro named alignas.

20.7.6.6 Other transformations [meta.trans.other]

Edit table 51 as follows.

Table 51 — Other transformations
TemplateConditionComments
template <std::size_t Len, std::size_t Align = default-alignment> struct aligned_storage; Len shall not be zero. Align shall be equal to alignment_of<T>::value for some type T or to default-alignment. The value of default-alignment shall be the most stringent alignment requirement for any C++ object type whose size is no greater than Len (3.9). The member typedef type shall be a POD type suitable for use as uninitialized storage for any object whose size is at most Len and whose alignment is a divisor of Align.
template < std::size_t Len, class ... Types > struct aligned_union; At least one type is provided. The member typedef type shall be a POD type suitable for use as uninitialized storage for any object whose type is listed in Types; its size shall be at least Len. The static member alignment_value shall be an integral constant of type std::size_t whose value is the strictest alignment of all types listed in Types.
... ... ...

D.5 C standard library headers [depr.c.headers]

Add <stdalign.h> to "Table 144 — C headers".