能否将枚举值标记为已弃用?
例如:
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue, // deprecated
fourthvalue
};
第二种解决方案是使用ifdef分别实现MSVC和GCC的解决方案。
能否将枚举值标记为已弃用?
例如:
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue, // deprecated
fourthvalue
};
第二种解决方案是使用ifdef分别实现MSVC和GCC的解决方案。
你可以这样做:
enum MyEnum {
firstvalue = 0,
secondvalue,
thirdvalue, // deprecated
fourthvalue
};
#pragma deprecated(thirdvalue)
那么每当变量被使用时,编译器将会输出以下内容:
warning C4995: 'thirdvalue': name was marked as #pragma deprecated
编辑
这看起来有点不正式,而且我没有GCC编译器确认(有人能为我做这件事吗?),但应该可以工作:
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的答案的结合
MyEnum test = MyEnum::thirdvalue;
- Konstantin您可以从C++14开始使用[[deprecated]]属性。
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3760.html
因此,您将会这样写:
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue [[deprecated]],
fourthvalue
};
当你使用显式赋值的方式废弃一个枚举值时,它会看起来像这样:
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue [[deprecated]] = 2,
fourthvalue
};
枚举 E { A [[已弃用]] = 3, B };
- Michał Walenciak从GCC 6开始,您可以简单地弃用枚举:
enum {
newval,
oldval __attribute__ ((deprecated ("too old")))
};
enum MyEnum
{
foo,
bar,
baz
};
typedef __attribute__ ((deprecated))MyEnum MyEnum_deprecated;
#define bar ((MyEnum_deprecated) bar)
int main ()
{
int a = foo; // yuck, why did C++ ever allow that...
int b = bar;
MyEnum c = foo;
MyEnum d = bar;
return 0;
}
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue
};
__attribute__ ((deprecated)) const MyEnum fourthvalue = MyEnum(thirdvalue + 1);
MyEnum
不足以容纳您分配的值(thirdvalue + 1
),它也会中断。 - Tony Delroy你也许可以使用一些宏编程技巧。
enum MyEnum {
firstvalue = 0
secondvalue,
real_thirdvalue, // deprecated
fourthvalue
};
template <MyEnum v>
struct real_value
{
static MyEnum value()
{
1 != 2U; // Cause a warning in for example g++. Leave a comment behind for the user to translate this warning into "thirdvalue is deprecated"
return v;
}
};
#define thirdvalue (real_value<real_thirdvalue>::value());
当需要一个常量时,这种方法无法正常工作。
使用编译器相关的编译指示:这里是Gcc和Visual Studio的文档。
thirdvalue
用作符号常量。它还会在每个尝试使用thirdvalue
的地方产生警告。#include <boost/serialization/static_warning.hpp>
enum MyEnum {
firstvalue = 0,
secondvalue,
deprecated_thirdvalue, // deprecated
fourthvalue
};
template <int line>
struct Deprecated
{
BOOST_SERIALIZATION_BSW(false, line);
enum {MyEnum_thirdvalue = deprecated_thirdvalue};
};
#define thirdvalue (static_cast<MyEnum>(Deprecated<__LINE__>::MyEnum_thirdvalue))
enum {symbolic_constant = thirdvalue};
int main()
{
MyEnum e = thirdvalue;
}
在GCC上,我会收到警告,最终指向包含thirdvalue
的罪犯行。
请注意,使用Deprecated
模板使得“实例化位置”编译器输出行显示了废弃枚举的使用位置。
如果您能够想出一个在Deprecated
模板内部生成可移植警告的方法,则可以省去对Boost的依赖。
C++14对标准语法属性(包括[[deprecated]]
)的支持比C++11更好,因为它允许枚举器也被注释(参见N3760)。这意味着OP的示例现在可以像这样:
enum MyEnum {
firstvalue = 0,
secondvalue,
thirdvalue [[deprecated]],
fourthvalue
};
_Pragma
,您确实可以在宏的扩展中包含一个编译指示。为什么不发表一个答案呢? - Matthieu M.