C++ creating template specialisation in a typelist -
i have following struct:
template<typename tag_type> struct tag{ typedef tag_type type; enum { value = -1 } };
i use class typeid identify specific classes @ runtime. each of these classes needs listed in typelist , each tag specialization needs have different value.
is there way make value
equal index of specialization in list.
my goal make maintenance of list of specialized tag
unique values easy possible... (i need make sure each type in list has unique value or part of system crash)
edit: failed mention use values @ compile time...
a non c++11 implementation of think mean, though not specify structure of typelist.
template <typename h, typename t> struct typelist { typedef h head; typedef t tail; }; template <typename t, typename list, int n> struct typelist_index_i { typedef typename list::tail tail; enum { value = n + typelist_index_i<t, tail, n + 1>::value }; }; template <typename list, int n> struct typelist_index_i<typename list::tail, list, n> { enum { value = n + 1 }; }; template <typename list, int n> struct typelist_index_i<typename list::head, list, n> { enum { value = n }; }; template <typename t, typename list> struct typelist_index { enum { value = typelist_index_i<t, list, 0>::value }; }; class {}; class b {}; class c {}; typedef typelist<a, typelist<b, c> > the_types; template<typename tag_type> struct tag{ typedef tag_type type; enum { value = typelist_index<tag_type, the_types>::value }; }; int main() { std::cout << tag<a>::value << std::endl; // 0 std::cout << tag<b>::value << std::endl; // 1 std::cout << tag<c>::value << std::endl; // 2 system("pause"); return 0; }
Comments
Post a Comment