Namespaces
Variants

std::execution::value_types_of_t

From cppreference.com
 
 
Execution control library
Concepts
 
Defined in header <execution>
template< class Sndr, class Env = execution::env<>,
          template< class... > class Tuple = /*decayed-tuple*/,
          template< class... > class Variant = /*variant-or-empty*/ >
    requires execution::sender_in<Sndr, Env>
using value_types_of_t = /* see below */;
(since C++26)

Names the types of the values with which a sender of type Sndr can complete successfully when it is connected to a receiver whose environment has type Env.

For each value completion signature execution::set_value_t(Args...) in execution::completion_signatures_of_t<Sndr, Env>, Tuple is applied to the arguments, and Variant is applied to the results: a sender with the value completions execution::set_value_t(A...) and execution::set_value_t(B...) gives Variant<Tuple<A...>, Tuple<B...>>. Tuple and Variant do not have to be variadic templates, but they must accept the arguments they get: for example, std::type_identity_t works as Variant when the sender has exactly one value completion.

By default:

  • /*decayed-tuple*/<Ts...> is std::tuple<std::decay_t<Ts>...>;
  • /*variant-or-empty*/<Ts...> is std::variant<Us...>, where Us... are the types std::decay_t<Ts>... with duplicates removed; if there are no Ts, it is an unspecified class type that cannot be constructed.

So for a sender that completes with execution::set_value_t(int, const std::string&) only, the result is std::variant<std::tuple<int, std::string>>.

Sndr includes the value category with which the sender is going to be used: Sndr& for an lvalue, Sndr or Sndr&& for an rvalue.

Notes

  • Generic code should not depend on the order of the alternatives in the Variant.
  • The default environment execution::env<> is the empty environment, not the absence of one: a sender whose value types depend on the environment, such as execution::read_env(std::get_stop_token), gets them from the defaults of the queries. To know the value types of a sender connected to a receiver of type Rcvr, use execution::value_types_of_t<Sndr, execution::env_of_t<Rcvr>>.
  • The default Tuple and Variant describe the objects in which the values can be stored, which is how sync_wait_with_variant and into_variant use them. Pass other templates, such as std::tuple and std::type_identity_t, to see the types exactly as sent, including references and cv-qualifiers.
  • Early proposals of std::execution provided this type through a member alias template value_types<Tuple, Variant> of a sender traits class. Older implementations and articles may show the earlier interface.

Example

Shows the value types of several senders, including one whose value types depend on the environment, and uses them to name the result of sync_wait_with_variant for a sender with two value completions.
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 <charconv>
#include <concepts>
#include <execution>
#include <print>
#include <stdexcept>
#include <stop_token>
#include <string>
#include <string_view>
#include <thread>
#include <tuple>
#include <type_traits>
#include <variant>

namespace ex = std::execution;

using token_env = ex::prop<std::get_stop_token_t, std::inplace_stop_token>;

// By default: a std::variant of std::tuple, one per value completion
static_assert(std::same_as<ex::value_types_of_t<decltype(ex::just(1, 2.5))>,
                           std::variant<std::tuple<int, double>>>);

// The completions, and so the value types, can depend on the environment
using read_token = decltype(ex::read_env(std::get_stop_token));
static_assert(std::same_as<ex::value_types_of_t<read_token>,
                           std::variant<std::tuple<std::never_stop_token>>>);
static_assert(std::same_as<ex::value_types_of_t<read_token, token_env>,
                           std::variant<std::tuple<std::inplace_stop_token>>>);

// The default templates decay the types; other templates see them as sent
int global = 0;
int& get_global() { return global; }
using ref_sender = decltype(ex::just() | ex::then(get_global));
static_assert(std::same_as<ex::value_types_of_t<ref_sender>,
                           std::variant<std::tuple<int>>>);
static_assert(std::same_as<
    ex::value_types_of_t<ref_sender, ex::env<>, std::tuple,
                         std::type_identity_t>,
    std::tuple<int&>>);

// No value completions
using stopped = decltype(ex::just_stopped());
static_assert(std::same_as<ex::value_types_of_t<stopped, ex::env<>, std::tuple,
                                                std::variant>,
                           std::variant<>>);

int to_int(std::string_view s)
{
    int v = 0;
    if (std::from_chars(s.data(), s.data() + s.size(), v).ec != std::errc{})
        throw std::invalid_argument("not a number");
    return v;
}

// Completes with an int, or with a std::string if parsing failed
auto parse(std::string_view s)
{
    return ex::just(s)
         | ex::then(to_int)
         | ex::upon_error([](std::exception_ptr)
           {
               return std::string("not a number");
           });
}

int main()
{
    using result = ex::value_types_of_t<decltype(parse(""))>;
    static_assert(std::variant_size_v<result> == 2);

    for (std::string_view s : {"42", "x42"})
    {
        // sync_wait_with_variant returns std::optional<result>
        result r = std::this_thread::sync_wait_with_variant(parse(s)).value();
        std::visit([s](auto& t)
        {
            std::println("{:<4} -> {}", s, std::get<0>(t));
        }, r);
    }
}

Output:

42   -> 42
x42  -> not a number

See also

obtains the error completion type of a sender
(alias template)[edit]
determines whether the sender supports stopped completion
(variable template)[edit]
lists the ways a sender can complete
(class template) [edit]
obtains the completion signatures of a sender, or their type
(function) [edit]
value completion function indicating successful completion
(customization point object)[edit]
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)[edit]
blocks current thread until the specified sender with possibly multiple completion signatures completes and returns its async result
(customization point object)[edit]