How do I switch/select types during compile-time?

This should work:

template<std::size_t N, typename... T>
using static_switch = typename std::tuple_element<N, std::tuple<T...> >::type;

Another method:

template<std::size_t N, typename T, typename... Ts>
struct static_switch {
    using type = typename static_switch<N - 1, Ts...>::type;
};
template<typename T, typename... Ts>
struct static_switch<0, T, Ts...> {
    using type = T;
};

Leave a Comment