为什么在使用 clang 进行静态声明时,alignas 无法编译?

问题描述 投票:0回答:1

我在 clang 上遇到编译错误,并且使用以下代码的 gcc 出现警告:

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

有谁知道为什么在包含 static 关键字的声明中不接受alignas?我将 --std=c++11 编译器选项与 gcc 和 clang 一起使用。 (编辑:我使用 clang 3.4 及以上版本和 gcc 4.8 及以上版本)

请注意,使用 Visual Studio (CL 19 RC) 进行编译时,在类似的静态声明中使用alignas 时不会出现错误。

c++11 compiler-errors static clang alignas
1个回答
0
投票
根据

简单声明

中的语法规则,
static alignas(16) int one_s = 1; 格式错误

 simple-declaration:
   decl-specifier-seq init-declarator-listopt ;  <--------this is the case here
   attribute-specifier-seq decl-specifier-seq init-declarator-list ;
   attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [ identifier-list ] initializer ; 

接下来我们进入decl-specifier-seq

decl-specifier-seq:
   decl-specifier attribute-specifier-seqopt   //this doesn't match
   decl-specifier decl-specifier-seq           //this doesn't match either

现在,以上都不适合

static alignas(16) int


正确的语法是:

alignas(16) static int one_s = 1;

这一次,语法允许了。

simple-declaration:
   decl-specifier-seq init-declarator-listopt ;
   attribute-specifier-seq decl-specifier-seq init-declarator-list ; //this is suitable now
© www.soinside.com 2019 - 2024. All rights reserved.