C++ std::array empty() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::array

Definition

In C++, std::array::empty() returns true when the array has no elements and false when it has at least one.

A std::array<T, N> has a fixed size N, so the answer never changes while the program runs. For N > 0 it is usually false. It is true only for std::array<T, 0>. Include <array> to use it.

This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.

Syntax

Call empty with empty brackets. It takes no arguments.

C++
bool none = nums.empty();
if (!nums.empty()) { /* safe to read */ }
CallWhat it does
nums.empty()Returns true if the array has zero elements.
!nums.empty()Returns true if the array has at least one element.

empty() asks about the size, not the values. An array full of zeros is not empty.

Library declarations
C++
constexpr bool empty() const noexcept;

Parameters

ParameterDescription
Noneempty() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
trueThe array has no elements. This only happens for std::array<T, 0>.
falseThe array has one or more elements.

C++ Examples

Example 1 — Basic empty()

Compare an array of 3 ints with an array of 0 ints.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> nums = {1, 2, 3};
    std::array<int, 0> none;

    std::cout << std::boolalpha;
    std::cout << "nums.empty(): " << nums.empty() << '\n';
    std::cout << "none.empty(): " << none.empty() << '\n';
    return 0;
}

How It Works

  1. std::boolalpha makes cout print true or false instead of 1 or 0.
  2. nums has 3 elements, so empty() is false.
  3. none has size 0, so empty() is true.

Example 2 — Report Any Array Size

A template function can accept arrays of any type and size, then use empty().

C++
#include <array>
#include <iostream>

template <typename T, std::size_t N>
void report(const std::array<T, N>& a) {
    if (a.empty()) {
        std::cout << "Array is empty\n";
    } else {
        std::cout << "Array has " << a.size() << " item(s)\n";
    }
}

int main() {
    report(std::array<int, 3>{4, 5, 6});
    report(std::array<int, 0>{});
    return 0;
}

How It Works

  1. The template copies the type T and the size N from whatever array you pass in.
  2. The first call has N = 3, so it goes to the else branch.
  3. The second call has N = 0, so empty() is true and the first message prints.

Example 3 — Zeros Are Not Empty

A zero-filled array still has elements.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 5> zeros{};

    std::cout << std::boolalpha;
    std::cout << "empty: " << zeros.empty() << '\n';
    std::cout << "size: " << zeros.size() << '\n';
    return 0;
}

How It Works

  1. The empty braces {} set all five elements to 0.
  2. The array still has five slots, so empty() prints false.
  3. size() confirms the count of 5. Values do not matter, only the size.

Frequently Asked Questions

It returns true if the array has no elements, and false otherwise.
Only if its size is 0, such as std::array<int, 0>. Any array with N greater than 0 is never empty.
No. It only looks at the size. An array like std::array<int, 5>{} is full of zeros but is not empty.
Yes. For std::array, empty() returns the same answer as size() == 0, and it is easier to read.
Yes, and it is more useful there. A vector can start empty and grow, so its empty() result can change. An array size is fixed.

Next: end()

Get an iterator one past the last element with std::array.

end() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful