我在理解std::result_of
C++ 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
是而第二个不第一个需要表达?