std::empty
Min standard notice:
Header: <array>
Returns whether the given range is empty.
# Declarations
template< class C >
constexpr auto empty( const C& c ) -> decltype(c.empty());
(since C++17)
template< class T, std::size_t N >
constexpr bool empty( const T (&array)[N] ) noexcept;
(since C++17)
template< class E >
constexpr bool empty( std::initializer_list<E> il ) noexcept;
(since C++17)
# Parameters
c: a container or view with an empty member functionarray: an array of arbitrary typeil: an std::initializer_list
# Notes
The overload for std::initializer_list is necessary because it does not have a member function empty.
# Example
#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);
}