fork download
  1.  
  2. #include <ostream>
  3. #include <iostream>
  4. #include <string>
  5. #include <tuple>
  6. #include <typeinfo>
  7. #include <cstddef>
  8. using namespace std;
  9.  
  10. template<class Ch, class Tr, class Tuple, std::size_t... Is>
  11. void print_tuple_impl(std::basic_ostream<Ch, Tr>& os,
  12. const Tuple & t,
  13. std::index_sequence<Is...>)
  14. {
  15. using swallow = int[]; // guaranties left to right order
  16. (void)swallow {
  17. 0, (void(os << (Is == 0 ? "" : ", ") << std::get<Is>(t)), 0)...
  18. };
  19. }
  20.  
  21. template<class Ch, class Tr, class... Args>
  22. auto operator<<(std::basic_ostream<Ch, Tr>& os, const std::tuple<Args...>& t)
  23. -> std::basic_ostream<Ch, Tr>&
  24. {
  25. os << "(";
  26. print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
  27. return os << ")";
  28. }
  29.  
  30.  
  31.  
  32. template< class Tuple, std::size_t... Is>
  33. auto tupleCombiner(int firstVal,const Tuple& t, std::index_sequence<Is...>)
  34. {
  35. tuple<int, char, int, char> combinedTuple{ firstVal, std::get<Is>(t)... };
  36. return combinedTuple;
  37. }
  38.  
  39. template< class... Args>
  40. auto tupleCombine(int firstVal,const std::tuple<Args...>& t)
  41. {
  42. return tupleCombiner(firstVal,t,std::index_sequence_for<Args...>{});
  43. }
  44.  
  45. int main()
  46. {
  47. auto combinedTuples= tupleCombine(1,make_tuple('A', 2, 'B'));
  48.  
  49. std::cout << combinedTuples;
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
(1, A, 2, B)