c++ mark enum value as deprecated?(c++ 将枚举值标记为已弃用?)
问题描述
是否可以将枚举值标记为已弃用?
Is it possible to mark an enum value as deprecated?
例如
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue, // deprecated
fourthvalue
};
二等奖的解决方案是 ifdef MSVC 和 GCC 解决方案.
A second prize solution would be to ifdef a MSVC and a GCC solution.
推荐答案
你可以这样做:
enum MyEnum {
firstvalue = 0,
secondvalue,
thirdvalue, // deprecated
fourthvalue
};
#pragma deprecated(thirdvalue)
然后每当使用该变量时,编译器将输出以下内容:
then when ever the variable is used, the compiler will output the following:
warning C4995: 'thirdvalue': name was marked as #pragma deprecated
编辑
这看起来有点 hacky,我没有 GCC 编译器来确认(有人可以为我做这件事吗?)但它应该可以工作:
EDIT
This looks a bit hacky and i dont have a GCC compiler to confirm (could someone do that for me?) but it should work:
enum MyEnum {
firstvalue = 0,
secondvalue,
#ifdef _MSC_VER
thirdvalue,
#endif
fourthvalue = secondvalue + 2
};
#ifdef __GNUC__
__attribute__ ((deprecated)) const MyEnum thirdvalue = MyEnum(secondvalue + 1);
#elif defined _MSC_VER
#pragma deprecated(thirdvalue)
#endif
这是我的回答和 MSalters 的回答的结合
it's a combination of my answer and MSalters' answer
这篇关于c++ 将枚举值标记为已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c++ 将枚举值标记为已弃用?


基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01