c++ mark enum value as deprecated?

后端 未结 8 1864
再見小時候
再見小時候 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:28

    You might be able to use some macro hackery.

    enum MyEnum {
        firstvalue = 0
        secondvalue,
        real_thirdvalue, // deprecated
        fourthvalue
    };
    
    template 
    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::value());
    

    This won't work in a context when a constant is needed.

提交回复
热议问题