std::empty(3)
πΈ 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.
- Returns
c.empty(). - Returns
false. - Returns
il.size() == 0.
π Parameters π
cβ a container or view with anemptymember functionarrayβ an array of arbitrary typeilβ anstd::initializer_list
β Return value β
c.empty()falseil.size() == 0
π Exceptions π
- 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(), andstd::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
- 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