fork download
  1.  
  2. #include <iostream>
  3. #include <tuple>
  4. #include <string>
  5.  
  6. namespace detail
  7. {
  8. template<int ... N>
  9. struct seq
  10. {
  11. using type = seq<N...>;
  12. template<int I>
  13. struct push_back : seq<N..., I> {};
  14. };
  15.  
  16. template<int N>
  17. struct genseq : genseq<N-1>::type::template push_back<N-1> {};
  18.  
  19. template<>
  20. struct genseq<0> : seq<> {};
  21.  
  22. template<typename ... Types, int ...N>
  23. void print(std::ostream & out, std::tuple<Types...> const & t, seq<N...>)
  24. {
  25. auto sink = {
  26. (out << "{", 0),
  27. (out << (N?",":"") << std::get<N>(t) , 0)...,
  28. (out << "}", 0)
  29. };
  30. }
  31. }
  32. template<typename ... Types>
  33. std::ostream& operator<<(std::ostream & out, std::tuple<Types...> const & t)
  34. {
  35. detail::print(out, t, typename detail::genseq<sizeof...(Types)>::type());
  36. return out;
  37. }
  38.  
  39. int main()
  40. {
  41. std::cout << std::make_tuple(10, 20.0, std::string("Nawaz")) << std::endl;
  42. std::cout << std::make_tuple(10, 20.0, std::string("Nawaz"), 9089) << std::endl;
  43. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
{10,20,Nawaz}
{10,20,Nawaz,9089}