Home > Blockchain >  How to replace decltype(f()) with std::invoke_result_t, where f is a lambda with non-type template p
How to replace decltype(f()) with std::invoke_result_t, where f is a lambda with non-type template p

Time:07-02

How to replace the following with std::invoke_result_t?

decltype(f.template operator()<0>())

Here's more context:

template <size_t I, typename Functor>
consteval void apply(Functor&& f)
{
    using ResultType = decltype(f.template operator()<0>())>);
    // ... More stuff ...
    f.template operator()<I>());
}

void test() {
   apply<5>([]<auto I>() {
   });
}

CodePudding user response:

You still need decltype, because invoke_result_t requires the type of the method followed by the type of the class. But here you go:

#include <cstddef>
#include <type_traits>

template <size_t I, typename Functor>
consteval auto apply(Functor&& f)
{
    // using ResultType = decltype(f.template operator()<0>());
    using ResultType = std::invoke_result_t<
        decltype(&std::remove_cvref_t<Functor>::template operator()<0>),
        Functor>;
        // ... More stuff ...
    return ResultType(f.template operator()<I>());
}

void
test()
{
    apply<5>([]<auto I>() {
        });
}
  • Related