enum-int casting: operator or function

后端 未结 5 1765
走了就别回头了
走了就别回头了 2021-01-02 12:59

In the external code that I am using there is enum:

enum En {VALUE_A, VALUE_B, VALUE_C};

In another external code that I am using there ar

5条回答
  •  我在风中等你
    2021-01-02 13:17

    Since you can't just cast here, I would use a free function, and if there are likely to be other enums that also need converting, try to make it look a little bit like the builtin casts:

    template
    T my_enum_convert(int);
    
    template<>
    En my_enum_convert(int in) {
        switch(in) {
            case ValA: return VALUE_A;
            case ValB: return VALUE_B;
            case ValC: return VALUE_C;
            default: throw std::logic_error(__FILE__ ": enum En out of range");
        }
    }
    
    int my_enum_convert(En in) {
        switch(in) {
            case VALUE_A: return ValA;
            case VALUE_B: return ValB;
            case VALUE_C: return ValC;
            // no default, so that GCC will warn us if we've forgotten a case
        }
    }
    
    En enumValue = my_enum_convert(ValA);
    int hashDefineValue = my_enum_convert(VALUE_A);
    enumValue = my_enum_convert(0); // throws exception
    

    Or something like that - might adjust it if issues arise while using it.

    The reason I wouldn't use implicit conversion is that there already is an implicit conversion from En to int, which gives the wrong answer. Even if you can reliably replace that with something which gives the right answer, the resulting code won't look as though it's doing any conversion. IMO this will hinder anyone who later looks at the code more than typing a call to a conversion routine will hinder you.

    If you want converting to int and converting from int to look very different, then you could give the template and the function above different names.

    Alternatively, if you want them to look the same (and more like a static_cast), you could do:

    template
    T my_enum_convert(En in) {
        switch(in) {
            case VALUE_A: return ValA;
            case VALUE_B: return ValB;
            case VALUE_C: return ValC;
        }
    }
    
    int hashDefineValue = my_enum_convert(VALUE_A);
    

    As written, T must have an implicit conversion from int. If you want to support T that only has an explicit conversion, use "return T(ValA);" instead (or "return static_cast(ValA);", if you consider that single-arg constructors are C-style casts and hence impermissible).

提交回复
热议问题