如何获得调用另一种类型的仿函数的返回类型?

Mar*_*dik 0 c++ templates c++11

鉴于类型TArgumentTFunctor,我怎么能找出导致调用的一个实例的类型TFunctor与类型的参数TArgument

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

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

private:
     static TFunctor functor_;
     static TArgument arguent_;
}
Run Code Online (Sandbox Code Playgroud)

但它的工作,既TFunctorTArgument需要是缺省构造的.

有没有更好的办法?

Sim*_*ple 7

typedef decltype(std::declval<TFunctor>()(std::declval<TArgument>())) Type;
Run Code Online (Sandbox Code Playgroud)

或使用std::result_of:

typedef typename std::result_of<TFunctor(TArgument)>::type Type;
Run Code Online (Sandbox Code Playgroud)