Question

How can I write a std::apply on a std::expected?

In C++23, given:

expected<A, string> getA(const X& x);
expected<B, string> getB(const Y& y);

C compute_all(const A& a, const B& b);

Is there a way to avoid a classic style check like:

auto a_ret = getA(x);
if (!a_ret)
  return a_ret.error();

auto b_ret = getB(y);
if (!b_ret)
  return b_ret.error();

C final_ret = compute_all(*a_ret, *b_ret);

and write something like

expected<C, string> final_ret = magic_apply(compute_all, getA(x), getB(y))

This is an idea of implementation of magic_apply but I need something more generic (perhaps using variadic templates), that allows passing to compute_all some parameter that is not an std::expected.

template<typename Func, typename A, typename B, typename Err>
auto magic_apply(Func func, const std::expected<A, Err>& a, const std::expected<B, Err>& b)
->
std::expected<decltype(func(a.value(), b.value())), Err>
{
    if(!a) {
        return std::unexpected{ a.error() };
    }
    if(!b) {
        return std::unexpected{ b.error() };
    }
    return func(a.value(), b.value());
}

Does some feature already exist in the language I could use to write this?

 15  1519  15
1 Jan 1970