[ACCEPTED]-c++ mark enum value as deprecated?-deprecated
you could do this:
enum MyEnum {
firstvalue = 0,
secondvalue,
thirdvalue, // deprecated
fourthvalue
};
#pragma deprecated(thirdvalue)
then when ever the variable 5 is used, the compiler will output the following:
warning C4995: 'thirdvalue': name was marked as #pragma deprecated
EDIT
This 4 looks a bit hacky and i dont have a GCC 3 compiler to confirm (could someone do that 2 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
it's a combination 1 of my answer and MSalters' answer
You can use the [[deprecated]] attribute 1 from C++14 on.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3760.html
Beginning with GCC 6 you can simply deprecate 1 enums:
enum {
newval,
oldval __attribute__ ((deprecated ("too old")))
};
Well, since we're at macro hacks already, here 8 is mine :-)
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;
}
This works with gcc, and it 7 does not require you to break type-safety. Unluckily 6 it still abuses your code with macros, so 5 meh. But as far as I could figure, it's 4 as good as it gets.
The proposal made by 3 Tom is much cleaner (works for MSVC, I assume), but 2 unluckily the only message gcc will give 1 you is "ignoring pragma".
You can declare enum constants outside an 1 enum declaration:
enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue
};
__attribute__ ((deprecated)) const MyEnum fourthvalue = MyEnum(thirdvalue + 1);
You might be able to use some macro hackery.
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());
This 2 won't work in a context when a constant 1 is needed.
I have a solution (inspired from Mark B's) that 11 makes use of boost/serialization/static_warning.hpp. However, mine 10 allows thirdvalue
to be used as a symbolic constant. It 9 also produces warnings for each place where 8 someone attempts to use 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;
}
On GCC I get warnings 7 that ultimately point to the culprit lines 6 containing thirdvalue
.
Note that the use of the Deprecated
template 5 makes it so that an "instantiated here" compiler 4 output line shows where the deprecated enum 3 is used.
If you can figure out a way to portably 2 generate a warning inside the Deprecated
template, then 1 you can do away with the dependency on Boost.
C++14's support for a standard syntax attributes 4 (including [[deprecated]]
) improved on that of C++11's 3 by allowing enumerators also to be annotated 2 (see N3760). This means the OP's example can 1 now look like this:
enum MyEnum {
firstvalue = 0,
secondvalue,
thirdvalue [[deprecated]],
fourthvalue
};
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.