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?
精彩评论