fork(1) download
  1. #include <tuple>
  2. #include <iostream>
  3. #include <type_traits>
  4.  
  5. template <size_t n, typename... T>
  6. typename std::enable_if<(n >= sizeof...(T))>::type
  7. print_tuple(std::ostream&, const std::tuple<T...>&)
  8. {}
  9.  
  10. template <size_t n, typename... T>
  11. typename std::enable_if<(n < sizeof...(T))>::type
  12. print_tuple(std::ostream& os, const std::tuple<T...>& tup)
  13. {
  14. if (n != 0)
  15. os << ", ";
  16. os << std::get<n>(tup);
  17. print_tuple<n+1>(os, tup);
  18. }
  19.  
  20. template <typename... T>
  21. std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& tup)
  22. {
  23. os << "[";
  24. print_tuple<0>(os, tup);
  25. return os << "]";
  26. }
  27.  
  28. int main()
  29. {
  30. std::cout << std::make_tuple(2, 3.14159F, 2345.678) << std::endl;
  31. std::cout << std::make_tuple("hello", 5) << std::endl;
  32. std::cout << std::make_tuple() << std::endl;
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
[2, 3.14159, 2345.68]
[hello, 5]
[]