8

How can I obtain the result type of a template member function?

The following minimal example illustrates the problem.

#include <type_traits>

template <typename U>
struct A {
};

struct B {
   template <typename F = int>
   A<F> f() { return A<F>{}; }

   using default_return_type = std::invoke_result_t<decltype(f)>;
};

int main()
{
    B::default_return_type x{};

    return 0;
}

See it live on Coliru.

The code does not compile, giving error:

main.cpp:11:63: error: decltype cannot resolve address of overloaded function

11 | using default_return_type = std::invoke_result_t;

What is the correct syntax to obtain the type of B::f with the template parameter F set to default?

francesco
  • 7,189
  • 7
  • 22
  • 49

1 Answers1

7

You can get the return type like this:

using default_return_type = decltype(std::declval<B>().f());

Complete example:

#include <type_traits>
#include <iostream>
template <typename U>
struct A {
};

struct B {
   template <typename F = int>
   A<F> f() { return A<F>{}; }

   using default_return_type = decltype(std::declval<B>().f());
};

int main()
{
    B::default_return_type x{};
    std::cout << std::is_same< B::default_return_type, A<int>>::value;
}

PS: It seems like clang and older gcc versions are not happy with B being an incomplete type and calling f. As a workaround, moving the using out of the class should help.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185