Common Issues with std::make_error_code(std::errc) and How to Fix Them

2025-09-07

In C++, std::make_error_code(std::errc) is a very useful function. It's part of the <system_error> header and is used to convert a value from the std::errc enum into a std::error_code object.

The std::errc enum contains a list of portable, system-agnostic error conditions, like std::errc::address_not_available or std::errc::permission_denied. These are based on the POSIX errno values. The great thing is that you can use std::make_error_code to easily create a std::error_code object that represents one of these conditions. This object is much more powerful because it can be used for things like throwing exceptions (std::system_error) or being stored as a member variable.

Here's a quick example

#include <iostream>
#include <system_error>
#include <string>

int main() {
    std::error_code ec = std::make_error_code(std::errc::permission_denied);
    
    std::cout << "Error Code: " << ec.value() << std::endl;
    std::cout << "Message: " << ec.message() << std::endl;
    
    return 0;
}

This code creates a std::error_code and prints out its value and a human-readable message.

This is a very common and easy-to-miss mistake. If you forget to include the <system_error> header, the compiler won't know what std::make_error_code or std::errc are, and you'll get a compile-time error.

Wrong way

// Missing <system_error> include
#include <iostream>

int main() {
    std::error_code ec = std::make_error_code(std::errc::permission_denied); // Compiler error
    return 0;
}

Right way

Always remember to include the necessary header file.

#include <iostream>
#include <system_error> // Correct include
#include <string>

int main() {
    std::error_code ec = std::make_error_code(std::errc::permission_denied);
    
    std::cout << "Error Message: " << ec.message() << std::endl;
    
    return 0;
}

std::error_code objects are associated with an error category. The std::make_error_code(std::errc) function always creates an std::error_code belonging to the std::generic_category. If you're comparing it to an std::error_code from a different category (for example, one created from a WinSock or other platform-specific error), the comparison might not work as you expect.

Example of a potential mistake

#include <iostream>
#include <system_error>

void doSomething(int platformSpecificError) {
    // This isn't how it works. You can't just compare
    // generic errors to platform-specific ones without
    // a proper mapping.
    // The comparison below will likely always be false.
    if (std::error_code(platformSpecificError, std::system_category()) == 
        std::make_error_code(std::errc::permission_denied)) {
        std::cout << "This comparison is likely not correct." << std::endl;
    }
}

Solution

The best practice is to always use the std::generic_category for your portable error checks. The great thing is that std::error_code has an overload of the == operator that handles comparisons between categories correctly. It's often better to compare against the std::errc value directly or check the category if you're not sure.

#include <iostream>
#include <system_error>

void handle_error(const std::error_code& ec) {
    if (ec == std::errc::permission_denied) {
        std::cout << "Permission was denied!" << std::endl;
    } else {
        std::cout << "Some other error occurred." << std::endl;
    }
}

int main() {
    std::error_code generic_ec = std::make_error_code(std::errc::permission_denied);
    handle_error(generic_ec);
    
    // You can even compare against a platform-specific error code
    // if the platform category has a mapping to the generic category.
    // The C++ standard library guarantees that the system_category has
    // a mapping for common errors.
    std::error_code system_ec = std::make_error_code(std::errc::permission_denied);
    if (system_ec == std::errc::permission_denied) {
        std::cout << "Comparison with system category works too!" << std::endl;
    }

    return 0;
}

While std::make_error_code(std::errc) is great for creating a std::error_code from a portable enum, sometimes you might need to handle errors in a different way.

If you are dealing with a platform-specific error code (like errno from a C library call or a Windows API error code), you can use the std::error_code constructor that takes an integer and an std::error_category.

Example

#include <iostream>
#include <system_error>
#include <cerrno> // For the 'errno' macro

// Simulate a C-style function that fails
int open_file_with_errno(const char* path) {
    // Let's pretend this function failed
    errno = EACCES; // EACCES is often a permission denied error
    return -1;
}

int main() {
    open_file_with_errno("my_file.txt");
    
    // Create an error code from the system's 'errno' value
    // EACCES will be mapped to a generic error if possible.
    std::error_code ec(errno, std::generic_category()); 
    
    std::cout << "Error Code from errno: " << ec.value() << std::endl;
    std::cout << "Message: " << ec.message() << std::endl;
    
    return 0;
}

This approach is perfect when you are interfacing with a C-style API that uses errno or similar integer codes.

In C++, you can use exceptions to handle errors, which is often a cleaner and more modern approach. The standard library provides std::system_error which can be constructed directly from a std::error_code.

Example

#include <iostream>
#include <system_error>
#include <stdexcept>

void access_file() {
    // Simulate an operation that fails due to a generic error
    bool success = false;
    if (!success) {
        throw std::system_error(std::make_error_code(std::errc::permission_denied));
    }
}

int main() {
    try {
        access_file();
    } catch (const std::system_error& e) {
        std::cout << "Caught a system error!" << std::endl;
        std::cout << "Error code: " << e.code().value() << std::endl;
        std::cout << "Error message: " << e.what() << std::endl;
    } catch (const std::exception& e) {
        std::cout << "Caught another exception: " << e.what() << std::endl;
    }
    
    return 0;
}

This is a great way to handle errors in a way that separates the error-prone code from the error-handling logic.


cpp



Mastering Leap Seconds in C++: A Guide to Alternative Chrono Solutions

It seems like you're asking about the std::chrono::leap_second::date class. That's a great question, but here's an important detail that specific class doesn't exist in the C++20 standard library


C++ Regex Troubleshooting: A Friendly Guide to std::sub_match

When you perform a regex search in C++, the std::match_results object stores the results. Each successful capture group within that result is represented by a std::sub_match object


C++ std::swap with std::function: Common Pitfalls and Safe Alternatives

The most common issue people run into when trying to swap std::function objects is that swapping can throw an exception



A Friendly Guide to C++ std::ostream_iterator and Its Common Pitfalls

The std::ostream_iterator is a kind of output iterator. Its job is to write elements to an output stream (like std::cout). The operator= is the assignment operator


Why You Shouldn't Use std::unexpected_handler: A C++ Deprecation Guide

First off, let's understand what std::unexpected_handler is.In older C++ standards (before C++17), you could use Dynamic Exception Specifications (like void func() throw(std::runtime_error


The Art of Moving: A Guide to std::move in C++

std::move is a fascinating tool that, despite its name, doesn't actually move anything. Instead, it's a static cast that turns an L-value into an R-value reference


Formatting C++'s std::chrono::month_day_last: A Developer's Guide

First, let's understand what std::formatter<std::chrono::month_day_last> is. It's part of the C++20 <chrono> library and the new <format> library


Mastering std::owner_less for C++ Smart Pointers

std::owner_less is a C++ utility that helps you compare std::shared_ptr and std::weak_ptr objects. Unlike a regular comparison (< or ==), which compares the value of the pointer itself


Mastering C++ Localizations: A Guide to std::isspace and std::locale

When you're working with text in C++, especially for international applications, you need to be careful about how you handle things like whitespace


Mastering C++ Memory Pools: The Right Way to Deallocate

The primary purpose of do_deallocate is to release memory allocated by the corresponding do_allocate call. A common problem is misusing the function directly instead of using the public interface