“enum class” emulation or solid alternative for MSVC 10.0

后端 未结 3 579
无人共我
无人共我 2020-12-05 11:30

I\'m looking for a hacky kind of solution to the following problem: GCC 4.4+ accepts the following c++0x code:

enum class my_enum
{
    value1,
    value2
};         


        
3条回答
  •  抹茶落季
    2020-12-05 12:20

    Do not use this solution. See the accepted answer by Howard for a better solution. I'm leaving this post here because Howard's answer refers to it.

    If you need to be able to compile your code with a compiler that doesn't yet support a new, not yet standard or not yet widely implemented language feature, it's best to avoid using that language feature in your code.

    That said, as a hack workaround, you can wrap the enum in a struct and use a pair of implicit conversions:

    struct my_enum {
        enum type { 
            value1, 
            value2 
        };
    
        my_enum(type v) : value_(v) { }
    
        operator type() const { return value_; }
    
    private:
    
        type value_;
    };
    

提交回复
热议问题