我在 clang 上遇到编译错误,并且在 gcc 中使用以下代码时出现警告:static alignas(16) int one_s = 1; // clang: error: an attribute list could not appeared here; gcc: warning:
我在 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
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 used now
^^^^^^^^alignas^^^^^^^^
接下来我们进入:
decl-specifier-seq:
decl-specifier attribute-specifier-seqopt
decl-specifier decl-specifier-seq <--------this is used here
^^^^static^^^^