Troubleshooting C++ std::ranges::empty: A Friendly Guide

2025-09-22

The std::ranges::empty function is a simple, non-member function that returns true if a given range contains no elements, and false otherwise. It's a cleaner, more generic way to check for emptiness compared to traditional methods like container.empty(). It works on any type that models the std::ranges::empty_range concept.

#include <iostream>
#include <vector>
#include <ranges>

int main() {
    std::vector<int> numbers = {1, 2, 3};
    std::vector<int> empty_vector;

    if (std::ranges::empty(numbers)) {
        std::cout << "The 'numbers' vector is empty." << std::endl;
    } else {
        std::cout << "The 'numbers' vector is not empty." << std::endl;
    }

    if (std::ranges::empty(empty_vector)) {
        std::cout << "The 'empty_vector' is empty." << std::endl;
    } else {
        std::cout << "The 'empty_vector' is not empty." << std::endl;
    }

    return 0;
}

Expected Output

The 'numbers' vector is not empty.
The 'empty_vector' is empty.

A very common mistake is forgetting to include the <ranges> header. Without it, the compiler won't know about std::ranges::empty and will throw a compilation error, typically an "undeclared identifier" error.

Bad Example (Missing header)

#include <vector> // Missing <ranges>

int main() {
    std::vector<int> data = {1, 2, 3};
    if (std::ranges::empty(data)) { // ERROR: 'std::ranges::empty' is not a member of 'std::ranges'
        // ...
    }
    return 0;
}

Fix
Just remember to add #include <ranges>.

#include <vector>
#include <ranges> // Don't forget this!

int main() {
    std::vector<int> data = {1, 2, 3};
    if (std::ranges::empty(data)) {
        // ...
    }
    return 0;
}

The std::ranges::empty function works on types that satisfy the std::ranges::empty_range concept. This usually means containers like std::vector, std::list, std::string, and views from the ranges library. If you try to use it on a type that isn't a range, like a raw pointer to a single element, you'll get a compiler error.

Bad Example (Using on a non-range)

#include <iostream>
#include <ranges>

int main() {
    int value = 42;
    if (std::ranges::empty(value)) { // ERROR: 'int' does not model 'std::ranges::empty_range'
        // ...
    }
    return 0;
}

Fix
Make sure you are passing a valid range. If you have a raw array, it works fine because arrays decay into a valid range. For a single value, it simply doesn't make sense to check if it's "empty."

While std::ranges::empty is the modern and often preferred way, especially for generic programming, you might see or need to use older methods. These are good to know for working with legacy code or when you simply prefer a different style.

Most standard library containers have an empty() member function. This is a classic and very efficient way to check for emptiness.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3};

    if (numbers.empty()) { // Using the member function
        std::cout << "The vector is empty." << std::endl;
    } else {
        std::cout << "The vector is not empty." << std::endl;
    }

    return 0;
}

You can also check if a container's size is equal to zero. This is less common now but still works perfectly. It's especially useful for containers that don't have an empty() member function but do have a size() function.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3};

    if (numbers.size() == 0) { // Checking the size
        std::cout << "The vector is empty." << std::endl;
    } else {
        std::cout << "The vector is not empty." << std::endl;
    }

    return 0;
}

std::ranges::empty
This is the most generic and modern approach. Use it when writing generic code (e.g., templates) that needs to work with any kind of range, including views and custom types that model the empty_range concept. It promotes consistency and makes your code more adaptable.

container.empty()
This is the most direct and idiomatic way to check for emptiness on a specific container type, like a std::vector or std::string. It's concise and readable. It's often the best choice for non-generic code.

container.size() == 0
While correct, this is generally less preferred than container.empty() because size() might not be a constant-time operation for all containers (e.g., std::list), whereas empty() is guaranteed to be O(1). Use it only when empty() isn't available.


cpp



Fixing Common std::ctype::scan_not Errors in C++

std::ctype::scan_not is a fascinating and often misunderstood function in the C++ standard library, part of the localization features


Efficiently Using std::ranges::stride_view: Pitfalls and Alternatives

Think of a stride_view as a way to "skip" elements in a range. Instead of looking at every item one by one, you can tell it to move forward by a certain number of steps (the "stride"). For example


Stop the Clock: Fixing std::chrono::month's Most Common Arithmetic Bugs

Let's dive into some common issues and look at powerful alternative methods using C++'s modern utilities.The std::chrono::month class is a calendar type that represents a month of the year



Common Issues and Alternatives for std::ranges::slide_view::iterator::operator[]

One of the biggest gotchas with operator[] on a slide view iterator is that it's often not what you think it is. The slide_view creates a "sliding window" over a range


Why You Should Stop Using std::wstring_convert::to_bytes in C++

Instead of using this deprecated feature, you should use the std::codecvt or icu4c (International Components for Unicode) library for robust and modern character encoding conversions


From Buffered to Unbuffered: A Guide to C++ Logging with std::clog and std::cerr

std::clog is an object used for outputting log messages, but it's often overlooked in favor of std::cerr or std::cout. Let's dive into some common snags you might hit and how to work around them


Understanding std::compare_three_way_result in C++ Utilities

I'd be happy to explain std::compare_three_way_result from the C++ Utilities library, including common pitfalls and alternative approaches


Why You Shouldn't Use std::not1 in C++ (and What to Use Instead)

The std::not1 function in C++ is a function object adapter. Its main job is to take a function object (like a functor or a lambda) that has one argument and return a new function object that does the opposite (the logical negation). It's designed specifically for use with unary predicates


Common Pitfalls and Alternatives for std::match_results

In C++, std::swap is a fundamental function template that exchanges the values of two objects. When you use it with std::match_results


Understanding C++ constexpr: When to Use It and When to Use const or Templates

This is the most frequent issue. A constexpr function or variable must be able to be evaluated at compile time. If your function uses something that's only known at runtime