Note that there are some explanatory texts on larger screens.

plurals
  1. PO"unpacking" a tuple to call a matching function pointer
    primarykey
    data
    text
    <p>I'm trying to store in a <code>std::tuple</code> a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types.</p> <p>I've created a simplified example showing the problem I'm struggling to solve:</p> <pre><code>#include &lt;iostream&gt; #include &lt;tuple&gt; void f(int a, double b, void* c) { std::cout &lt;&lt; a &lt;&lt; ":" &lt;&lt; b &lt;&lt; ":" &lt;&lt; c &lt;&lt; std::endl; } template &lt;typename ...Args&gt; struct save_it_for_later { std::tuple&lt;Args...&gt; params; void (*func)(Args...); void delayed_dispatch() { // How can I "unpack" params to call func? func(std::get&lt;0&gt;(params), std::get&lt;1&gt;(params), std::get&lt;2&gt;(params)); // But I *really* don't want to write 20 versions of dispatch so I'd rather // write something like: func(params...); // Not legal } }; int main() { int a=666; double b = -1.234; void *c = NULL; save_it_for_later&lt;int,double,void*&gt; saved = { std::tuple&lt;int,double,void*&gt;(a,b,c), f}; saved.delayed_dispatch(); } </code></pre> <p>Normally for problems involving <code>std::tuple</code> or variadic templates I'd write another template like <code>template &lt;typename Head, typename ...Tail&gt;</code> to recursively evaluate all of the types one by one, but I can't see a way of doing that for dispatching a function call.</p> <p>The real motivation for this is somewhat more complex and it's mostly just a learning exercise anyway. You can assume that I'm handed the tuple by contract from another interface, so can't be changed but that the desire to unpack it into a function call is mine. This rules out using <code>std::bind</code> as a cheap way to sidestep the underlying problem.</p> <p>What's a clean way of dispatching the call using the <code>std::tuple</code>, or an alternative better way of achieving the same net result of storing/forwarding some values and a function pointer until an arbitrary future point?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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