You can use a class template and partial specializations to do what you want. (Note that std::tuple_element
does almost the same like the other answer says):
#include <tuple>
#include <type_traits>
template <int N, typename... Ts>
struct get;
template <int N, typename T, typename... Ts>
struct get<N, std::tuple<T, Ts...>>
{
using type = typename get<N - 1, std::tuple<Ts...>>::type;
};
template <typename T, typename... Ts>
struct get<0, std::tuple<T, Ts...>>
{
using type = T;
};
int main()
{
using var = std::tuple<int, bool, std::string>;
using type = get<2, var>::type;
static_assert(std::is_same<type, std::string>::value, ""); // works
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…