Note that there are some explanatory texts on larger screens.

plurals
  1. POPolymorphic signature of function as template argument (using lambdas)
    primarykey
    data
    text
    <p>I'm trying hard for some hours and didn't manage to get this working.</p> <p>I have a templated class spinlock:</p> <pre><code>template&lt;typename T&gt; class spinlock { // ... volatile T *shared_memory; }; </code></pre> <p>I'm trying to create something like this:</p> <pre><code> // inside spinlock class template&lt;typename F, typename... Ars&gt; std::result_of(F(Args...)) exec(F fun, Args&amp;&amp;... args) { // locks the memory and then executes fun(args...) }; </code></pre> <p>But I'm trying to use a polymorphic function so that I can do this:</p> <pre><code>spinlock&lt;int&gt; spin; int a = spin.exec([]() { return 10; }); int b = spin.exec([](int x) { return x; }, 10); // argument here, passed as x // If the signature matches the given arguments to exec() plus // the shared variable, call it int c = spin.exec([](volatile int &amp;shared) { return shared; }); // no extra arguments, shared becomes the // variable inside the spinlock class, I need to make // a function call that matches this as well // Same thing, matching the signature int d = spin.exec([](volatile int &amp;shared, int x) { return shared + x; }, 10); // extra argument, passed as x... should match too // Here, there would be an error int d = spin.exec([](volatile int &amp;shared, int x) { return shared + x; }); // since no extra argument was given </code></pre> <p>Basically, I'm trying to make an <code>exec</code> function that accepts <code>F(Args...)</code> or <code>F(volatile T &amp;, Args...)</code> as an argument.</p> <p>But I can't manage to make automatic detection of types. How could I accomplish that?</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.
    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