std::array empty() method
- od C++20
- do C++20
// Const version only
[[nodiscard]] constexpr bool empty() const noexcept;
// Const version only
constexpr bool empty() const noexcept;
Checks if the container has no elements, i.e. whether begin() == end()
.
Parameters
(none)
Return value
true
if the container is empty, false
otherwise.
Complexity
Constant.
Why [[nodiscard]]
?
The [[nodiscard]]
attribute is an attribute that invokes compiler warnings whenever
a function has been called an it's result has been discarded.
The reason behind the nodiscard being applied only to the empty
method is that
it's likely that the programmer might confuse the adjective empty
(
which would mean - is this container empty?) for the verb empty
(which would mean - please empty this container for me.).
Example
#include <array>
#include <iostream>
int main()
{
std::array<char, 6> letters {'a', 'b', 'c', 'd', 'e', 'f'};
if (!letters.empty()) {
std::cout << "The last character is '" << letters.back() << "'.\n";
}
}
numbers.empty(): false
no_numbers.empty(): true
Hover to see the original license.