我在理解其需求时遇到了一些困难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);
}
我真的看不出以下代码有什么区别:
template <typename F, typename Arg>
auto invoke(F f, Arg a) -> decltype(f(a)) //uses the f parameter
{
return f(a);
}
or
template <typename F, typename Arg>
auto invoke(F f, Arg a) -> decltype(F()(a)); //"constructs" an F
{
return f(a);
}
我认为这两个解决方案的唯一问题是我们需要:
- 有一个仿函数的实例,可以在传递给 decltype 的表达式中使用它。
- 知道函子的定义构造函数。
我认为两者之间唯一的区别是对的吗?decltype
and result_of
是不是第一个需要表达式而第二个不需要?
result_of
was 在Boost中引入 http://www.boost.org/doc/libs/1_42_0/boost/utility/result_of.hpp, 进而包含在 TR1 中 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1454.html,最后在 C++0x 中。所以result_of
具有向后兼容的优点(使用合适的库)。
decltype
是 C++0x 中的全新事物,不仅限于函数的返回类型,而且是一种语言功能。
无论如何,在 gcc 4.5 上,result_of
是根据以下方面实施的decltype
:
template<typename _Signature>
class result_of;
template<typename _Functor, typename... _ArgTypes>
struct result_of<_Functor(_ArgTypes...)>
{
typedef
decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )
type;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)