简体   繁体   中英

C++ Template for mapping struct type to enum?

I have something like:

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

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

template<typename T> MyEnum::Value StructToMyEnum();

template<>
MyEnum::Value StructToMyEnum<A>()
{
   return MyEnum::a;
}

template<>
MyEnum::Value StructToMyEnum<B>()
{
   return MyEnum::b;
}

I basically want to get a directly by calling soemthing like

StructToMyEnum<A>();

This is the best I could come up with, but when I compile I get multiple definition of 'MyEnum::Value StructToMyEnum<A>()' errors when trying to link.

Any recommendations on the best way to map types to enums as per this example?

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;
}

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);
}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM