Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is really late, but I was trying to figure out how to do this and ran into this question. The environment I'm using can't use C++11 (aka C++0x) or Boost, although both of those are awesome, so I figured I'd post how I figured out to do this without either for the sake of posterity.</p> <p>As UncleBens alluded to, the functional header in the STL has some useful features if you aren't using C++11 or Boost: <a href="http://www.cplusplus.com/reference/std/functional/" rel="nofollow">http://www.cplusplus.com/reference/std/functional/</a></p> <p>This problem is a bit more general than just not wanting to call a second template function. For example, one might want to build a vector of the return type of a functor, in which case calling a second template function might not work.</p> <p>By using some function overloading (to operate on both function pointers and functors) and stl's , we can make this work. Here's an example that prints out the result of a one-argument functor/function argument after declaring the variable explicitly:</p> <pre><code>#include &lt;iostream&gt; #include &lt;functional&gt; using namespace std; // Simple function [pointer] that adds one to its argument int addOne(int n) { return n + 1; } // Simple functor that multiplies its argument by two class timesTwo { public: int operator()(int n) const { return n * 2; } }; // Simple higher-order function: takes a functor f and calls f on n, returning the result // This is your template function in which you want to know the return type of f template &lt;typename Functor&gt; void printResultImpl(Functor f, typename Functor::argument_type n) { typename Functor::result_type r = f(n); cout &lt;&lt; r &lt;&lt; endl; } // Wrapper function for function pointer template &lt;typename Arg, typename Result&gt; void printResult(Result (*f)(Arg), Arg n) { printResultImpl(ptr_fun(f), n); } // Wrapper function for functor (function object) template &lt;typename Functor, typename Arg&gt; void printResult(Functor f, Arg n) { printResultImpl(bind1st(mem_fun(&amp;Functor::operator()), &amp;f), n); } // Prints out 8 then 14 int main() { printResult(addOne, 7); printResult(timesTwo(), 7); } </code></pre> <p>There are a couple limitations to this method: 1. You can't have your function return the result type of the functor (since the wrapper function doesn't know the result type) 2. It relies upon unary_function or binary_function in the stl. As UncleBens demonstrated, it's possible to extend to other types - simply follow the pattern of the declarations in : <a href="http://www.cplusplus.com/reference/std/functional/" rel="nofollow">http://www.cplusplus.com/reference/std/functional/</a></p> <p>But it worked for what I needed; maybe it'll work for someone else.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload