The following code compiles, runs, and gives the result as if range
is being converted to bool
via range.empty()
. Is it actually what happens? I'm really not comfortable enough in navigating Range-v3 headers, so I'm asking this question here.
#include <iostream>
#include <range/v3/view/take.hpp>
#include <range/v3/view/drop.hpp>
#include <vector>
int main() {
std::vector<int> v{1};
if (auto range = v | ranges::views::take(1)) {
std::cout << "take" << std::endl;
}
if (auto range = v | ranges::views::drop(1)) {
std::cout << "drop" << std::endl;
}
}
The only operations required for a type to fit the
std::ranges::range
concept (or its Range-v3 equivalent) is being able to performstd::ranges::begin
/end
on it (or its Range-v3 equivalent). But this does not mean that a range type only has those operations.A type that fits a concept can implement anything it wants on top of that. So while the
range
concept does not specify the functionality you're seeing, it would be valid for any particular type that satisfiesrange
to provide it.So the question you're really asking is whether these particular range view types provide such an operator. The answer is yes, according to the documentation. The
view_interface
type provides anexplicit operator bool
if the underlying range is a type for which empty testing is allowed. And for what it's worth, so does the C++20 equivalent.