c++language-lawyer

GCC and MSVC accept "alignas" whereas Clang rejects it


I wrote the following program that compiles with GCC and MSVC, but Clang rejects it. The program is:

int a[2] alignas(16) [5]; //GCC: Ok, Clang: No, MSVC: OK

Demo

What is the correct behavior as per the latest C++ standard?


Solution

  • The program is ill-formed because dcl.align doesn't allow alignas(16) to be applied to an array type(int[2] here) and through CWG2205 in your example the alignas(16) appertains to the array type int[2].

    An alignment-specifier may be applied to a variable or to a class data member, but it shall not be applied to a bit-field, a function parameter, or an exception-declaration ([except.handle]). An alignment-specifier may also be applied to the declaration of a class (in an elaborated-type-specifier ([dcl.type.elab]) or class-head ([class]), respectively). An alignment-specifier with an ellipsis is a pack expansion ([temp.variadic]).