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 8650201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:45:38+00:00 2026-06-12T13:45:38+00:00

I have a decent amount of code that relies on capturing a shared_from_this() when

  • 0

I have a decent amount of code that relies on capturing a shared_from_this() when using a lambda expression as a callback to ensure that my instance stays alive:

std::shared_ptr<Thing> self = shared_from_this();
auto doSomething = [this, self] ()
{
    // various statements, none of which reference self, but do use this
}

So the question is: Since I am not referencing self inside the lambda body, is a conformant compiler allowed to optimize the capture away?


Consider the following program:

#include <functional>
#include <iostream>
#include <memory>

std::function<void ()> gFunc;

struct S : std::enable_shared_from_this<S>
{
    void putGlobal()
    {
        auto self = shared_from_this();
        gFunc = [self] { };
    }
};

int main()
{
    auto x = std::make_shared<S>();
    std::cout << x.use_count() << std::endl;
    x->putGlobal();
    std::cout << x.use_count() << std::endl;
}

The output is:

1
2

This indicates that g++-4.7.1 does not optimize the capture away (nor does clang-3.1).

  • 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-12T13:45:39+00:00Added an answer on June 12, 2026 at 1:45 pm

    The standard guarantees that captured values are not optimized away (per §5.1.2/14):

    An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly
    captured
    with a capture that does not include an &. For each entity captured by copy, an unnamed non-
    static data member is declared in the closure type.
    The declaration order of these members is unspecified.
    The type of such a data member is the type of the corresponding captured entity if the entity is not a
    reference to an object, or the referenced type otherwise.

    So, self is copied into the closure on evaluation (per §5.1.2/21):

    When the lambda-expression is evaluated, the entities that are captured by copy are used to direct-initialize
    each corresponding non-static data member of the resulting closure object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would usually include a decent amount of code along with what I have
I have a jsfiddle set up because there is a decent amount of code
I'm using Core Data to cache a decent amount of information, and I have
I have been unable to find any decent documentation on this function. The code
I have heard people state that Code Generators and T4 templates should not be
Here's my particular situation... I have a decent amount of experience with webforms, and
There have been a decent amount of questions about mysql spatial datatypes, however mine
I have been using the code below in web.config as a simple way to
Over the course of a few projects, I have written a decent amount of
I have a set up where it is returning a possibly decent amount of

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.