c++ - 我应该在哪里指定 [[gnu::transparent_union]]?

标签 c++ c++11 clang

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/

相关文章:

c++ - 在 ostream 中使用迭代器失败

c++ - 私有(private)静态成员可以用作其类的成员函数的默认参数吗?

emacs 使用 auto-complete-clang 自动完成,显示无效的成员函数

c++ - 在存在不可预测的类型别名的情况下如何处理显式模板实例化?

c++ - 实现昂贵的 C++ 迭代器

c++ - 如何在 C++ 中使用带有映射的排序函数?

c++ - clang 编译器卡在 Windows 上

c++ - 没有内联汇编的 x64 函数绕行

c++ - Jsoncpp 没有正确读取指数

c++ - 如何通过 C++ std::set 尽早结束迭代?