带有 C++ 和 clang 的属性

Attribute packed with C++ and clang

我认为双括号表示法旨在取代 __attribute__((foo)),但我没有得到等价物。

C++14 的 Clang 工具链集。

class [[packed]] test {};
class __attribute__((packed)) test2 {};

test 忽略了关于未知属性 packed 的警告。 test2 没有警告。如果我用虚构的属性替换 packed,两者都会发出警告。

在 Clang 中打包结构的最佳方式是什么?

I thought that the double bracket notation was intended to replace __attribute__((foo)), but I'm not getting equivalence.

这并不意味着完全等同。双括号表示法是为了 C++ 标准引入具有或多或少标准含义的属性。这些通常已经作为编译器特定属性存在,但这并不意味着所有编译器特定属性都具有相应的标准属性。所以你不能指望 [[packed]] 是一个东西,仅仅因为 __attribute__((packed)) 是。

尽管如此,该标准确实为编译器提供了添加使用双括号的自定义属性的选项。但是这些属性通常是有范围的。在这种情况下,自定义属性被命名为 [[gnu::packed]]。顾名思义,它是特定于供应商的。