fork(5) download
  1. #include <iostream>
  2. #include <string>
  3. #include <tuple>
  4.  
  5. template<typename T, T...>
  6. struct integer_sequence { };
  7.  
  8. template<std::size_t N, std::size_t... I>
  9. struct gen_indices : gen_indices<(N - 1), (N - 1), I...> { };
  10. template<std::size_t... I>
  11. struct gen_indices<0, I...> : integer_sequence<std::size_t, I...> { };
  12.  
  13. template<typename H>
  14. std::string& to_string_impl(std::string& s, H&& h)
  15. {
  16. using std::to_string;
  17. s += to_string(std::forward<H>(h));
  18. return s;
  19. }
  20.  
  21. template<typename H, typename... T>
  22. std::string& to_string_impl(std::string& s, H&& h, T&&... t)
  23. {
  24. using std::to_string;
  25. s += to_string(std::forward<H>(h));
  26. return to_string_impl(s, std::forward<T>(t)...);
  27. }
  28.  
  29. template<typename... T, std::size_t... I>
  30. std::string to_string(const std::tuple<T...>& tup, integer_sequence<std::size_t, I...>)
  31. {
  32. std::string result;
  33. int ctx[] = { (to_string_impl(result, std::get<I>(tup)...), 0), 0 };
  34. (void)ctx;
  35. return result;
  36. }
  37.  
  38. template<typename... T>
  39. std::string to_string(const std::tuple<T...>& tup)
  40. {
  41. return to_string(tup, gen_indices<sizeof...(T)>{});
  42. }
  43.  
  44. int main(int argc, char** argv)
  45. {
  46. std::tuple<int, double, float> tup(1, 2.1, 3.2);
  47. std::cout << to_string(tup) << std::endl;
  48. }
  49.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
12.1000003.200000