Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to defer the generation of a functor?
    text
    copied!<p>I had an interesting idea of creating a type of vector table using functors. However, after further analysis it would seem that this would be a pipe dream due to the fact that it would be prohibitively expensive since the <em>entire</em> table would have to be generated to pass on some of the function parameters from the function that calls the functor. The functor in this case would act as a proxy to manage parameter differences between functions that the functor is calling.</p> <p>Confused yet? :-) Example:</p> <pre><code>#include &lt;functional&gt; class classA {}; void fn1(int a); void fn2(classA&amp; a); void fn_x(int fn_to_call, int a1, classA&amp; a2) { std::function&lt;void()&gt; vtable[] = { [&amp;]{ fn1(a1); }, [&amp;]{ fn2(a2); } }; vtable[fn_to_call](); } void fn_y(int fn_to_call, int a1, classA&amp; a2) { switch(fn_to_call) { case 0: return fn1(a1); case 1: return fn2(a2); } } </code></pre> <p>For brevity, I skipped checks as I'm doing this on a phone.</p> <p>So as I understand it, fn_x would have to create an array of functors upon entering the function, and then call the one required. Whereas fn_y would just jump to the one required.</p> <p>When the number of functions to dispatch is small, this isn't much of a problem, but as the number increases, it becomes worse and worse.</p> <p>If I could somehow defer the creation of the functor objects until it it's called, that would be optimal. Is that possible?</p> <p><strong>Edit</strong> also, how will a compiler handle this? Will it result in multiple calls stack entries (a call to the functor and then a call to the target function) or would it just be as efficient as the switch?</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