std::execution::when_all_with_variant
| Defined in header <execution>
|
||
struct when_all_with_variant_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr
when_all_with_variant_t when_all_with_variant{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto
when_all_with_variant( execution::sender auto... sndrs );
|
(3) | (since C++26) |
Like when_all, runs several senders concurrently and completes when all of them have completed. The difference is in what each input is allowed to send: when_all accepts only senders that complete with a single set of values, while when_all_with_variant accepts senders that can complete with several different sets of values, and reports which set each input produced.
Each input contributes one std::variant to the result, whose alternatives are std::tuples: one tuple type for every set of values the input can send. when_all_with_variant(sndrs...) behaves like when_all(into_variant(sndrs)...), where into_variant turns a single sender into one that sends such a variant.
Parameters
| sndrs | - | the senders to run concurrently; at least one, and each may complete with any number of different sets of values |
Return value
A sender that completes when every input has completed. If all inputs complete with values, it completes with one value per input, in the order of the arguments: a std::variant of std::tuples holding the values that input actually sent. For example, an input that sends either an int or a std::string gives a std::variant<std::tuple<int>, std::tuple<std::string>>, and std::get_if or std::visit tell which one it was. An input with a single set of values still gives a variant, with a single alternative. An input that sends no values gives std::variant<std::tuple<>>.
Errors and cancellation behave as for when_all: if an input fails, the returned sender completes with that error, if one is stopped it completes stopped, and in both cases the other inputs are asked to stop and the sender completes once all of them have finished.
when_all_with_variant(sndrs...) is ill-formed if no senders are passed or one of them does not satisfy execution::sender.
Customization point objects
The name execution::when_all_with_variant 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::when_all_with_variant_t. See CustomizationPointObject for details.
Details
- Everything about starting, concurrency, the stop token given to the inputs, and where the returned sender completes is the same as for when_all.
- The returned sender sends one value per input, never the concatenation of their values:
when_all_with_variant(just(1, 2), just(3))sends two values (a variant holding astd::tuple<int, int>, and a variant holding astd::tuple<int>), wherewhen_all(just(1, 2), just(3))would send three. - The values are copied or moved into the tuples (move-only values are fine).
Notes
- Use
when_all_with_variantwhen an input can succeed in more than one way and you need to know which. If every input sends exactly one set of values,when_allis simpler, because it gives the values directly instead of inside variants. - A common way to get a sender with more than one set of values is to turn a failure into a value with upon_error: a lookup that produces either a result or an error message then has two value completions.
Example
Two lookups that can each produce either a number or an error message run concurrently. when_all would reject them, because each can complete with two kinds of value, so when_all_with_variant reports for each input which kind it produced.
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 <exception>
#include <execution>
#include <print>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <variant>
namespace ex = std::execution;
// Completes with the number 42, or with a message if the lookup fails.
ex::sender auto lookup(bool found)
{
return ex::just(found)
| ex::then([](bool found)
{
if (!found)
throw std::runtime_error("not found");
return 42;
})
| ex::upon_error([](std::exception_ptr)
{
return std::string("not found");
});
}
void report(const char* label,
const std::variant<std::tuple<int>, std::tuple<std::string>>& result)
{
if (auto* number = std::get_if<std::tuple<int>>(&result))
std::println("{}: found {}", label, std::get<0>(*number));
else
std::println("{}: {}", label, std::get<0>(std::get<std::tuple<std::string>>(result)));
}
int main()
{
auto sndr = ex::when_all_with_variant(lookup(true), lookup(false));
auto [first, second] = std::this_thread::sync_wait(std::move(sndr)).value();
report("first", first);
report("second", second);
}
Output:
first: found 42
second: not found
See also
(C++26) |
adapts multiple input senders into a sender that completes once all of the input senders have completed (customization point object) |
(C++26) |
adapts a sender that can complete with several different sets of values into one that completes with a single std::variant of tuples (customization point object) |