Namespaces
Variants

std::execution::with_error

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
template< class E >
struct with_error
{
    using type = std::remove_cvref_t<E>;
    type error;
};
(1) (since C++26)
Deduction guide
template< class E >
with_error( E ) -> with_error<E>;
(2) (since C++26)

with_error holds an error that a coroutine of type task passes to co_yield to complete with that error, without throwing an exception: co_yield execution::with_error(err); completes the task with set_error, and the coroutine is not resumed.

1) with_error is an aggregate that holds a copy of the error, of type std::remove_cvref_t<E>. It has no other members, and does nothing by itself: it marks the operand of co_yield as an error, so that the promise type of the task can tell it apart from other values.
2) The deduction guide allows execution::with_error(err) and execution::with_error{err} to deduce E from the error.

Member types

Type Definition
type std::remove_cvref_t<E>

Data members

Member Description
type error the error

Completing a task with an error

  • co_yield execution::with_error(err) suspends the coroutine and completes the task with set_error(std::move(rcvr), Err(std::move(err))), where Err is the error type of the task that the error converts to. The error types of a task are listed in the error_types of its environment type, and are completion_signatures<set_error_t(std::exception_ptr)> by default. The program is ill-formed if the error converts to none of them, or to more than one.
  • Before the task completes, the error is moved out of the coroutine, and the coroutine frame is destroyed, together with the local variables and the parameters of the coroutine. The receiver of the task sees the error only after that, so the error must not refer to objects of the coroutine, such as a std::string_view of a local std::string.
  • co_yield can be used anywhere in the body of the coroutine where co_await can, including in a task<void>, but not in an exception handler. To report an error caught by a catch clause, store it in a variable and use co_yield after the handler.

Notes

  • Throwing an exception from the coroutine also completes the task with an error, but only if set_error_t(std::exception_ptr) is one of its error types: otherwise, std::terminate is called. with_error reports errors of other types, such as std::error_code, without the cost of an exception, and the error types of the task describe them in its completion signatures, so the receiver knows what to expect.
  • The error completes the task right away, like co_return does for a value. co_yield is used instead of co_return execution::with_error(err) because the promise type of a task<void> has return_void, and a promise type cannot have both return_void and return_value.
  • With the default error types, co_yield execution::with_error(std::make_exception_ptr(e)) completes the task with an std::exception_ptr error, without throwing.

Example

parse_digit reports invalid input with an std::error_code instead of an exception. The local variables of the coroutine are destroyed before upon_error sees the error.
The execution control library components may not be available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec (one of the reference implementations).

#include <execution>
#include <print>
#include <system_error>

namespace ex = std::execution;

// The task completes with an std::error_code on failure. Its exceptions
// are not reported: an exception that leaves the coroutine terminates
struct error_code_env
{
    using error_types =
        ex::completion_signatures<ex::set_error_t(std::error_code)>;
};

struct guard
{
    ~guard() { std::println("The local variables are destroyed"); }
};

ex::task<int, error_code_env> parse_digit(char c)
{
    guard g;
    if (c < '0' || c > '9')
    {
        auto ec = std::make_error_code(std::errc::invalid_argument);
        co_yield ex::with_error(ec); // the coroutine does not resume
    }
    co_return c - '0';
}

int main()
{
    for (char c : {'7', 'x'})
    {
        auto sndr = parse_digit(c) | ex::upon_error([](std::error_code ec)
        {
            std::println("Failed: {}", ec.message());
            return -1;
        });
        auto [digit] = std::this_thread::sync_wait(std::move(sndr)).value();
        std::println("Result: {}", digit);
    }
}

Output:

The local variables are destroyed
Result: 7
The local variables are destroyed
Failed: Invalid argument
Result: -1

See also

represents a sender that can be used as the return type of coroutine functions
(class template) [edit]
error completion function indicating that an error occurred during calculation or scheduling
(customization point object)[edit]
returns a sender that immediately sends a copy of the given error
(customization point object)[edit]
chains a function onto a sender, calling it with the error if the sender fails and sending its result on as a value
(customization point object)[edit]