Return Value or Error with std::expected in C++23

Return Value or Error with std::expected in C++23

Error handling is an important part of reliable software design. Many operations may either produce a valid result or fail due to invalid input. Representing these two possible outcomes in a clear and structured way improves code readability and maintainability. Historically, C++ applications have addressed this problem using exceptions or by returning error codes alongside output parameters. Although both approaches are widely used, they can introduce additional complexity. Exceptions separate the error path from the normal control flow, while manual error-code handling may lead to verbose and inconsistent patterns.

Since C++23, we can use the std::expected for storing either a successfully computed value or an error. Unlike std::optional, which only indicates the presence or absence of a value, std::expected also contains information describing the failure.

The following example demonstrates a function that performs division and returns either the computed result or an error message:

#include <iostream>
#include <expected>

std::expected<float, const char *> divide(float a, float b) {
    if (b == 0) {
        return std::unexpected("Division by zero");
    }

    return a / b;
}

int main() {
    auto result = divide(10.5f, 0.0f);
    if (result) {
        std::cout << *result << std::endl;
    } else {
        std::cout << result.error() << std::endl;
    }

    return 0;
}

In this code, the divide function returns a floating-point value when the operation succeeds. If the divisor is zero, it returns an error wrapped in std::unexpected. The caller checks whether a valid result is available and handles both scenarios explicitly, keeping success and failure paths clear and easy to follow.

Leave a Comment

Cancel reply

Your email address will not be published.