dimanche 25 avril 2021

How can I associate a type with a number

I want to be able to generate a number for each type of a particular typelist.

i.e. for the following 2 typelists (where Group works like std::variant):

class myclass;

using GroupA = Group<int, double, myclass>;

static_assert( GroupA::type_number<int> == 0 );
static_assert( GroupA::type_number<double> == 1 );
static_assert( GroupA::type_number<myclass> == 2 );


using GroupB = Group<double, myclass>;  // different group => different numbering

static_assert( GroupB::type_number<double> == 0 );
static_assert( GroupB::type_number<myclass> == 1 );

I tried to recurse through the Types, but I cannot end up with one template parameter on every type

template < typename ... Types> 
struct Group
{
    
    template < typename T > 
    constexpr static int type_number = 0;
    
    template < typename T, typename ... Types >
    constexpr static int type_number = type_number<Types...> + 1;

};

I am imitating any/variant and I want to be able to sort a vector of them.

Aucun commentaire:

Enregistrer un commentaire