如何获得调用另一类型的函子的返回类型

How to get the return type of calling a functor with another type?

本文关键字:返回类型 类型 何获得 调用      更新时间:2023-10-16

给定类型TArgumentTFunctor,如何使用类型为TArgument的参数调用TFunctor实例的结果类型?

这是我的笨拙,肮脏的解决方案:

template <class TFunctor, typename TArgument>
class ReturnValue
{
public:
     typedef decltype(functor_(arguent_)) Type;
private:
     static TFunctor functor_;
     static TArgument arguent_;
}

但是要使其工作,TFunctorTArgument都需要是默认可构造的。

有更好的方法吗?

typedef decltype(std::declval<TFunctor>()(std::declval<TArgument>())) Type;

或使用std::result_of:

typedef typename std::result_of<TFunctor(TArgument)>::type Type;