std::execution::connect, std::execution::connect_result_t
| Defined in header <execution>
|
||
struct connect_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr connect_t connect{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
template< class Sndr, class Rcvr >
/* see below */
connect( Sndr&& sndr, Rcvr&& rcvr ) noexcept(/* see below */);
|
(3) | (since C++26) |
| Helper alias template |
||
template< class Sndr, class Rcvr >
using connect_result_t =
decltype(execution::connect(std::declval<Sndr>(), std::declval<Rcvr>()));
|
(4) | (since C++26) |
connect(sndr, rcvr) connects the sender sndr to the receiver rcvr: it creates the asynchronous operation that the sender describes, with rcvr as the receiver of its result, and returns its operation state. Nothing runs yet: the operation runs when start is called on the operation state, and it then completes by calling one of the completion functions on the receiver.
connect is the second of the three steps in which asynchronous work is run: a sender is created, it is connected to a receiver, and the operation state is started. The first two steps can throw exceptions; start, and everything that happens after it, reports errors through the receiver instead.
connect.sndr and rcvr as follows:
- First, the sender can be replaced with another one by
execution::transform_sender(sndr, execution::get_env(rcvr)). This lets the environment of the receiver choose a different implementation of a sender algorithm, for example one that runs on the hardware of a scheduler in that environment; see transform_sender. For most senders, the sender is not changed. - Then, if
new_sndr.connect(rcvr)is a valid expression, wherenew_sndris the result of the first step, it is called, and its result is returned. It must be an object whose type satisfies operation_state; otherwise, the program is ill-formed. - Otherwise,
new_sndris an awaitable object, such as the return object of a coroutine that can be awaited:connectcreates a coroutine that awaits it withco_await, and returns an operation state that starts this coroutine. When theco_awaitexpression produces a result, the coroutine completes the operation withset_value(std::move(rcvr), result), or withset_value(std::move(rcvr))if the result has typevoid. If it throws an exception, the coroutine completes the operation withset_error(std::move(rcvr), std::current_exception()). If the awaitable reports that it was stopped, by callingunhandled_stopped()on the promise of the coroutine, as the awaitables created by as_awaitable do for senders that complete with set_stopped, the operation completes withset_stopped(std::move(rcvr)).
- The expression
rcvris evaluated only once. The program is ill-formed if the sender cannot be used in the environment of the receiver, or if the receiver does not accept every completion that the sender can complete with in that environment.
Sndr to a receiver of type Rcvr.Parameters
| sndr | - | the sender that describes the work |
| rcvr | - | the receiver that receives the result of the work |
Return value
The operation state of the asynchronous operation. Its type, connect_result_t<Sndr, Rcvr>, satisfies operation_state.
Exceptions
Any exception thrown while the operation state is created, for example when values that the sender holds are copied into it, or when memory is allocated for it. Whether connect can throw may depend only on the type of the sender and on the type of the environment of the receiver, and not on any other property of the type of the receiver; the program is ill-formed, no diagnostic required, if a receiver of another type, with the same type of environment, would change it. This lets a sender decide whether its completion signatures need set_error_t(std::exception_ptr) before it knows the receiver.
Customization point objects
The name execution::connect 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::connect_t. See CustomizationPointObject for details.
Notes
- Most programs do not call
connect: this_thread::sync_wait, spawn, spawn_future,co_awaitin a task, and the sender adaptors connect senders to receivers of their own.connectis called directly by code that runs senders in its own way, such as a sender algorithm, or a bridge to code that does not use senders. - A sender that is connected as an lvalue is not changed, and can be connected again, which creates another operation; its values are copied into each operation state, so they must be copyable. A sender that is connected as an rvalue can move its values into the operation state, and may then not be usable afterwards.
- The receiver is usually passed as an rvalue, and the operation state stores it.
- The operation state cannot be moved or copied. It can be stored by initializing it from the result of
connect, because of guaranteed copy elision, as inauto op = execution::connect(sndr, rcvr);, and it must stay alive until the operation completes; see operation_state. - A sender and a receiver that cannot be connected make the program ill-formed; this is not a substitution failure, so
requires { execution::connect(sndr, rcvr); }does not test whether they can be connected (LWG issue 4206). To test it, check that the sender satisfiessender_in<Sndr, execution::env_of_t<Rcvr>>, and that the receiver accepts each signature inexecution::completion_signatures_of_t<Sndr, execution::env_of_t<Rcvr>>. - Connecting an awaitable that is not a sender creates a coroutine, whose frame is allocated with the global operator new unless the allocation is optimized out (LWG issue 4356).
- A sender type supports
connectby defining a member functionconnect(rcvr)that returns its operation state. It can be overloaded on the value category of the sender, for example asconnect(Rcvr rcvr) &&to move from the sender, andconnect(Rcvr rcvr) const&to copy from it.
Example
Connecting a sender, and an awaitable that is not written as a sender, to a program-defined receiver, and starting the operations.
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 <coroutine>
#include <exception>
#include <execution>
#include <print>
namespace ex = std::execution;
// Prints how the operation it is connected to completes
struct print_receiver
{
using receiver_concept = ex::receiver_tag;
void set_value(int v) && noexcept { std::println("Value: {}", v); }
void set_error(std::exception_ptr) && noexcept { std::println("Error"); }
void set_stopped() && noexcept { std::println("Stopped"); }
};
// An awaitable that is not written as a sender
struct ready_value
{
int value;
bool await_ready() const noexcept { return true; }
void await_suspend(std::coroutine_handle<>) const noexcept {}
int await_resume() const noexcept { return value; }
};
int main()
{
auto sndr = ex::just(6) | ex::then([](int i)
{
return i * 7;
});
// connect creates the operation state; nothing runs until it is started
auto op = ex::connect(sndr, print_receiver{});
std::println("Connected");
ex::start(op);
// An lvalue sender is not consumed: connecting it again creates
// another operation
auto op2 = ex::connect(sndr, print_receiver{});
ex::start(op2);
// Connecting an awaitable runs it in a coroutine
auto op3 = ex::connect(ready_value{5}, print_receiver{});
ex::start(op3);
}
Output:
Connected
Value: 42
Value: 42
Value: 5
See also
(C++26) |
starts the asynchronous operation associated with an operation_state object (customization point object) |
(C++26) |
specifies that a type is an operation state (concept) |
(C++26) |
specifies that a type is a sender, a description of asynchronous work (concept) |
(C++26) |
specifies that a type is a receiver (concept) |
(C++26) |
specifies that the ways a sender can complete are known, for a given environment or without one (concept) |
| obtains the completion signatures of a sender, or their type (function) | |
(C++26) |
transforms into a new sender under a given execution domain tag (function template) |