std::execution::just
| Defined in header <execution>
|
||
struct just_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr just_t just{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto just( auto&&... values );
|
(3) | (since C++26) |
Returns a sender that holds copies of values and, when started, immediately completes by sending them as its result.
just is the usual way to put ordinary values into a sender chain. It is used to begin a chain with data that is already known, as in just(x) | then(f); to return a result from code that must produce a sender but has nothing asynchronous to do, such as one branch of the function passed to let_value; to supply fixed inputs to algorithms such as when_all; and to test sender-based code. just(), with no arguments, completes immediately with no values; it is a common first step for a chain that starts with then.
Parameters
| values | - | the values to send; each one is copied into the returned sender, or moved if it is an rvalue |
Return value
A sender that, when connected to a receiver and started, completes immediately with set_value of the values it holds, on the execution agent that started it. If values are of types Ts..., the values sent are of types std::decay_t<Ts>....
The sender never completes with an error or with the stopped signal.
just(values...) is ill-formed if any of values is an array, or cannot be copied or moved into the sender.
Customization point objects
The name execution::just 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_t. See CustomizationPointObject for details.
Details
- The values are taken when
justis called: the arguments are evaluated and copied into the sender right away, before anything is started. Changing the original objects afterwards does not change what the sender sends. To compute a value when the work runs instead, usejust() | then(f). - When the sender is started as an rvalue (a temporary, or
std::move(sndr)), the values are moved out of it. When it is started as an lvalue, they are copied, so the same sender can be started again and still sends the same values. A sender that holds a value that cannot be copied, such as a std::unique_ptr, has to be moved when it is started; starting it as an lvalue is a compile-time error. - The values are passed on as rvalues, so the function receiving them, for example in then, may take them by value, by rvalue reference or by const reference, and may move from them.
- The sender completes inline, inside the call that starts it, and has no scheduler of its own. The steps that follow it run on the same thread. To produce the values on a given scheduler, use
starts_on(sch, just(values...))orjust(values...) | continues_on(sch).
Notes
- What
justsends does not depend on where the sender is used, so mistakes in a chain that starts with it are diagnosed where the chain is written. For example,just(42) | then([](int* p) { return *p; })is a compile-time error on that line, not when the sender is later started. - To send a reference instead of a copy, pass
std::ref(x). A std::reference_wrapper is sent, which converts to a reference toxwhen it is passed to the next function;xmust outlive the operation. - A string literal is an array, so
just("text")is ill-formed. Pass a std::string, a std::string_view (for example"text"sv), or a pointer. - If the environment of the receiver provides an allocator, for example one supplied with write_env, allocator-aware values such as a
std::pmr::vectorare copied into the started operation using that allocator. - just_error and just_stopped do the same for the error and stopped channels.
Example
just holds its own copies of the values. The same sender is started twice, a value read by just is contrasted with one read when the work runs, and a sender holding a move-only value is moved when started.
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 <memory>
#include <print>
#include <string>
#include <thread>
#include <vector>
namespace ex = std::execution;
int main()
{
// just stores copies of its arguments; nothing runs until it is started.
std::vector v{1, 2, 3};
ex::sender auto scaled = ex::just(v, 10)
| ex::then([](std::vector<int> w, int factor)
{
for (int& e : w)
e *= factor;
return w;
});
v.push_back(4); // does not change the copy held by the sender
// Starting an lvalue sender copies the stored values again,
// so the same sender can be started more than once.
auto [first] = std::this_thread::sync_wait(scaled).value();
auto [second] = std::this_thread::sync_wait(scaled).value();
std::println("v: {}, first run: {}, second run: {}", v, first, second);
// The arguments of just are read when just is called, not when it runs.
// To read a value when the work runs, use then.
int counter = 0;
ex::sender auto early = ex::just(counter);
ex::sender auto late = ex::just() | ex::then([&] { return counter; });
counter = 5;
auto [e] = std::this_thread::sync_wait(early).value();
auto [l] = std::this_thread::sync_wait(late).value();
std::println("just(counter): {}, then reading counter: {}", e, l);
// A sender that holds a move-only value has to be moved when started.
auto owner = ex::just(std::make_unique<std::string>("Moved, not copied"));
auto [p] = std::this_thread::sync_wait(std::move(owner)).value();
std::println("{}", *p);
}
Output:
v: [1, 2, 3, 4], first run: [10, 20, 30], second run: [10, 20, 30]
just(counter): 0, then reading counter: 5
Moved, not copied
See also
(C++26) |
returns a sender that immediately sends a copy of the given error (customization point object) |
(C++26) |
returns a sender that immediately completes with the stopped signal (customization point object) |
(C++26) |
creates a sender that queries its receiver's associated environment (customization point object) |
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and sending its result on (customization point object) |