Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Define few typedefs as:</p> <pre><code>typedef typename DataStructure::reference reference; typedef typename DataStructure::const_reference const_reference; typedef reference (DataStructure::*top_type)(); //non-const version typedef const_reference (DataStructure::*ctop_type)() const;//const version </code></pre> <p>then use whatever you need it. </p> <p>In the posted code, you need const version, so write this:</p> <pre><code>mem_fun( static_cast&lt;ctop_type&gt;(&amp;DataStructure::top )) </code></pre> <p>The casting helps compiler to choose the version you intend to use.</p> <p>By the way, in C++11, <code>std::mem_fun</code> is <strong>deprecated</strong>. And it has added another function called <code>std::mem_fn</code>. Notice the difference in the spelling. See this topic for details:</p> <ul> <li><a href="https://stackoverflow.com/questions/11680807/stdmem-fun-vs-stdmem-fn">std::mem_fun vs std::mem_fn</a></li> </ul> <hr> <p>What you need is called <code>std::bind</code>, not <code>std::mem_fun</code> (or <code>std::mem_fn</code>) :</p> <p>It works now:</p> <ul> <li><a href="http://stacked-crooked.com/view?id=88f50b5f7e3e4d1e94de019640326075-18aa934a8d82d638dde2147aa94cac94" rel="nofollow noreferrer">Online Demo</a></li> </ul> <p>The way you're calling <code>getter</code> suggests that you need <code>std::bind</code> because you're invoking <code>getter</code> like this:</p> <pre><code>auto const current = getter(); </code></pre> <p>That is an error if <code>getter</code> is an object returned from <code>std::mem_fun</code>, in which case it should be invoked like this:</p> <pre><code>auto const current = getter(&amp;ds); </code></pre> <p>See this demo:</p> <ul> <li><a href="http://stacked-crooked.com/view?id=d764365d5460991ace4d8f9ebe6b685f-18aa934a8d82d638dde2147aa94cac94" rel="nofollow noreferrer">Online Demo</a></li> </ul> <p>Or if you simply pass the pointer-to-member, then you invoke as:</p> <pre><code>auto const current = (ds.*getter)(); </code></pre> <p>See : <a href="http://stacked-crooked.com/view?id=aa7c828018df318ab3b6d53b75ecb538-18aa934a8d82d638dde2147aa94cac94" rel="nofollow noreferrer">Online Demo</a></p> <p>Compare all three. See the differences!</p> <p>Hope that helps.</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