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++ 将枚举值标记为已弃用?
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
