确定std :: function的返回类型 [英] Determining return type of std::function

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

问题描述

我正在编写一个接收 std :: function 对象的模板函数(通过调用 std :: bind 带有适当的参数)。
在此函数中,我想确定此函数对象的返回类型。

I'm writing a template function that receives a std::function object (Generated by calling std::bind with the proper arguments). Within this function, I would like to determine the return type of this function object. Is is possible?

事实上,我希望模板函数返回相同的类型。您能想到一种优雅,基于标准的方法来实现此目标吗?

As a matter of fact, I want the template function to return the same type. Can you think of an elegant, standard based, way of achieving this goal?

类似这样的事情:

template <typename T>
T::return_type functionObjWrapper(T functionObject) {
   // ...
   return functionObject();
}

谢谢

推荐答案

您可以使用 decltype 并使用尾随的返回类型:

You can do it using decltype and trailing return type:

template <typename T>
auto functionObjWrapper(T functionObject) -> decltype(functionObject()) {
   // ...
   return functionObject();
}

这篇关于确定std :: function的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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