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

标签 c++ enums namespaces standards

如问题所述,我知道 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.

关于c++ - 为什么 C++17 向命名空间和枚举器添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541829/

相关文章:

c++ - switch 语句中第一个 case 之前的代码

C# 同义枚举处理

linq - 在 .NET 3.5 中的枚举列表上使用 List.Find 或 LINQ

c#-4.0 - 在 C# 中在运行时更改命名空间

javascript - 学习在 Javascript 中使用命名空间和模块

xml - XSD 架构 - 多次使用相同的 namespace

c++ - 32位和64位可以一起工作吗?

c++ - 将结构句柄从托管转换为非托管 C++/CLI

c++ - vector 元素是否保证有序?

javascript - JavaScript 字符串比较和数字比较一样快吗?