如何推导函子的返回值的类型? [英] How to deduce the type of the functor's return value?

查看:112
本文介绍了如何推导函子的返回值的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个接受2个值和函数或lambda的模板函数。
该函数用这些值调用函子并返回结果。

 模板< typename T,typename Fn> 
_ReturnTypeOfPred_ Apply(T x,T y,Fn fn)
{
return fn(x,y);问题:如何定义的返回类型?
}


$ b <应用
等于 Fn 的返回类型?它不一定等于 T ,如函子

的示例

  template< typename T> 
auto Sum(T x,T y) - > decltype(x + y)
{
return x + y;
}

更新

第一个例子过于简单。
这个工作吗?

 模板< typename TContainer,typename Fn> 
auto Apply(const TContainer& x,const TContainer& y,Fn fn) - > decltype(fn(x.front(),y.front()))
{
return fn(x.front(),y.front());
}

如果我重复 code>表达式在 decltype 的返回类型?是否有更优雅的方式?

解决方案

只需使用 decltype

  template< typename T,typename Fn> 
auto Apply(T x,T y,Fn fn) - > decltype(fn(x,y))
{
return fn(x,y);
}

您可以使用 std :: result_of std :: result_of和decltype 之间的差异),但为什么

 模板< typename T,typename Fn> 
typename std :: result_of< Fn,T,T> :: type Apply(T x,T y,Fn fn)
{
return fn(x,y);
}






问题:对于函数

  auto fn(< args>) - > < return-type> {return< expression> ;; } 

return-type c $ c> decltype(< expression>)将通常工作,但可能容易出错。例如,考虑:

  auto f(char c) - > decltype(std :: string()+ = c){return std :: string()+ = c; } 

这里 decltype $ c> std :: string& ,你的函数将返回一个本地的左值引用!这将必须更改为:

  auto f(char c) - > std :: remove_reference< decltype(std :: string()+ = c)> :: type {
return std :: string()+ = c;
}



在其他情况下,< expression& code>可能产生一个值,由于是eg不可返回不可复制,包含lambda等。


I would like to write a template function which accepts 2 values and a functor or a lambda. The function calls the functor with those values and returns the result.

template <typename T, typename Fn> 
_ReturnTypeOfPred_ Apply(T x, T y, Fn fn)
  {
  return fn(x, y);
  }

Question: How can I define the return type of Apply to become equal to the return type of Fn? It is not necessarily equal to T, as in this example of the functor

template <typename T> 
auto Sum(T x, T y) -> decltype(x+y)
  {
  return x+y;
  }

Update

The 1st example was oversimplified. Should this one work?

template <typename TContainer, typename Fn> 
auto Apply(const TContainer& x, const TContainer& y, Fn fn) -> decltype(fn(x.front(), y.front()))
  {
  return fn(x.front(), y.front());
  }

Would it always work if I repeat return expression in decltype of the return type? Is there a more elegant way?

解决方案

You're nearly there; just use decltype:

template <typename T, typename Fn> 
auto Apply(T x, T y, Fn fn) -> decltype(fn(x, y))
{
  return fn(x, y);
}

You could use std::result_of (Difference between std::result_of and decltype) but why bother?

template <typename T, typename Fn> 
typename std::result_of<Fn, T, T>::type Apply(T x, T y, Fn fn)
{
  return fn(x, y);
}


Regarding the follow-up question: for a function

auto fn(<args>) -> <return-type> { return <expression>; }

substituting return-type with decltype(<expression>) will usually work, but can be error prone. For example, consider:

auto f(char c) -> decltype(std::string() += c) { return std::string() += c; }

Here decltype will yield std::string & and your function will return an lvalue reference to a local! This would have to be changed to:

auto f(char c) -> std::remove_reference<decltype(std::string() += c)>::type {
    return std::string() += c;
}

In other cases, <expression> could yield a value that is not returnable for reason of being e.g. noncopyable, containing a lambda, etc.

这篇关于如何推导函子的返回值的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆