std::execution::inlinable_receiver
| Defined in header <execution>
|
||
template< class Rcvr, class ChildOp >
concept inlinable_receiver =
execution::receiver<Rcvr> &&
requires (ChildOp* child)
{
{ std::remove_cvref_t<Rcvr>::make_receiver_for(child) } noexcept
-> std::same_as<std::remove_cvref_t<Rcvr>>;
};
|
(since C++26) | |
The concept inlinable_receiver specifies that a receiver of type Rcvr can be recreated from a pointer to the operation state of type ChildOp that was created when the receiver was connected to a sender. An operation state that is given such a receiver does not need to store it: it can call Rcvr::make_receiver_for(this) whenever it needs the receiver, to complete or to ask the receiver's environment for a stop token or another property.
ChildOp can be an incomplete type, so that the concept can be checked in the definition of the operation state class to decide whether it stores the receiver.
Semantic requirements
Given a receiver rcvr of type Rcvr that was connected to a sender, producing an operation state op of type Op, Rcvr models inlinable_receiver<Op> only if Rcvr::make_receiver_for(std::addressof(op)) returns a receiver that is equal to rcvr.
Standard library
It is unspecified whether the receivers used by the standard library, for example those that the standard sender algorithms connect to the senders they adapt, satisfy inlinable_receiver.
If a sender of the standard library is connected to a receiver that satisfies inlinable_receiver for the resulting operation state, it is implementation-defined whether the operation state stores the receiver or recreates it with make_receiver_for.
Notes
- A sender algorithm usually connects the senders it adapts to receivers whose only data member is a pointer to the operation state of the algorithm, and the operation states of the adapted senders are stored in that operation state. Each operation state in a chain of work then holds a pointer to the one that contains it, and asking the environment for a property, such as a stop token, follows these pointers up the chain. If the address of the containing operation state can be computed from the address of the contained one, the pointers do not need to be stored, operation states become smaller, and the compiler can replace the walk up the chain by a constant offset.
- The receiver is only recreated if both sides support it: the receiver provides
make_receiver_for, and the operation state checks this concept and does not store the receiver. Otherwise the receiver is stored as usual. - To find the containing operation state from the address of a contained one,
make_receiver_forusually makes the contained operation state, or a class that holds it, a base class of the containing one, and usesstatic_castto convert the pointer to the derived class. Computing it from the address of a data member, for example with offsetof, is undefined behavior unless the two objects are pointer-interconvertible.
Example
answer_operation is the operation state of a sender that completes with 42. It stores a receiver only if the receiver cannot be recreated from the address of the operation state. parent_operation is an operation state that contains it as a base class and connects it to parent_receiver, which it can recreate.
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 <iostream>
#include <type_traits>
#include <utility>
namespace ex = std::execution;
// The operation state of a sender that completes with 42. It stores the
// receiver only if the receiver cannot be recreated from its address
template<class Rcvr>
struct answer_operation
{
using operation_state_concept = ex::operation_state_tag;
static constexpr bool inlined =
ex::inlinable_receiver<Rcvr, answer_operation>;
struct empty { empty(Rcvr) {} };
[[no_unique_address]] std::conditional_t<inlined, empty, Rcvr> rcvr;
explicit answer_operation(Rcvr r) : rcvr(std::move(r)) {}
void start() & noexcept
{
if constexpr (inlined)
ex::set_value(Rcvr::make_receiver_for(this), 42);
else
ex::set_value(std::move(rcvr), 42);
}
};
// A receiver that holds a pointer to where the result goes
struct result_receiver
{
using receiver_concept = ex::receiver_tag;
int* result;
void set_value(int value) && noexcept { *result = value; }
};
// A parent operation state that has the child operation state as a base
class parent_operation;
// The same receiver, but the child operation state can recreate it: the
// parent operation state is found from the address of the child
struct parent_receiver : result_receiver
{
template<class ChildOp>
static parent_receiver make_receiver_for(ChildOp* child) noexcept;
};
class parent_operation : public answer_operation<parent_receiver>
{
public:
int result = 0;
parent_operation()
: answer_operation<parent_receiver>(parent_receiver{&result}) {}
};
template<class ChildOp>
parent_receiver parent_receiver::make_receiver_for(ChildOp* child) noexcept
{
return {&static_cast<parent_operation*>(child)->result};
}
static_assert(!ex::inlinable_receiver<result_receiver,
answer_operation<result_receiver>>);
static_assert(ex::inlinable_receiver<parent_receiver,
answer_operation<parent_receiver>>);
int main()
{
int a = 0;
answer_operation<result_receiver> op1(result_receiver{&a});
ex::start(op1);
parent_operation op2;
ex::start(op2);
std::cout << "Results: " << a << ' ' << op2.result << '\n'
<< std::boolalpha << "Receiver stored: "
<< !decltype(op1)::inlined << ' '
<< !parent_operation::inlined << '\n';
}
Output:
Results: 42 42
Receiver stored: true false
See also
(C++26) |
specifies that a type is a receiver (concept) |
(C++26) |
specifies that a type is an operation state (concept) |
(C++26) |
connects a sender with a receiver (customization point object) |