【发布时间】:2019-12-10 12:58:39
【问题描述】:
如何获取模板成员函数的结果类型?
以下最小示例说明了问题。
#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;
}
在 Coliru 上查看 live。
代码没有编译,报错:
main.cpp:11:63: 错误:decltype 无法解析重载地址 功能
11 |使用 default_return_type = std::invoke_result_t;
在模板参数F设置为默认值的情况下,获取B::f类型的正确语法是什么?
【问题讨论】:
标签: c++ templates typetraits invoke-result