std::ranges...face::empty(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::ranges::view_interface::empty πŸ’–

okay bestie, this is the C++ Standard Libary manual page for std::ranges::view_interface::empty, also living in the header as std::ranges...face::empty(3) β€” we folded the manual name in right here instead of copying the whole header line, because we're efficient like that πŸ’…βœ¨


πŸ’… NAME πŸ’…

std::ranges::view_interface::empty β€” yes, it's literally also std::ranges::view_interface::empty, that's the whole name, we're not hiding anything 🫢


🌸 Synopsis 🌸

Here's the signature situation, copied exactly so nobody comes for us:

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

The default implementation of the empty() member function checks whether the object of the derived type's size is 0 (if valid), or whether the beginning iterator and the sentinel compare equal. That's the whole vibe check πŸ”₯

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

πŸŽ€ Parameters πŸŽ€

Parameters: (none)

literally none, bestie β€” it's a no-argument situation πŸ™ˆ


✨ Return value ✨

true if the size of the object of the derived type is 0 (if D satisfies std::ranges::sized_range), or its beginning iterator and the sentinel compare equal; false otherwise.

periodt. that's the return value πŸ’–


πŸ¦‹ Notes πŸ¦‹

Following derived types may use the default implementation of empty:

  • std::ranges::common_view
  • std::ranges::drop_view
  • std::ranges::drop_while_view
  • std::ranges::elements_view
  • std::ranges::filter_view
  • std::ranges::join_view
  • std::ranges::lazy_split_view
  • std::ranges::reverse_view
  • std::ranges::single_view
  • std::ranges::split_view
  • std::ranges::take_view
  • std::ranges::take_while_view
  • std::ranges::transform_view

and also this whole crew:

  • std::ranges::adjacent_transform_view
  • std::ranges::adjacent_view
  • std::ranges::as_const_view
  • std::ranges::as_rvalue_view
  • std::ranges::cartesian_product_view
  • std::ranges::chunk_view
  • std::ranges::chunk_by_view (since C++23)
  • std::ranges::join_with_view
  • std::ranges::repeat_view
  • std::ranges::slide_view
  • std::ranges::stride_view
  • std::ranges::zip_view
  • std::ranges::zip_transform_view

Small plot twist: 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 satisfies neither std::ranges::sized_range nor std::ranges::forward_range.

not me pointing at the one view that can't sit with us πŸ˜­πŸ’€


🌈 Example 🌈

run this code, it's giving static_assert flex πŸ’…

// Run this code

#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(5)).empty());
    static_assert((a | std::views::drop(5)).empty());
    static_assert(!(a | std::views::drop(3)).empty());
    static_assert(std::views::iota(0,0).empty());
    static_assert(!std::views::iota(0).empty());
}

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 3715
    • Applied to: C++20
    • Behavior as published: empty() only supported forward_range types
    • Correct behavior: sized_range-only types are also supported

glow-up applied retroactively, we love to see it ⭐


πŸ‘€ See also πŸ‘€

  • empty (C++17) β€” checks whether the container is empty (function template)
  • ranges::empty (C++20) β€” checks whether a range is empty (customization point object)

iykyk, these are the related besties 🫢


made with πŸ’– by the cppreference.com girlies β€” source/version: http://cppreference.com, date: 2024.06.10, footer page name: std::ranges...face::empty(3). slay and goodbye πŸŽ€βœ¨

πŸ“¦ where this page comes from