Using std::get and std::tie on boost tuples, zip_i

2019-06-17 19:10发布

问题:

What are my options for using std::get<>() and std::tie<>() together with boost constructs?

Example: I want to use range-based for-loop for iteration over several containers. I can implement zip function, which uses boost::zip_iterator.

 #include <boost/iterator/zip_iterator.hpp>
 #include <boost/range.hpp>

 template <typename... TContainer>
 auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>> {
    auto zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
    auto zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
    return boost::make_iterator_range(zip_begin, zip_end);
 }

Now I can use it like this:

std:list<int> a;
std::vector<double> b;
...
for (auto t : zip(a, b)) {
   // access elements by boost::get<0>(t), boost::get<1>(t)
   // or use boost::tie(a_element, b_element)
}

Problems occur when I call some other method, which expects std::tuple or std::pair - I have to convert), since rest of the code uses std::tuples, or when templated code uses std::get<>() and/or std::tie().

I found some patches which add std::tuple support for zip_iterator, but these are not applied in my version (I use Boost 1.54).

Am I missing something? What are my options to either force zip_iterator to return std::tuple or make std::get, std::tie, etc. available for boost types?

回答1:

Have you tried

#include <boost/iterator/zip_iterator.hpp>
#include <boost/range.hpp>
#include <tuple>

template <typename... TContainer> auto zip(TContainer&... containers) ->
boost::iterator_range<boost::zip_iterator<decltype(std::make_tuple(std::begin(containers)...))>> {
                                                   ^^^
    auto zip_begin = boost::make_zip_iterator(std::make_tuple(std::begin(containers)...));
                                              ^^^
    auto zip_end = boost::make_zip_iterator(std::make_tuple(std::end(containers)...));
                                            ^^^
    return boost::make_iterator_range(zip_begin, zip_end);
 }


回答2:

use boost::combine in boost >= 1.55.

for (auto a_tuple: boost::combine(va, vb)) {...} or

BOOST_FOREACH(boost::tie(elem_a, elem_b), boost::combine(va, vb)) {...}