std::ranges...face::empty(3)
π 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 π₯
- Let
derivedbe a reference bound tostatic_cast<D&>(*this). Equivalent toreturn ranges::size(derived) == 0;ifDsatisfiessized_range. Otherwise, equivalent toreturn ranges::begin(derived) == ranges::end(derived);. - Same as (1), except that
derivedisstatic_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_viewstd::ranges::drop_viewstd::ranges::drop_while_viewstd::ranges::elements_viewstd::ranges::filter_viewstd::ranges::join_viewstd::ranges::lazy_split_viewstd::ranges::reverse_viewstd::ranges::single_viewstd::ranges::split_viewstd::ranges::take_viewstd::ranges::take_while_viewstd::ranges::transform_view
and also this whole crew:
std::ranges::adjacent_transform_viewstd::ranges::adjacent_viewstd::ranges::as_const_viewstd::ranges::as_rvalue_viewstd::ranges::cartesian_product_viewstd::ranges::chunk_viewstd::ranges::chunk_by_view(since C++23)std::ranges::join_with_viewstd::ranges::repeat_viewstd::ranges::slide_viewstd::ranges::stride_viewstd::ranges::zip_viewstd::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 supportedforward_rangetypes - 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
- 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