std::ranges::empty

From cppreference.com
< cpp‎ | ranges
 
 
 
Defined in header <ranges>
inline namespace /*unspecified*/ {

    inline constexpr auto empty = /*unspecified*/;

}
(since C++20)
(customization point object)
Call signature
template< class T >

    requires /* see below */

constexpr bool empty(T&& t);

Determines whether or not t has any elements.

Let t be an object of type T. A call to ranges::empty is expression-equivalent to:

  1. bool(std::forward<T>(t).empty()), if that expression is valid.
  2. Otherwise, (ranges::size(std::forward<T>(t)) == 0), if that expression is valid.
  3. Otherwise, bool(ranges::begin(t) == ranges::end(t))

In all other cases, a call to ranges::empty is ill-formed, which can result in substitution failure when ranges::empty(t) appears in the immediate context of a template instantiation.

Expression-equivalent

Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.

Customization point objects

The name ranges::empty denotes a customization point object, which is a const function object of a literal semiregular class type (denoted, for exposition purposes, as empty_ftor). All instances of empty_ftor are equal. Thus, ranges::empty can be copied freely and its copies can be used interchangeably.

Given a set of types Args..., if std::declval<Args>()... meet the requirements for arguments to ranges::empty above, empty_ftor will satisfy std::invocable<const empty_ftor&, Args...>. Otherwise, no function call operator of empty_ftor participates in overload resolution.

Example

#include <iostream>
#include <ranges>
#include <vector>
 
template <std::ranges::input_range R>
void print(R&& r)
{
    if (std::ranges::empty(r)) {
        std::cout << "\tEmpty\n";
        return;
    }
 
    std::cout << "\tElements:";
    for (const auto& element : r) {
        std::cout << ' ' << element;
    }
 
    std::cout << '\n';
}
 
int main()
{
    {
        auto v = std::vector<int>{1, 2, 3};
        std::cout << "1. calling ranges::empty on std::vector:\n";
        print(v);
 
        v.clear();
        print(v);
    }
    {
        std::cout << "2. calling ranges::empty on std::initializer_list:\n";
        auto il = {7, 8, 9};
        print(il);
 
        print(std::initializer_list<int>{});
    }
    {
        std::cout << "2. calling ranges::empty on a raw array:\n";
        int array[] = {4, 5, 6}; // array has a known bound
        print(array);
    }
    {
        struct NoEmptyNorSize : private std::vector<int> {
            auto begin() { return std::vector<int>::begin(); }
            auto end() { return std::vector<int>::end(); }
        };
 
        std::cout << "3. calling ranges::empty on an object that satisfies only case 3):\n";
        print(NoEmptyNorSize{});
    }
}

Output:

1. calling ranges::empty on std::vector:
	Elements: 1 2 3
	Empty
2. calling ranges::empty on std::initializer_list:
	Elements: 7 8 9
	Empty
2. calling ranges::empty on a raw array:
	Elements: 4 5 6
3. calling ranges::empty on an object that satisfies only case 3):
	Empty

See also

(C++17)
checks whether the container is empty
(function template)