c++c++11boost-range

How to encapsulate custom iterator in function using boost-range


Lately I was using boost-range to create ranges over elements satisfying certain criteria. In all cases I'm using the same kind of filtered range all the time, so that I tried to encapsulate this behaviour in an external function.

This was the point where my problems started. Consider the following example.

#include <boost/range/adaptor/filtered.hpp>
#include <iostream>
#include <vector>

auto myFilter = [](const std::vector<int>& v, int r) {
    return v | boost::adaptors::filtered([&r](auto v) { return v%r == 0; });
};

int main(int argc, const char* argv[])
{
    using namespace boost::adaptors;

    std::vector<int> input{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for (auto& element : input | filtered([](auto v) {return v % 2 == 0; } ))
    {
        std::cout << "Element = " << element << std::endl;
    }
    std::cout << std::endl;
    for (auto& element : myFilter(input,4))
    {
        std::cout << "Element = " << element << std::endl;
    }
    return 0;
}

The first for-loop behaves as expected printing 4 and 8. The second for-loop however prints just 4. Why is that?

My second idea was to implement a class having a begin() and end() function. This should be a thin wrapper around a range object.

This was the solution, after fiddling out the type of the range iterator.

struct MyFilter {
    MyFilter(const std::vector<int>& c, int r) : c(c), r(r), f([&r](auto v) { return v%r == 0; }) {
    }

    boost::range_detail::filtered_range<std::function<bool(int)>, std::vector<int>>::iterator begin() {
        return rng.begin();
    }

    boost::range_detail::filtered_range<std::function<bool(int)>, std::vector<int>>::iterator end() {
        return rng.end();
    }

    std::vector<int> c;
    int r;
    std::function<bool(int)> f;
    boost::range_detail::filtered_range < std::function<bool(int)>, std::vector<int>> rng=c | boost::adaptors::filtered(f);
 };

Usage should be something like:

    for (auto& element : MyFilter(input, 4)) {
        std::cout << "Element = " << element << std::endl;
    }

Unfortunately, it prints again just the 4. Whichs is quite strange to me??

Now, I got the solution by myself. I have to remove the "&" in my lambda function to make it work!


Solution

  • In:

    auto myFilter = [](const std::vector<int>& v, int r) {
        return v | boost::adaptors::filtered([&r](auto v) { return v%r == 0; });
    };
    

    It returns another range adaptor while r captured by reference becomes a dangling reference. To fix it capture r by value:

    auto myFilter = [](const std::vector<int>& v, int r) {
        return v | boost::adaptors::filtered([r](auto v) { return v%r == 0; });
    };                                        ^
                                              +--- capture by value