fork(1) download
  1. #include <iostream>
  2. #include <tuple>
  3. #include <cassert>
  4.  
  5. template<std::size_t ...Indices>
  6. struct index_tuple{
  7. };
  8. template<typename Left,typename Right>
  9. struct concat_index_tuple;
  10. template<std::size_t ...Lefts,std::size_t ...Rights>
  11. struct concat_index_tuple<index_tuple<Lefts...>,index_tuple<Rights...>>{
  12. using type = index_tuple<Lefts...,Rights...>;
  13. };
  14. template<std::size_t Beg,std::size_t End>
  15. struct make_index_tuple{
  16. using type = typename concat_index_tuple<
  17. index_tuple<Beg>,
  18. typename make_index_tuple<Beg + 1,End>::type
  19. >::type;
  20. };
  21. template<std::size_t End>
  22. struct make_index_tuple<End,End>{
  23. using type = index_tuple<End>;
  24. };
  25. template<typename Tuple>
  26. struct tuple_index;
  27. template<typename ...Ts>
  28. struct tuple_index<std::tuple<Ts...>>{
  29. using type = typename make_index_tuple<0,sizeof...(Ts) - 1>::type;
  30. };
  31.  
  32. template<typename T>
  33. void output_impl(std::size_t s,T const& x){
  34. assert(s == 0);
  35. std::cout << x << std::endl;
  36. }
  37. template<typename T,typename ...Us>
  38. void output_impl(std::size_t s,T const& x,Us const& ...xs){
  39. if(s){
  40. output_impl(s - 1,xs...);
  41. }
  42. else{
  43. std::cout << x << std::endl;
  44. }
  45. }
  46. template<std::size_t ...Indices,typename ...Types>
  47. void output_unpack(std::size_t s,std::tuple<Types...> const& t,index_tuple<Indices...>){
  48. output_impl(s,std::get<Indices>(t)...);
  49. }
  50. template<typename Tuple>
  51. void output(std::size_t s,Tuple const& t){
  52. output_unpack(s,t,typename tuple_index<Tuple>::type{});
  53. }
  54.  
  55. int main() {
  56. int i;
  57. std::cout << "Enter index" << std::endl;
  58. std::cin >> i;
  59. output(i,std::make_tuple("a",42,3.14f,&i));
  60. return 0;
  61. }
Success #stdin #stdout 0s 3344KB
stdin
3
stdout
Enter index
0xbfa32e9c