samedi 18 mars 2017

C++: Redefinition of template class with different types

I have two(multiple) enums:

enum Choices1 : int
{
    a,
    b
};
enum Choices2 : int
{
    c,
    d
};

I want to be able to associate a Type to each of these enum choices. If I only had one set of enums say Choices1, I could introduce:

template <Choices1 c>
struct choice_traits; 

and then specialise it for each entry:

template <>
struct choice_traits<Choices1::a> 
{
    using MyType = float;
};

template <>
struct choice_traits<Choices1::b> 
{
    using MyType = double;

But I want to be able to do it for multiple enums, while using the same keyword, ie something like:

template <>
struct choice_traits<Choices1::a>
{...};

template <>
struct choice_traits<Choices1::b>
{...};

template <>
struct choice_traits<Choices2::c>
{...};

template <>
struct choice_traits<Choices2::d>
{...};

Is this possible? if so, what would be the non specialised case?

If not, is there any alternative way of associating a Type to each of (Choice1::a,Choice1::b,Choice2::c,Choice2::d) and potentially more such enums?

Aucun commentaire:

Enregistrer un commentaire