Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

c++ - 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?

question from:https://stackoverflow.com/questions/65617373/why-output-of-boostadaptorsfiltered-has-no-member-named-size

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...