Using std::get and std::tie on boost tuples, zip_iterator, etc

别等时光非礼了梦想. 提交于 2019-12-08 17:32:14

问题


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)) {...}



来源:https://stackoverflow.com/questions/22659287/using-stdget-and-stdtie-on-boost-tuples-zip-iterator-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!