Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, this signature will not compile:</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>The return type needs to be</p> <pre><code>typename std::result_of&lt;F(Args...)&gt;::type </code></pre> <p>If your compiler implements <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3436.html" rel="nofollow">N3436</a> then this function will not participate in overload resolution when <code>fun(args...)</code> is not a valid expression, but that is not required in C++11 and not implemented by many compilers yet. You will need to implement your own SFINAE check to prevent <code>result_of</code> giving an error when <code>fun(args...)</code> is not valid, or rewrite it without <code>result_of</code></p> <pre><code>template&lt;typename F, typename... Args&gt; auto exec(F fun, Args&amp;&amp;... args) -&gt; decltype(fun(std::forward&lt;Args&gt;(args)...)) { // locks the memory and then executes fun(args...) } </code></pre> <p>Then you can overload it for functions that need the additional parameter passed in:</p> <pre><code>template&lt;typename F, typename... Args&gt; auto exec(F fun, Args&amp;&amp;... args) -&gt; decltype(fun(*this-&gt;shared_memory, std::forward&lt;Args&gt;(args)...)) { // locks the memory and then executes fun(*shared_memory, args...) } </code></pre> <p>When <code>fun(std::forward&lt;Args&gt;(args)...)</code> is not valid the first overload will not participate in overload resolution. When <code>fun(*this-&gt;shared_memory, std::forward&lt;Args&gt;(args)...)</code> is not valid the second overload will not participate in overload resolution. If neither is valid the call will be ill-formed, if both are valid the call will be ambiguous.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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