我在理解std::result_ofC++ 0x中的需求时遇到了一些麻烦.如果我理解正确,result_of则用于获取调用具有某些类型参数的函数对象的结果类型.例如:
template <typename F, typename Arg>
typename std::result_of<F(Arg)>::type
invoke(F f, Arg a)
{
return f(a);
}
Run Code Online (Sandbox Code Playgroud)
我真的没有看到与以下代码的区别:
template <typename F, typename Arg>
auto invoke(F f, Arg a) -> decltype(f(a)) //uses the f parameter
{
return f(a);
}
Run Code Online (Sandbox Code Playgroud)
要么
template <typename F, typename Arg>
auto invoke(F f, Arg a) -> decltype(F()(a)); //"constructs" an F
{
return f(a);
}
Run Code Online (Sandbox Code Playgroud)
我能用这两种解决方案看到的唯一问题是我们需要:
难道我就在想,唯一的区别decltype和result_of是而第二个不第一个需要表达?
以下无法在gcc和clang上编译
#include <type_traits>
int foo();
int main()
{
using R = std::result_of_t<decltype(foo)()>; // error
}
Run Code Online (Sandbox Code Playgroud)
两个编译器的错误都是处理声明函数返回函数的非法性.但是我没有声明这样的功能 - 我只是想写出它的类型 - 因为那是result_of预期的.这真的还是不合格吗?