c++ mark enum value as deprecated?

后端 未结 8 1922
再見小時候
再見小時候 2020-12-30 23:41

Is it possible to mark an enum value as deprecated?

e.g.

enum MyEnum {
    firstvalue = 0
    secondvalue,
    thirdvalue, // deprecated
    fourthva         


        
8条回答
  •  情书的邮戳
    2020-12-31 00:22

    I have a solution (inspired from Mark B's) that makes use of boost/serialization/static_warning.hpp. However, mine allows thirdvalue to be used as a symbolic constant. It also produces warnings for each place where someone attempts to use thirdvalue.

    #include 
    
    enum MyEnum {
        firstvalue = 0,
        secondvalue,
        deprecated_thirdvalue, // deprecated
        fourthvalue
    };
    
    template 
    struct Deprecated
    {
        BOOST_SERIALIZATION_BSW(false, line);
        enum {MyEnum_thirdvalue = deprecated_thirdvalue};
    };
    
    #define thirdvalue (static_cast(Deprecated<__LINE__>::MyEnum_thirdvalue))
    
    enum {symbolic_constant = thirdvalue};
    
    int main()
    {
        MyEnum e = thirdvalue;
    }
    

    On GCC I get warnings that ultimately point to the culprit lines containing thirdvalue.

    Note that the use of the Deprecated template makes it so that an "instantiated here" compiler output line shows where the deprecated enum is used.

    If you can figure out a way to portably generate a warning inside the Deprecated template, then you can do away with the dependency on Boost.

提交回复
热议问题