cpp / latest / ranges / view_interface / empty.html /

std::ranges::view_interface<D>::empty

constexpr bool empty() requires ranges::forward_range<D>;
(1) (since C++20)
constexpr bool empty() const requires ranges::forward_range<const D>;
(2) (since C++20)

The default implementation of empty() member function checks whether the beginning iterator and the sentinel compare equal.

1) Let derived be a reference bound to static_cast<D&>(*this). Equivalent to return ranges::begin(derived) == ranges::end(derived);.
2) Same as (1), except that derived is static_cast<const D&>(*this).

Parameters

(none).

Return value

true if the beginning iterator and the sentinel of the object of the derived type compare equal, false otherwise.

Notes

Following derived types may use the default implementation of empty:

Although std::ranges::basic_istream_view inherits from std::ranges::view_interface and does not declare the empty() member function, it cannot use the default implementation, because it never satisfies std::ranges::forward_range.

Example

#include <array>
#include <ranges>
 
int main() {
    constexpr std::array a {0, 1, 2, 3, 4};
    static_assert( ! std::ranges::single_view(a).empty() );
    static_assert(   (a | std::views::take(0)).empty() );
    static_assert( ! (a | std::views::take(2)).empty() );
    static_assert(   (a | std::views::drop(9)).empty() );
    static_assert( ! (a | std::views::drop(2)).empty() );
    static_assert(   std::views::iota(0,0).empty() );
    static_assert( ! std::views::iota(0).empty() );
}

See also

(C++17)
checks whether the container is empty
(function template)
(C++20)
checks whether a range is empty
(customization point object)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/ranges/view_interface/empty