c++ - result_of 无法推断出返回类型

标签 c++ templates types type-traits

以下代码无法在 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/

相关文章:

c++链表反转 - 指针不前进

c++ - 继承和成员函数模板重载

c++ - 错误 : aggregate ‘food_type a’ has incomplete type and cannot be defined

c++ - std::initializer_list 的类型推断

types - 为什么函数参数在 Ocaml 中不能是多态的?

c++ - C++中类与函数的模板类型推导?

javascript - Typescript:忽略与基本类型并行的自定义类型

c++ - Cygwin 控制台转储大量神秘输出然后键入 1;2;6;22c

c++ - 多维数组初始化: Any benefit from Threading?

c++ - 无限循环与无限递归。两者都未定义吗?