Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invoke_result to obtain return type of template member function

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?

like image 591
francesco Avatar asked Dec 10 '19 12:12

francesco


1 Answers

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.

like image 186
463035818_is_not_a_number Avatar answered Oct 23 '22 04:10

463035818_is_not_a_number