Function return type deduction in C++03

廉价感情. 提交于 2019-12-03 20:12:31

If limited to function pointers, partial specializations can be used, something like

template<class T> struct func_ptr_result {};

template<class R>
struct func_ptr_result<R (*)()> {
    typedef R type; 
};

template<class R, class Arg1>
struct func_ptr_result<R (*)(Arg1)> {
    typedef R type; 
}; 

// etc for > 1 arguments

template<typename F, typename A>
typename func_ptr_result<F>::type call(F function, A arg) {
    return function(arg);
}

In the more general case (arbitrary functors), it isn't possible without compiler support or cooperation from the type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!