std::ranges...operatorbool(3)
π NAME π
std::ranges::view_interface::operatorbool β std::ranges::view_interface::operatorbool
okay so this is the C++ Standard Libary manual page (yes, the header spells it βLibaryβ, and weβre keeping that energy) for the std::ranges::view_interface::operator bool situation. Itβs the default little member that lets a view answer a contextual bool question about being non-empty, bestie. π
π¦ Synopsis π¦
bestie, the signatures are literally these, and both are (since C++20):
constexpr explicit operator bool() requires /* see below */;
(1) (since C++20)
constexpr explicit operator bool() const requires /* see below */;
(2) (since C++20)
The default implementation of the operator bool member function checks whether the view is non-empty. It makes the derived type contextually convertible to bool. No cap. β¨
- Let
derivedbestatic_cast<D&>(*this). The expression in the requires-clause is equal torequires { ranges::empty(derived); }, and the function body is equivalent toreturn !ranges::empty(derived);. - Same as (1), except that
derivedisstatic_cast<const D&>(*this).
π Parameters π
(none) β literally no parameters, sheβs lowkey simple like that. π
π Return value π
false if the value of the derived type is empty (determined by std::ranges::empty), true otherwise. periodt. π
π§ Notes π§
In C++20, no type derived from std::ranges::view_interface in the standard library provides their own operator bool. Almost all of these types use the default implementation.
A notable exception is std::ranges::basic_istream_view. For its iterator type never satisfies forward_iterator, the view cannot use the inherited operator bool.
bestie, that exception is important:std::ranges::basic_istream_viewcannot use the inheritedoperator boolβ the note says its iterator type never satisfiesforward_iterator. π«Ά
π₯ Example π₯
// Run this code β bestie, go off:
#include <array>
#include <iostream>
#include <ranges>
int main()
{
const std::array ints {0, 1, 2, 3, 4};
auto odds = ints | std::views::filter([](int i) { return 0 != i %
2; });
auto negs = ints | std::views::filter([](int i) { return i < 0; });
std::cout << std::boolalpha
<< "Has odd numbers: " << (!!odds) << ' ' << '\n'
<< "Has negative numbers: " << (!!negs) << ' ' << '\n';
}
πΈ Output πΈ
Has odd numbers: true
Has negative numbers: false
π See also π
ranges::emptyβ checks whether a range is empty. (C++20) (customization point object)emptyβ returns whether the derived view is empty. Provided if it satisfiessized_rangeorforward_range. (C++20) (public member function)emptyβ checks whether the container is empty. (C++17) (function template)
xoxo, your C++ Standard Libary bestie π β source: http://cppreference.com; version/date: 2024.06.10; page: std::ranges...operatorbool(3).
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms