Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Until the auto keyword makes it really trivial, one can rely on the convention that function objects provide certain typedef's, including result_type. One just needs to wrap a plain function into a function object. The standard "functional" header provides some (pointer_to_unary_function/pointer_to_binary_function in addition to a couple of member function wrappers). When these are not enough, boost library provides more powerful wrappers. </p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/function.hpp&gt; #include &lt;boost/bind.hpp&gt; int answer() { return 42; } template &lt;class T&gt; T unknown() { return T(); } template &lt;class Function&gt; void foobar(Function unknown) { typename Function::result_type x = unknown(); std::cout &lt;&lt; x &lt;&lt; '\n'; } int main() { foobar(boost::function&lt;int()&gt;(answer)); foobar(boost::bind(unknown&lt;int&gt;)); } </code></pre> <p>And below's an example, how you might add a pointer_to_zeronary_function. (I suppose the helper function that helps you create one, ptr_fun, might be added to the standard namespace as well as an overload(?)</p> <pre><code>template &lt;class T&gt; class pointer_to_zeronary_function { typedef T(*zeronary_func)(); zeronary_func func; public: typedef T result_type; pointer_to_zeronary_function(zeronary_func f): func(f) {} T operator()() const { return func(); } }; template &lt;class T&gt; pointer_to_zeronary_function&lt;T&gt; ptr_fun(T(*f)()) { return pointer_to_zeronary_function&lt;T&gt;(f); } ... //usage: foobar(ptr_fun(answer)); </code></pre>
 

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