Note that there are some explanatory texts on larger screens.

plurals
  1. POreturning a std::function wrapped lambda that invokes a specified pointer to member function
    primarykey
    data
    text
    <p>I have a case where I can pass a lambda to std::sort, and I can also supply the predicate by calling a function that returns a std::function which wraps this same lambda, but, if I try to call a similar function which allows me to specify a pointer to member function, this compiles but fails at run time.</p> <p>This works:</p> <pre><code>std::sort(myContainer.begin(), myContainer.end(), [&amp;](type lhs, type rhs) { return MyMemberFunction(lhs, rhs); }); </code></pre> <p>And this works:</p> <pre><code>std::function&lt;bool(type,type)&gt; ReturnPred() { std::function&lt;bool(type,type)&gt; pred = [&amp;](type lhs, type rhs) { return MyMemberFunction(lhs, rhs); }; return pred; } std::sort(myContainer.begin(), myContainer.end(), ReturnPred()); </code></pre> <p>But this does not work:</p> <pre><code>std::function&lt;bool(type,type)&gt; ReturnGeneralPred( bool(MyClass::Func*)(type lhs, type rhs)) { std::function&lt;bool(type,type)&gt; pred = [&amp;](type lhs, type rhs) { return (this-&gt;*Func)(lhs, rhs); }; return pred; } std::function&lt;bool(type,type)&gt; ReturnThisPred() { return ReturnGeneralPred(&amp;MyClass::MyMemberFunction); } std::sort(myContainer.begin(), myContainer.end(), ReturnThisPred()); </code></pre> <p>When I try to do it this last generic way and I step through the debugger, when std::sort calls the predicate, it steps into what I've call ReturnGeneralPred above, and Func seems to be undefined, as though it was a local variable that went out of scope.</p> <p>At the moment I can get the same functionality by losing some genericness, but I was wondering if maybe there's a way to accomplish what I was trying to do.</p>
    singulars
    1. This table or related slice is empty.
    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