相关疑难解决方法(0)

std :: result_of和decltype之间的区别

我在理解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的表达式中使用它.
  • 知道仿函数的定义构造函数.

难道我就在想,唯一的区别decltyperesult_of是而第二个不第一个需要表达?

c++ result-of decltype c++11

97
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

decltype ×1

result-of ×1