This may solve your problem:
#include <tuple> //Index from http://stackoverflow.com/a/18063608/3484570 template <class T, class Tuple> struct Index; template <class T, class... Types> struct Index<T, std::tuple<T, Types...>> { static const std::size_t value = 0; }; template <class T, class U, class... Types> struct Index<T, std::tuple<U, Types...>> { static const std::size_t value = 1 + Index<T, std::tuple<Types...>>::value; }; template <class T> constexpr std::size_t type_id() { //need to add every type in this tuple: return Index<T, std::tuple<int, double, char>>::value; } int main() { size_t a = type_id<int>(); switch (a) { case type_id<int>(): break; } }
The good news is that you get a type_id<T>()
, which can be used in the context of constexpr
, for example, in case
, as you like.
The bad news is that you need to list all supported types.
In practice, you can get used to the error message that occurs when you request the type_id
unsupported type and just add it and ultimately add all the relevant types.
source share