I have a function which "zips" multiple containers for range based foor loop iteration. (taken from a another Stack Overflow post)
template<class... Conts>
auto zip_range(Conts&... conts)
-> decltype(boost::make_iterator_range(boost::make_zip_iterator(boost::make_tuple(conts.begin()...)),
boost::make_zip_iterator(boost::make_tuple(conts.end()...)))) {
return {boost::make_zip_iterator(boost::make_tuple(conts.begin()...)),
boost::make_zip_iterator(boost::make_tuple(conts.end()...))};
}
I can use this function like so:
std::vector<int> x({1,2,3});
std::vector<int> y({4,5,6});
for ( auto const& entries : zip_range(x,y) ) {
std::cout << entries.get<0>() << " " << entries.get<1>() << std::endl;
}
And get expected behavior:
1 4
2 5
3 6
If I try to define a function which takes two std::initizalizer_list
arguments, passes them to a vector, and tries to loop:
template <class T1, class T2>
void foo(const std::initializer_list<T1>& il1,
const std::initializer_list<T2>& il2) {
std::vector<T1> v1(il1);
std::vector<T2> v2(il2);
for ( auto const& zipitr : zip_range(v1,v2) ) {
std::cout << zipitr.get<0>() << " " << zipitr.get<1>() << std::endl;
}
}
I get a compilation error that isn't very helpful:
testing.cpp: In function ‘void foo(const std::initializer_list<_Tp>&, const std::initializer_list<T2>&)’:
testing.cpp:34:32: error: expected primary-expression before ‘)’ token
std::cout << zipitr.get<0>() << " " << zipitr.get<1>() << std::endl;
^
testing.cpp:34:58: error: expected primary-expression before ‘)’ token
std::cout << zipitr.get<0>() << " " << zipitr.get<1>() << std::endl;
Why am I unable to do this? (much less go straight to giving the initializer lists to the function zip_range
)