GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::empty(3) C++ Standard Libary std::empty(3)

std::empty - std::empty


Defined in header <array>
Defined in header <deque>
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> (since
C++20)
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
template <class C> (since
constexpr auto empty(const C& c) -> C++17)
decltype(c.empty()); (until
C++20)
template <class C> (since
[[nodiscard]] constexpr auto empty(const C& c) -> C++20)
decltype(c.empty());
template <class T, std::size_t N> (since
constexpr bool empty(const T (&array)[N]) C++17)
noexcept; (until
(1) C++20)
template <class T, std::size_t N> (since
[[nodiscard]] constexpr bool empty(const T C++20)
(&array)[N]) noexcept; (2)
template <class E> (since
constexpr bool empty(std::initializer_list<E> il) C++17)
noexcept; (until
(3) C++20)
template <class E> (since
[[nodiscard]] constexpr bool C++20)
empty(std::initializer_list<E> il) noexcept;


Returns whether the given range is empty.


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


c - a container or view with an empty member function
array - an array of arbitrary type
il - an initializer list


true if the range doesn't have any element.


1) May throw implementation-defined exceptions.


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


Feature-test macro: __cpp_lib_nonmember_container_access


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


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;
}

// 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);
}


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


ranges::empty checks whether a range is empty
(C++20) (customization point object)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.