Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the founding principles of C++ is that you don't pay for what you don't use. That means in this case that contexts where taking a <code>shared_ptr</code> to <code>this</code> is unnecessary shouldn't incur any reference counting overhead. This also means that it shouldn't happen automatically even e.g. as a feature of <code>enable_shared_from_this</code>, since you might want to pass a short-lived lambda to an algorithm (<code>for_each</code>, etc.) in which case the lambda doesn't outlive its scope.</p> <p>I'd suggest adapting the <a href="http://jrb-programming.blogspot.co.uk/2012/11/another-alternative-to-lambda-move.html" rel="nofollow noreferrer">lambda-wrapper pattern</a>; in that case it's used for <code>move</code> capture of a large object (<a href="https://stackoverflow.com/questions/10280937/how-to-capture-stdunique-ptr-by-move-for-a-lambda-in-stdfor-each">How to capture std::unique_ptr &quot;by move&quot; for a lambda in std::for_each</a>), but it can equally be used for shared capture of <code>this</code>:</p> <pre><code>template&lt;typename T, typename F&gt; class shared_this_lambda { std::shared_ptr&lt;T&gt; t; // just for lifetime F f; public: shared_this_lambda(std::shared_ptr&lt;T&gt; t, F f): t(t), f(f) {} template&lt;class... Args&gt; auto operator()(Args &amp;&amp;...args) -&gt; decltype(this-&gt;f(std::forward&lt;Args&gt;(args)...)) { return f(std::forward&lt;Args&gt;(args)...); } }; template&lt;typename T&gt; struct enable_shared_this_lambda { static_assert(std::is_base_of&lt;std::enable_shared_from_this&lt;T&gt;, T&gt;::value, "T must inherit enable_shared_from_this&lt;T&gt;"); template&lt;typename F&gt; auto make_shared_this_lambda(F f) -&gt; shared_this_lambda&lt;T, F&gt; { return shared_this_lambda&lt;T, F&gt;( static_cast&lt;T *&gt;(this)-&gt;shared_from_this(), f); } template&lt;typename F&gt; auto make_shared_this_lambda(F f) const -&gt; shared_this_lambda&lt;const T, F&gt; { return shared_this_lambda&lt;const T, F&gt;( static_cast&lt;const T *&gt;(this)-&gt;shared_from_this(), f); } }; </code></pre> <p>Use by inheriting <code>enable_shared_this_lambda</code> in addition to <code>enable_shared_from_this</code>; you can then explicitly request that any long-lived lambdas take a shared <code>this</code>:</p> <pre><code>doSomethingAsynchronously(make_shared_this_lambda([this] { someMember_ = 42; })); </code></pre>
    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