You can do this with a list of types:
template <typename... Ts> struct TypeList; template <> struct TypeList<> { static const int size = 0; static std::integral_constant<int, -1> indexOf(...); }; template <typename Head, typename... Tail> struct TypeList<Head, Tail...> : TypeList<Tail...> { static const int size = sizeof...(Tail) + 1; static std::integral_constant<int, sizeof...(Tail)> indexOf(Head&&); using TypeList<Tail...>::indexOf; }; template <typename TypeList, typename T> using IndexOf = std::integral_constant<int, TypeList::size - decltype(TypeList::indexOf(std::declval<T>()))::value - 1>;
If T not in the List , then IndexOf<List, T>::value is -1 . You can make this case be a compilation error by removing the ellipsis from the TypeList<>::indexOf(...) signature.
Using:
struct Type00 { }; struct Type01 { }; struct Type02 { }; struct Type03 { }; struct TypeXX { }; struct TypeYY { }; using MyTypeList = TypeList< Type00, Type01, Type02, Type03, TypeXX, TypeYY >; int main() { std::cout << IndexOf<MyTypeList, Type00>::value << IndexOf<MyTypeList, Type01>::value << IndexOf<MyTypeList, Type02>::value << IndexOf<MyTypeList, Type03>::value << IndexOf<MyTypeList, TypeXX>::value << IndexOf<MyTypeList, TypeYY>::value; }
Demo
source share