Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:33:15+00:00 2026-06-16T02:33:15+00:00

I’ve come to C++11 from an Objective-C background, and one thing I’m struggling to

  • 0

I’ve come to C++11 from an Objective-C background, and one thing I’m struggling to come to terms with is the different capturing semantics of C++11 lambdas vs Objective-C “blocks”. (See here for a comparison).

In Objective-C, like C++, the self/this pointer is implicitly captured if you refer to a member variable. But because all objects in Objective-C are effectively “shared pointers”, to use the C++ terminology, you can do this:

doSomethingAsynchronously(^{
  someMember_ = 42;
});

… and you’re guaranteed that the object whose member you’re accessing will be alive when the block executes. You don’t have to think about it. The equivalent in C++ seems to be something like:

// I'm assuming here that `this` derives from std::enable_shared_from_this and 
// is already owned by some shared_ptr.
auto strongThis = shared_from_this();

doSomethingAsynchronously([strongThis, this] {
  someMember_ = 42;   // safe, as the lambda holds a reference to this
                      // via shared_ptr.
});

Here, you need to remember to capture the shared_ptr in addition to the this pointer. Is there some less error-prone way of achieving this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:33:16+00:00Added an answer on June 16, 2026 at 2:33 am

    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 shared_ptr to this 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 enable_shared_from_this, since you might want to pass a short-lived lambda to an algorithm (for_each, etc.) in which case the lambda doesn’t outlive its scope.

    I’d suggest adapting the lambda-wrapper pattern; in that case it’s used for move capture of a large object (How to capture std::unique_ptr "by move" for a lambda in std::for_each), but it can equally be used for shared capture of this:

    template<typename T, typename F>
    class shared_this_lambda {
      std::shared_ptr<T> t;  // just for lifetime
      F f;
    public:
      shared_this_lambda(std::shared_ptr<T> t, F f): t(t), f(f) {}
      template<class... Args>
      auto operator()(Args &&...args)
      -> decltype(this->f(std::forward<Args>(args)...)) {
        return f(std::forward<Args>(args)...);
      }
    };
    
    template<typename T>
    struct enable_shared_this_lambda {
      static_assert(std::is_base_of<std::enable_shared_from_this<T>, T>::value,
        "T must inherit enable_shared_from_this<T>");
      template<typename F>
      auto make_shared_this_lambda(F f) -> shared_this_lambda<T, F> {
        return shared_this_lambda<T, F>(
          static_cast<T *>(this)->shared_from_this(), f);
      }
      template<typename F>
      auto make_shared_this_lambda(F f) const -> shared_this_lambda<const T, F> {
        return shared_this_lambda<const T, F>(
          static_cast<const T *>(this)->shared_from_this(), f);
      }
    };
    

    Use by inheriting enable_shared_this_lambda in addition to enable_shared_from_this; you can then explicitly request that any long-lived lambdas take a shared this:

    doSomethingAsynchronously(make_shared_this_lambda([this] {
      someMember_ = 42;
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.