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.
constexprtype_number. Just conduct a simple thought experiment yourself. Put two of these types in different header files. No translation unit#includes both of them. Or they are#included in different order. How do you expect to achieve a consistenttype_numberfor each one of the two types?std::type_indexmight be the best tool heretype_infodoes not guarantee that, does it?get_type_number()function. I will sort it using this type_number first. If they are the same type, I can then compare them the normal way.