fork(2) download
  1. #include<tuple>
  2. #include<type_traits>
  3. #include<string>
  4. #include<iostream>
  5.  
  6. template<int Index, class Search, class First, class... Types>
  7. struct get_internal
  8. {
  9. typedef typename get_internal<Index + 1, Search, Types...>::type type;
  10. static constexpr int index = Index;
  11. };
  12.  
  13. template<int Index, class Search, class... Types>
  14. struct get_internal<Index, Search, Search, Types...>
  15. {
  16. typedef get_internal type;
  17. static constexpr int index = Index;
  18. };
  19.  
  20. template<class T, class... Types>
  21. T get(std::tuple<Types...> tuple)
  22. {
  23. return std::get<get_internal<0,T,Types...>::type::index>(tuple);
  24. }
  25.  
  26. int main()
  27. {
  28. std::tuple<int, double, std::string> test{1, 1.7, "test"};
  29. std::cout<<"get<0> == get<int> :"<< (std::get<0>(test) == get<int>(test))<< "\n";
  30. std::cout<<"get<1> == get<double> :"<<(std::get<1>(test) == get<double>(test))<< "\n";
  31. std::cout<<"get<2> == get<std::string> :"<<(std::get<2>(test) == get<std::string>(test))<< "\n";
  32. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
get<0> == get<int> :1
get<1> == get<double> :1
get<2> == get<std::string> :1