fork download
  1. #include <iostream>
  2. #include <tuple>
  3. using namespace std;
  4.  
  5. template<typename T, typename R = conditional_t<is_same<T, char const*>::value, string, T>>
  6. R bar (T &&value) {return value;}
  7.  
  8. template<class Ch, class Tr, class Tuple, std::size_t... Is>
  9. void print_tuple_impl(std::basic_ostream<Ch,Tr>& os,
  10. const Tuple & t,
  11. std::index_sequence<Is...>)
  12. {
  13. using swallow = int[]; // guaranties left to right order
  14. (void)swallow{0, (void(os << (Is == 0? "" : ", ") << std::get<Is>(t)), 0)...};
  15. }
  16.  
  17. template<class Ch, class Tr, class... Args>
  18. decltype(auto) operator<<(std::basic_ostream<Ch, Tr>& os,
  19. const std::tuple<Args...>& t)
  20. {
  21. os << "(";
  22. print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
  23. return os << ")";
  24. }
  25.  
  26. template<typename...Args>
  27. decltype(auto) foo(Args...args)
  28. {
  29. return [args = make_tuple(bar(args)...)] () { cout<< args; return; };
  30. }
  31.  
  32. int main() {
  33. // your code goes here
  34. string *s = new string("Hello, World!");
  35. const char *p = s->c_str();
  36. auto f = foo(1, p, 3.14);
  37. delete s;
  38. f();
  39. return 0;
  40. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
(1, Hello, World!, 3.14)