I have a compilation error on clang and a warning with gcc with this code:
static alignas(16) int one_s = 1; // clang: error: an attribute list cannot appear here; gcc: warning: attribute ignored;
static __attribute__((aligned(16))) int zero_s = 0; // on the other hand this works well on both compilers...
alignas(16) int one = 1; // this also works on both compilers
__attribute__((aligned(16))) int zero = 0; // as well as this
Does anyone know why alignas is not accepted in the declaration that contains the static keyword? I used the --std=c++11 compiler option with both gcc and clang. (Edit: I used clang 3.4 and above and gcc 4.8 and above)
Note that when compiling with Visual Studio (CL 19 RC) I don't get an error when using alignas in a static declaration like that.
static alignas(16) int one_s = 1;
is ill-formed as per the grammar rules in simple-declaration:Next we move onto decl-specifier-seq:
Now, neither of the above two rules allow
static alignas(16) int
.Solution
The correct syntax would be:
This time, the grammar allows it.
Next we move onto:
Here is the msvc bug: MSVC accepts invalid program having static alignas(16) int one_s = 1;