可调用对象的结果类型的推导

Deduction of result type of callable

本文关键字:类型 结果 对象 调用      更新时间:2023-10-16

我尝试推断可调用模板参数的类型,不幸的是没有成功:

template<typename callable, typename T_out >
class A
{};
template<typename callable>
auto make_A( callable f )
{
  return A<callable, typename std::result_of_t<callable> >{ f };
}
int main()
{
  make_A( []( float f ){ return f;} );
}
上面的代码会导致以下错误:
error: implicit instantiation of undefined template 'std::__1::result_of<(lambda at /Users/arirasch/WWU/dev/xcode/tests/tests/main.cpp:31:11)>'
template <class _Tp> using result_of_t = typename result_of<_Tp>::type;

有谁知道怎么修理它吗?

提前感谢。

您需要将参数列表传递给std::result_of,否则无法告诉返回类型(毕竟operator()可以重载)。

return A<callable, std::result_of_t<callable(float)> >{ f }

(假设A<callable, std::result_of_t<callable(float)>可以用f构造,但本例并非如此)