Clang 支持使用 C++11 generalized attribute syntax for vendor-specific attributes .这意味着理论上,我可以使用 Clang 支持的任何属性,并使用 [[attribute]]
语法对其进行标记。
但是,我在使用 transparent_union
属性时遇到问题。我记得 C++ 基本上可以将属性放在任何地方,所以我尝试了以下所有方法,但都无济于事:
[[gnu::transparent_union]] union x { int foo; };
// error: an attribute list cannot appear here
union [[gnu::transparent_union]] x { int foo; };
// warning: transparent_union attribute can only be applied to a union definition;
// attribute ignored [-Wignored-attributes]
union x [[gnu::transparent_union]] { int foo; };
// error: an attribute list cannot appear here
// warning: transparent_union attribute can only be applied to a union definition;
// attribute ignored [-Wignored-attributes]
union x { int foo; } [[gnu::transparent_union]];
// error: an attribute list cannot appear here
// warning: transparent_union attribute can only be applied to a union definition;
// attribute ignored [-Wignored-attributes]
使用 __attribute__
语法的正确位置似乎是在 union 定义的末尾:
union x { int foo; } __attribute__((transparent_union));
// OK
将 [[gnu::transparent_union]]
与 Clang 放在一起的正确位置(如果有的话)在哪里?
最佳答案
在 C++11 标准中,它在语法中显示 attribute-specifier-seq 位于 class-key 之后,这就是 union
是。
class-head: class-key attribute-specifier-seqopt class-head-name class-virt-specifieropt base-clauseopt class-key attribute-specifier-seqopt base-clauseopt [..] class-key: class struct union
This is confirmed as:
union [[gnu::transparent_union]] x { int foo; };
似乎是唯一不会引起诊断的语法。所以这可能是一个 Clang 错误。
请记住,__attribute__
看起来是一个 GNU extension而 [[attribute]]
是 C++ 的一部分。
关于c++ - 我应该在哪里指定 [[gnu::transparent_union]]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031049/