Namespaces
Variants

std::execution::just_error

From cppreference.com
 
 
Execution control library
Concepts
Utility components
Execution contexts
Execution domains
Forward progress guarantee
Environments
    
    
    
Queries
Completion signatures
Pipeable sender adaptors
Coroutine utility
 
Defined in header <execution>
struct just_error_t { /* unspecified */ };
(1) (since C++26)
inline constexpr just_error_t just_error{};
(2) (since C++26)
(customization point object)
Call signature
execution::sender auto just_error( auto&& error );
(3) (since C++26)

Returns a sender that holds a copy of error and, when started, immediately completes with it as an error.

just_error is the error counterpart of just: it puts a known error into a sender chain. Its main use is inside the function passed to let_value, let_error or let_stopped, where returning just_error(e) turns that completion into an error: for example, to report cancellation as an error, or to replace a low-level error with one that means more to the caller. It is also useful for testing how sender-based code handles failure, and as a stand-in for an operation that has failed.

Parameters

error - the error to send; it is copied into the returned sender, or moved if it is an rvalue. It can be of any type that can be copied or moved, except an array; std::exception_ptr and std::error_code are the most common

Return value

A sender that, when connected to a receiver and started, completes immediately with set_error of the error it holds, on the execution agent that started it. If error is of type E, the error sent is of type std::decay_t<E>.

The sender never completes with a value or with the stopped signal.

just_error(error) is ill-formed if error is an array or cannot be copied or moved into the sender, or if it is not called with exactly one argument.

Customization point objects

The name execution::just_error denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::just_error_t. See CustomizationPointObject for details.

Details

  • The error is taken when just_error is called: the argument is evaluated and copied into the sender right away, before anything is started.
  • When the sender is started as an rvalue, the error is moved out of it; when it is started as an lvalue, the error is copied, so the same sender can be started more than once. The error is passed on as an rvalue, so a handler such as the function given to upon_error may take it by value, by rvalue reference or by const reference.
  • The sender completes inline, inside the call that starts it, on the same thread.
  • Algorithms that follow the sender in a chain see only an error: then and the other value handlers are skipped, and the error is passed on until something handles it, such as upon_error or let_error.

Notes

  • The sender cannot complete with a value, so it cannot be waited for on its own: this_thread::sync_wait(just_error(e)) is a compile-time error, because sync_wait needs exactly one way of completing with values. The same holds for a when_all that contains it. Handle the error first, for example with upon_error, which turns it into a value.
  • When an error reaches sync_wait, or a co_await in a coroutine, it is thrown: a std::exception_ptr is rethrown, a std::error_code is thrown as a std::system_error, and an error of any other type is thrown as is.
  • just(value) and just_error(error) have different types, so a function cannot return one or the other depending on a run-time condition. To fail at run time, throw from the function passed to then, which makes the sender complete with a std::exception_ptr; in an task coroutine, use co_yield with_error(e).
  • stopped_as_error reports cancellation as an error in the same way as the second part of the example below, by returning just_error from let_stopped.
  • just and just_stopped do the same for the value and stopped channels.

Example

A sender made with just_error skips then and is handled by upon_error. Then just_error is returned from let_stopped to report a cancellation as an error.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.

#include <execution>
#include <iostream>
#include <string>
#include <system_error>
#include <thread>

namespace ex = std::execution;

int main()
{
    auto report = [](std::error_code ec) { return "Handled: " + ec.message(); };

    // A sender that fails as soon as it is started. It sends no value, so work
    // attached with then is skipped; upon_error turns the error into a value.
    auto failed = ex::just_error(std::make_error_code(std::errc::timed_out))
                | ex::then([] { std::cout << "Not printed\n"; })
                | ex::upon_error(report);
    auto [a] = std::this_thread::sync_wait(std::move(failed)).value();
    std::cout << a << '\n';

    // Returned from let_stopped (or let_value, let_error), just_error turns
    // that completion into an error; here, cancellation is reported as one.
    auto as_error = []() noexcept
    {
        auto ec = std::make_error_code(std::errc::operation_canceled);
        return ex::just_error(ec);
    };
    auto cancelled = ex::just_stopped()
                   | ex::let_stopped(as_error)
                   | ex::upon_error(report);
    auto [b] = std::this_thread::sync_wait(std::move(cancelled)).value();
    std::cout << b << '\n';
}

Output:

Handled: Connection timed out
Handled: Operation canceled

See also

returns a sender that immediately sends copies of the given values
(customization point object)[edit]
returns a sender that immediately completes with the stopped signal
(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]
chains a function onto a sender, calling it with the error if the sender fails and continuing with the sender it returns
(customization point object)[edit]
returns a sender that maps the stopped channel to an error
(customization point object)[edit]