result_of无法推导返回类型

result_of fails to deduce return type

本文关键字:返回类型 of result      更新时间:2023-10-16

以下代码在GCC 5.2中编译失败:

template<typename FuncType, typename... ArgTypes>
result_of_t<FuncType(ArgTypes...)> FuncCall(const FuncType &f, ArgTypes&... args)
{
    return f(forward<ArgTypes>(args)...);
}
string SomeFunc()
{
    return "SomeFunc";
}
int main()
{
    cout << FuncCall([](){return "Lambda";}) << "n"; // This call works properly
    cout << FuncCall(SomeFunc) << "n"; // this call fails
}

但如果我更改以下行:

result_of_t<FuncType(ArgTypes...)> FuncCall(const FuncType &f, ArgTypes&... args)

result_of_t<FuncType&&(ArgTypes...)> FuncCall(const FuncType &f, ArgTypes&... args)

然后它正常工作。

我不明白在这种情况下使FuncType重新值引用是如何解决问题的。有人能分享一下这方面的信息吗?

根据[dcl.fct]/10:,C++中的函数类型可能没有作为函数类型的返回类型

函数不应具有类型为数组或函数的返回类型,尽管它们可能具有类型为指针或引用的返回类型。

因此,当FuncType被推导为函数类型时,所谓的类型FuncType(Args...)是不成形的。