Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit</strong>: This is not really a solution; it's more of a cheat. But I think it does what is needed.</p> <pre><code>#define INVOKE(hof, func, arg) \ hof([](const decltype(arg)&amp; arg_){return func(arg_);}, arg) </code></pre> <p>Example:</p> <pre><code>// This function mimics the signature of QtCollector::run for testing template &lt;typename Functor, typename Arg1&gt; auto QtConcurrent_run(Functor functor, const Arg1 &amp;arg1) -&gt; decltype(functor(arg1)) { return functor(arg1); } #include &lt;iostream&gt; int f(int x) { std::cout &lt;&lt; "int" &lt;&lt; " " &lt;&lt; x &lt;&lt; std::endl; return x; } double f(double x) { std::cout &lt;&lt; "double" &lt;&lt; " " &lt;&lt; x &lt;&lt; std::endl; return x; } int main() { INVOKE(QtConcurrent_run, f, 3); INVOKE(QtConcurrent_run, f, 3.14); INVOKE(QtConcurrent_run, f, '3'); return 0; } </code></pre> <p>See it <a href="http://ideone.com/CTSPJW" rel="nofollow">here</a> on ideone.</p> <p>The original answer follows, for historical purposes and a bit of explanation.</p> <hr> <p>Just for clarity, because this is an interesting question but perhaps it is more important for you to get your project moving, is there a reason why you don't want to just wrap the various function overrides into a functor struct, and pass the functor struct directly to QtConcurrent::run, which will happily accept such a thing?</p> <p>If all the definitions of the function were in a single class, then there would be no problem:</p> <pre><code>struct f_collector { ReturnType1 f(ArgType1 arg); ReturnType2 f(ArgType2 arg); ReturnType3 f(ArgType3 arg); // ... // Make it a functor: template&lt;typename Argtype&gt; auto operator()(const Argtype&amp; arg) -&gt; decltype(f(arg)) { return f(arg); } } </code></pre> <p>Then you could call <code>QtConcurrent::run(f_collector(), argument)</code> and it would Just Work (unless you need perfect forwarding, but that's a minor detail).</p> <p>So I had the following idea, which was to build a functor like the above on the fly, which basically means feeding it a lambda expression. The lambda itself is easy enough boilerplate; easy enough to make a macro out of it:</p> <pre><code>// This is the functor template&lt;typename Arg, typename Func&gt; struct wrapper { wrapper(Func f) : f(f) {} const Func f; auto operator()(Arg arg) const -&gt; decltype(f(arg)) {return f(arg);} }; // As usual, a make_* function, because you can't template deduce a constructor template&lt;typename Arg, typename Func&gt; wrapper&lt;Arg, Func&gt; make_wrapper(Func f) { return wrapper&lt;Arg, Func&gt;(f); } // Boilerplate inside a macro #define INVOKE(hof,func,arg) \ hof(make_wrapper&lt;decltype(arg)&gt;( [](const decltype(arg)&amp; arg_) { \ return func(arg_); \ }), \ arg) // The above was ugly, but it's easy to use. For testing, I define // this with a similar signature to QtConcurrent::run template &lt;typename Functor, typename Arg1&gt; auto QtConcurrent_run(Functor functor, const Arg1 &amp;arg1) -&gt; decltype(functor(arg1)) { return functor(arg1); } #include &lt;iostream&gt; int f(int x) { std::cout &lt;&lt; "int" &lt;&lt; " " &lt;&lt; x &lt;&lt; std::endl; return x; } double f(double x) { std::cout &lt;&lt; "double" &lt;&lt; " " &lt;&lt; x &lt;&lt; std::endl; return x; } int main() { INVOKE(QtConcurrent_run, f, 3); INVOKE(QtConcurrent_run, f, 3.14); INVOKE(QtConcurrent_run, f, '3'); return 0; } </code></pre> <p>But then I remembered that lambda's, along with their other virtues, can be automatically converted to function pointers as long as they have no captures. And this lambda has no captures, because the only external symbol is the function itself, and that is not an object with automatic storage class. So, I think the bottom line is, you can actually do this with a bit of boilerplate:</p> <pre><code>#define INVOKE(hof, func, arg) \ hof([](const decltype(arg)&amp; arg_){return func(arg_);}, arg); </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