invoke_result to obtain return type of template member function

ぃ、小莉子 提交于 2020-01-03 11:02:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/59267797/invoke-result-to-obtain-return-type-of-template-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!