4

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.

bluespeck
  • 173
  • 7
  • 4
    Wrong syntax - it needs to be: `alignas(16) static int one_s = 1;`. – Paul R Mar 09 '17 at 10:08
  • 1
    @Paul R I see that this compiles indeed, but VS accepts the other version too, even with /Wall. Is the syntax with alignas(16) before static the correct one (according to the standard)? – bluespeck Mar 09 '17 at 10:27
  • 2
    I'm not much of a language lawyer, otherwise I'd probably have expanded on the above comment and made it an answer, but if you take a pragmatic approach and Google for alignas you'll see that all the examples put `alignas` at the start of the variable definition. VS has always tended to be a little slack on standards adherence (which is why it tends to be a pain porting VS C++ code to other compilers), so it wouldn't surprise me it was slack in this regard too. – Paul R Mar 09 '17 at 11:16

0 Answers0