std::empty(3)

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

🌸 std::empty(3) β€” C++ Standard Library 🌸

✨ NAME ✨

std::empty β€” std::empty (the self-titled icon of emptiness checks, periodt πŸ’…)

πŸ¦‹ Synopsis πŸ¦‹

Okay bestie, this one is defined in a whole squad of headers. Like, it's everywhere:

  • Defined in header <array>
  • Defined in header <deque>
  • Defined in header <flat_map>
  • Defined in header <flat_set>
  • Defined in header <forward_list>
  • Defined in header <iterator>
  • Defined in header <list>
  • Defined in header <map>
  • Defined in header <regex>
  • Defined in header <set>
  • Defined in header <span>
  • Defined in header <string>
  • Defined in header <string_view>
  • Defined in header <unordered_map>
  • Defined in header <unordered_set>
  • Defined in header <vector>

πŸ’œ Overload (1) β€” container/view edition πŸ’œ

template< class C >
constexpr auto empty( const C& c ) -> decltype(c.empty());

(since C++17) (until C++20)

template< class C >
[[nodiscard]] constexpr auto empty( const C& c ) -> decltype(c.empty());

(since C++20)

🧚 Overload (2) β€” array time 🧚

template< class T, std::size_t N >
constexpr bool empty( const T (&array)[N] ) noexcept;

(since C++17) (until C++20)

template< class T, std::size_t N >
[[nodiscard]] constexpr bool empty( const T (&array)[N] ) noexcept;

(since C++20)

πŸŽ€ Overload (3) β€” initializer_list girlie πŸŽ€

template< class E >
constexpr bool empty( std::initializer_list<E> il ) noexcept;

(since C++17) (until C++20)

template< class E >
[[nodiscard]] constexpr bool empty( std::initializer_list<E> il ) noexcept;

(since C++20)

Returns whether the given range is empty. No cap, that's the whole vibe check.

  1. Returns c.empty().
  2. Returns false.
  3. Returns il.size() == 0.

🌈 Parameters 🌈

  • c β€” a container or view with an empty member function
  • array β€” an array of arbitrary type
  • il β€” an std::initializer_list

⭐ Return value ⭐

  1. c.empty()
  2. false
  3. il.size() == 0

πŸ’€ Exceptions πŸ’€

  1. May throw implementation-defined exceptions.
Okay but like, bestie, only overload (1) has the Exception Momentβ„’ here. The other two are just out here living their best noexcept lives. πŸ™ˆ

🫢 Notes 🫢

The overload for std::initializer_list is necessary because it does not have a member function empty.

Feature-test macro:

  • Feature-test macro: __cpp_lib_nonmember_container_access
  • Value: 201411L
  • Std: (C++17)
  • Feature: std::size(), std::data(), and std::empty()

πŸ‘‘ Possible implementation πŸ‘‘

First version

template<class C>
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty())
{
    return c.empty();
}

Second version

template<class T, std::size_t N>
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept
{
    return false;
}

Third version

template<class E>
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept
{
    return il.size() == 0;
}

πŸ”₯ Example πŸ”₯

// Run this code

#include <iostream>
#include <vector>

template<class T>
void print(const T& container)
{
    if (std::empty(container))
        std::cout << "Empty\n";
    else
    {
        std::cout << "Elements:";
        for (const auto& element : container)
            std::cout << ' ' << element;
        std::cout << '\n';
    }
}

int main()
{
    std::vector<int> c = {1, 2, 3};
    print(c);
    c.clear();
    print(c);

    int array[] = {4, 5, 6};
    print(array);

    auto il = {7, 8, 9};
    print(il);
}

✨ Output ✨

Elements: 1 2 3
Empty
Elements: 4 5 6
Elements: 7 8 9

🌸 See also 🌸

  • ranges::empty β€” checks whether a range is empty. (C++20) (customization point object)

Cute sign-off bestie: this page is from cppreference.com, dated 2024.06.10, and it's literally the std::empty(3) manual page. mwah πŸ’–

πŸ“¦ where this page comes from