Monday, November 27, 2017

C++11: std::tuple_cat()

C++11 added the template function std::tuple_cat() that concatenates all tuples passed to it into a single tuple. Here is an example:

#include <iostream>
#include <tuple>

int main()
{
  auto myTuple1   = std::make_tuple(1, 2);
  auto myTuple2   = std::make_tuple(3, 4);
  auto myTuple3   = std::make_tuple(5, 6);
  auto myTupleAll = std::tuple_cat(myTuple1, myTuple2, myTuple3);

  std::cout << std::get<0>(myTupleAll) << " "; // 1
  std::cout << std::get<1>(myTupleAll) << " "; // 2
  std::cout << std::get<2>(myTupleAll) << " "; // 3
  std::cout << std::get<3>(myTupleAll) << " "; // 4
  std::cout << std::get<4>(myTupleAll) << " "; // 5
  std::cout << std::get<5>(myTupleAll) << " "; // 6
  std::cout << std::endl;
  return 0;
}
// Output" 1 2 3 4 5 6
Reference: http://en.cppreference.com/w/cpp/utility/tuple/tuple_cat

No comments:

Post a Comment