This is a gradual improvement in Johannes' first answer. It avoids the struct dummy and avoids copying all input lists (although this does not make much difference if only one list is shorter than the others). I also made it common to all containers.
But this requires a package template index generator, which is quite useful anyway
template< std::size_t n, typename ... acc > struct make_index_tuple { typedef typename make_index_tuple< n - 1, std::integral_constant< std::size_t, n - 1 >, acc ... >::type type; }; template< typename ... acc > struct make_index_tuple< 0, acc ... > { typedef std::tuple< acc ... > type; };
A βrealβ implementation consists of a simple function that requires output from the aforementioned utility and an interface function that maps packets to tuples.
template< typename ret_t, std::size_t ... indexes, typename lst_tuple > ret_t simple_zip( std::tuple< std::integral_constant< std::size_t, indexes > ... >, lst_tuple const &lst ) { ret_t ret; auto iters = std::make_tuple( std::get< indexes >( lst ).begin() ... ); auto ends = std::make_tuple( std::get< indexes >( lst ).end() ... ); while ( std::max< bool >({ std::get< indexes >( iters ) == std::get< indexes >( ends ) ... }) == false ) { ret.emplace_back( * std::get< indexes >( iters ) ++ ... ); } return ret; } template< typename ... T > std::list< std::tuple< typename T::value_type ... > > simple_zip( T const & ... lst ) { return simple_zip < std::list< std::tuple< typename T::value_type ... > > > ( typename make_index_tuple< sizeof ... lst >::type(), std::tie( lst ... ) ); }
At least that puts in perspective what Johannes did. This is how most algorithms with a variational pattern look like, because there is no way to save a variable state of a type without tuple , and there is no way to process a variational tuple without a package of indices or a metarecursive function. (Edit: ah, now Johannes used a recursive local tail function to define local packages to do all this without tuple at all. Amazing ... if you can handle all the functional programming; v).)
Potatoswatter
source share