std::initializer_list<T>::empty
From cppreference.com
bool empty() const noexcept;
|
(since C++11) (constexpr since C++14) |
|
Checks whether the initializer_list is empty. Equivalent to return size() == 0;.
Return value
true if the initializer_list is empty, false otherwise.
Complexity
Constant
Notes
As for 2026-05-27, libstdc++ has not treated the addition of the empty function from P3016R6 as a defect report against C++11, and only adds empty since C++26.
Example
Run this code
#include <initializer_list>
#include <iostream>
constexpr std::initializer_list<int> il1{};
constexpr std::initializer_list<int> il2{3, 1, 4, 1, 5, 9};
int main()
{
static_assert(il1.empty(), "");
static_assert(!il2.empty(), "");
std::cout << std::boolalpha
<< il1.empty() << '\n'
<< il2.empty() << '\n';
}
Output:
true
false
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P3016R6 | C++11 | std::initializer_list lacked empty function
|
added |
See also
(C++11) |
returns the number of elements in the initializer list (public member function) |