我应该在哪里指定 [[gnu::transparent_union]]
Where should I specify [[gnu::transparent_union]]?
Clang 支持对特定于供应商的属性使用 C++11 通用属性语法。这意味着理论上,我可以使用 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 x { int foo; } __attribute__((transparent_union));
// OK
如果有的话,在哪里放置[[gnu::transparent_union]]
与 Clang 在一起?
在 C++11 标准中,它在语法中显示属性说明符 seq 在类键之后,这就是 union
是什么。
班长: class-key 属性说明符-seq opt 类头名称 class-virt-specifier opt base-clauseopt 类键属性说明符-seqopt 基句选项[..]类键: .class 结构 联盟
这被确认为:
union [[gnu::transparent_union]] x { int foo; };
似乎是唯一不会引发诊断的语法。所以也许这是一个叮当虫。
请记住,__attribute__
似乎是GNU扩展,而[[attribute]]
是C++的一部分。
相关文章:
- C++Union/Struct位域的实现和可移植性
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 错误:"Left of getValue must have class/struct/union"
- C++Union,两个活跃成员,仅简历资格不同
- 如何以"union-like"方式更改C++向量的数据类型
- 如何控制或优化或删除或释放 UNION 中未使用的内存
- 对结构使用 %union
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- [class.union]/3 注释中"Absent default member initializers, ..."的句子的目的是什么?
- 在Union UB中,写作超出了较小尺寸的成员的规模
- Sockaddr union and getaddrinfo()
- Union-Find方法性能,迭代与递归
- 使用Flatbuffer Union通过ZeroMQ发送不同的事件
- 如何使光标"True Transparent"窗口,最好是在纯QML上?(Qt 5.7)
- 如何从C 中的Inner Union访问类成员
- 获取"Use of undefined type"和"Must have class/struct/union"错误
- 我对set.union(C++)有问题
- 不断收到错误"member reference base type 'string [1000]' is not a structure or union"不知道如何解决?
- A类{..};typedef a a_t;Union u{...; a_t ai;...};不起作用
- 前向声明不够时如何避免"'identifier' uses undefined class/struct/union 'name'"错误?