C++ Template for mapping struct type to enum?

前端 未结 3 1793
夕颜
夕颜 2021-01-15 12:15

I have something like:

struct A { ... };
struct B { ... };
struct C { ... };

class MyEnum {
public:
    enum Value { a, b, c; }
}

template

        
相关标签:
3条回答
  • 2021-01-15 12:39

    You can map types to enums at compile time:

    #include <iostream>
    
    struct A { int n; };
    struct B { double f; };
    struct C { char c; };
    
    class MyEnum
    {
    public:
        enum Value { a, b, c };
    };
    
    template<typename T> struct StructToMyEnum {};
    
    template<> struct StructToMyEnum<A> {enum {Value = MyEnum::a};};
    template<> struct StructToMyEnum<B> {enum {Value = MyEnum::b};};
    template<> struct StructToMyEnum<C> {enum {Value = MyEnum::c};};
    
    int main (int argc, char* argv[])
    {
        std::cout << "A=" << StructToMyEnum<A>::Value << std::endl;
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-15 12:41

    Why don't you just make a static member variable of type enum and add it to your structs?

    struct A
    {
    //Stuff
    
    static MyEnum enumType; // Don't forget to assign a value in cpp
    };
    

    Than you can just do:

    MyEnum e = StructA::enumType;
    

    Or do you really want to use templates?

    0 讨论(0)
  • 2021-01-15 12:43

    The multiple definitions are because you need to either add the inline keyword or push the implementation of your specializations into a cpp file, leaving only the declarations of such in the header.

    You could probably use mpl::map to write a sort-of generic version. Something like so:

    struct A {};
    struct B {};
    struct C {};
    
    enum Value { a,b,c };
    
    template < typename T >
    Value get_value()
    {
      using namespace boost::mpl;
      typedef mpl::map
      < 
        mpl::pair< A, mpl::int_<a> >
      , mpl::pair< B, mpl::int_<b> >
      , mpl::pair< C, mpl::int_<c> >
      > type_enum_map;
    
      typedef typename mpl::at<type_enum_map, T>::type enum_wrap_type;
    
      return static_cast<Value>(enum_wrap_type::value);
    }
    
    0 讨论(0)
提交回复
热议问题