以下代码无法在 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 右值引用来解决问题。谁能分享一下这方面的情况吗?
最佳答案
C++ 中的函数类型可能没有函数类型的返回类型,根据 [dcl.fct]/10:
Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things.
因此,当 FuncType
被推导为函数类型时,所声称的类型 FuncType(Args...)
格式不正确。
关于c++ - result_of 无法推断出返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434675/