std::execution::into_variant
| Defined in header <execution>
|
||
struct into_variant_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr into_variant_t into_variant{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto into_variant( execution::sender auto sndr );
|
(3) | (since C++26) |
Adapts a sender that can complete with several different sets of values into one that completes with a single value: a std::variant of std::tuples, with one tuple type for every set of values the original sender can send. The value tells which set was actually sent, and carries its values.
Many algorithms and consumers need a sender to complete with exactly one set of values, for example this_thread::sync_wait and when_all. into_variant makes any sender fit: each possible outcome becomes an alternative of the variant, instead of a separate completion.
into_variant is a pipeable sender adaptor: sndr | into_variant() is equivalent to into_variant(sndr). The parentheses are needed, because it takes one argument, the sender.
Parameters
| sndr | - | the sender whose possible sets of values are wrapped in a variant |
Return value
A sender that completes with one value, a std::variant whose alternatives are std::tuples of the decayed value types, one alternative per set of values sndr can send. For example, a sender that sends either an int or a std::string becomes a sender of std::variant<std::tuple<int>, std::tuple<std::string>>. A sender with a single set of values gives a one-alternative variant, and a sender that sends no values gives std::variant<std::tuple<>>. Sets of values that give the same tuple type share one alternative, so a variant never has duplicate alternatives. Do not rely on the order of the alternatives: use std::get_if or std::visit with the tuple type instead of an index.
Error and stopped completions of sndr are forwarded unchanged.
into_variant(sndr) is ill-formed if sndr does not satisfy execution::sender. It is also diagnosed at compile time if the values of some completion cannot be copied or moved into the tuple.
Customization point objects
The name execution::into_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::into_variant_t. See CustomizationPointObject for details.
Details
- Nothing runs when the sender is built. The input is started, and its completion converted, only when the returned sender is started.
- The values are copied or moved into the tuple (move-only values are fine), and the tuple is moved into the variant.
into_variantnever changes where anything runs: the returned sender completes on the execution agent on whichsndrcompleted.
Notes
- A common source of a sender with more than one set of values is upon_error or upon_stopped: turning a failure into a value gives a sender that completes either with the result or with the replacement value.
- when_all_with_variant applies
into_variantto each of its inputs for you. - To get one value out of a sender whose completions you control, it is usually simpler to make them all send the same type, for example with upon_error. Use
into_variantwhen the different outcomes have different types and you need to tell them apart.
Example
A lookup completes with either a number or, after upon_error, a message. sync_wait rejects a sender with two kinds of value, so into_variant turns it into one sender of a variant, and the program looks at the variant to see which kind it got.
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 run(bool found)
{
// sync_wait alone would reject lookup(...): it completes with two kinds of value.
auto [result] = std::this_thread::sync_wait(lookup(found) | ex::into_variant()).value();
if (auto* number = std::get_if<std::tuple<int>>(&result))
std::println("found {}", std::get<0>(*number));
else
std::println("{}", std::get<0>(std::get<std::tuple<std::string>>(result)));
}
int main()
{
run(true);
run(false);
}
Output:
found 42
not found
See also
| adapts multiple input senders, with each possibly having multiple completion signatures, into a sender that completes once all of the input senders have completed (customization point object) | |
(C++26) |
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) |