确定std :: function的返回类型
问题描述:
我正在编写一个接收 std :: function
对象的模板函数(通过调用 std :: bind $ c $生成c>带有适当的参数)。
在此函数中,我想确定此函数对象的返回类型。
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();
}