Since ranges::view_interface
has an explicit operator bool()
function, this makes most C++20 ranges adaptors have the ability to convert to bool
:
https://godbolt.org/z/ccbPrG51c
static_assert(views::iota(0));
static_assert("hello"sv | views::split(' '));
static_assert(views::single(0) | std::views::transform([](auto) { return 0; }));
Although this seems very convenient, do we really need this feature? The reason is that traditional STL containers such as std::vector
, or commonly used views such as std::string_view
, do not have this functionality of converting to bool
, which seems to have some inconsistencies. And it seems more intuitive to just call .empty()
or ranges::empty
directly to determine whether a range is empty.
In addition, this implicit conversion maybe also causes confusion:
static_assert(!views::empty<int>);
So, why does ranges::view_interface
provide this operator bool
function? Are there any practical use cases?
Please note that this may be an opinion-based question, but I want to know the philosophy behind view_interface
providing operator bool
.