Search code examples
c++boostfunctional-programmingc++14monads

Why output of boost::adaptors::filtered has no member named size?


The following dummy program compiles and runs

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <functional>
#include <utility>
#include <vector>

using boost::adaptors::filtered;
using boost::adaptors::transformed;
auto whatever = [](auto&& x){ return std::forward<decltype(x)>(x); };
auto whenever = [](auto&){ return true; };

int main() {
    std::vector<int> v{1,2,3};
    auto w1 = v | transformed(whatever);
    auto w2 = v | transformed(whatever) | filtered(whenever);
    w1.size();
    //w2.size();
}

uncommenting the commented line and attempting a compilation with g++ -std=c++14 that_file.cpp causes this error:

uffa.cpp: In function ‘int main()’:
uffa.cpp:17:8: error: ‘struct
boost::range_detail::filtered_range<<lambda(auto:2&)>, const
boost::range_detail::transformed_range<<lambda(auto:1&&)>, std::vector<int> >
>’ has no member named ‘size’
   17 |     w2.size();
      |        ^~~~

Since filtered, just like transformed, takes a range and returns a range, I don't understand why size is not available on filtered's output.

I know that transformed and filtered are two different mathematical functions (e.g. the former assumes that its input is a functor, whereas the latter assumes, correct me if I'm wrong, that its input is a monad), but still... here the input is a std::vector so what's wrong with asking the size of the output of filtered?


Solution

  • When you transform a range, the size doesn't change. That means transform can known what the size will be, as it's the same as the input.

    When you filter a range, you may or may not be removing elements from the range. This is done lazily, so you can't know until you go through the filtered range how large it will be. If you do a non-lazy filter you can know, but ranges are supposed to be lazy.