为什么 C++17 向命名空间和枚举器添加属性?

Why is C++17 adding attributes to namespaces and enumerators?

如问题所述,我知道 C++17 中将有命名空间和枚举器的属性。这将如何影响我们的代码?这些属性是什么?它们允许我们做什么?我找不到任何好的资源来理解这个新功能。

目前,如果要贬低特定的枚举值,则需要依赖编译器扩展。例如,在 clang 中,您可以通过以下方式指定弃用的枚举值:

enum OperationMode {
  OM_Invalid,
  OM_Normal,
  OM_Terrified __attribute__((deprecated)),
  OM_AbortOnError __attribute__((deprecated)) = 4
};

一旦枚举和命名空间支持属性,就会有一种标准的交叉编译器方式来实现类似的功能:

enum OperationMode {
  OM_Invalid,
  OM_Normal,
  OM_Terrified [[deprecated("re-named to invalid")]],
  OM_AbortOnError  [[deprecated("exceptions are used instead")]] = 4
};

其他属性可能有一天会发现与名称空间和枚举值相关,但正如提案作者所述:

This paper proposes resolving these issues by allowing attributes to be specified on enumerators and namespaces, and extends the [[deprecated]] attribute to apply to these entities, as was originally intended.