ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.13k stars 439 forks source link

question: Distinction between factories and adaptors? Clarifying all_view / views::all #1394

Open thehans opened 4 years ago

thehans commented 4 years ago

I've just started reading up and trying to learn about ranges, and I guess I'm confused about what makes a view a factory vs an adaptor.

My basic understanding is that adaptors can be composed from other ranges via pipe operator.

Whereas factories act more like the beginning or initial input to such a composition, and don't need input from another range.

So to me, it seems like all_view makes sense as a range factory which creates a range from a container, as this example shows:

std::vector<int> v{0,1,2,3,4,5};
for(int n : ranges::views::all(v) | ranges::views::take(2) ) {
    std::cout << n << ' ';
}

But as an adaptor it seems to be a completely superfluous noop?

for(int n : ranges::views::iota(0) | ranges::views::all | ranges::views::take(2) ) {
    std::cout << n << ' ';
}
JohelEGP commented 4 years ago

Note that I don't think there's an all_view in ranges-v3, but there is all_t. The standard will follow suit given http://wg21.link/LWG3335.

views::all is used to make an initial, viewable_range that's not a view into a view. For example, given std::vector<int> x{1, 2, 3};, views::take(x, 3) will pass x through views::all(x) to make into a view. In this case, it is not a noop.

thehans commented 4 years ago

OK, I had been looking at the docs from cppreference descriptions, which I guess are (understandably) not necessarily consistent with the most recent drafts?