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