4

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?

2 Answers 2

6

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.

2
  • Oh, so it's all because of lazyness. Thanks, I'll accept asap.
    – Enlico
    Jan 7, 2021 at 17:55
  • 2
    @Enlico Yep. If you don't actually evaluate the filter you can't know the resulting size. Jan 7, 2021 at 18:00
4

You can use boost::size:

boost::size(w2)

but it may be costly in terms of performance.

1
  • 1
    I've accepted the other answer because it addresses the why of the error I got. However this answer adds a lot by adding a possible solution, boost::size, and telling me that it forces the evaluation of the range.
    – Enlico
    Jan 7, 2021 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.