How to limit boost::combine to the minimum of two ranges

Viewed 59

Finding the following as the source of a segfault just cost me about 4h of work:

#include <boost/range/combine.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <vector>
#include <list>

int main(int, const char*[])
{
    std::vector<int> v;
    std::list<char> l;
    for (int i = 0; i < 5; ++i)
    {
        v.push_back(i);
        l.push_back(static_cast<char>(i) + 'a');
    }

    v.push_back(5);

    int ti;
    char tc;
    BOOST_FOREACH(boost::tie(ti, tc), boost::combine(v, l))
    {
        std::cout << '(' << ti << ',' << tc << ')' << '\n';
    }

    return 0;
}

If you execute this example, you will note that combine happily iterates as long as the longer range has values. I do not see this in the documentation.

Is there a way to limit the iteration to the shorter of the two ranges?

0 Answers
Related