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

  • Home
  • SEARCH
  • 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 6713429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:22:47+00:00 2026-05-26T08:22:47+00:00

Consider this code: #include <memory> #include <iostream> class A { public: A(int data) :

  • 0

Consider this code:

#include <memory>
#include <iostream>

class A
{
public:
    A(int data) : data_(data)
    { std::cout << "A(" << data_ << ")" << std::endl; }
    ~A() { std::cout << "~A()" << std::endl; }
    void a() { std::cout << data_ << std::endl; }
private:
    int data_;
};

class B
{
public:
    B(): a_(new A(13)) { std::cout << "B()" << std::endl; }
    ~B() { std::cout << "~B()" << std::endl; }
    std::function<void()> getf()
    {
        return [=]() { a_->a(); };
    }
private:
    std::shared_ptr<A> a_;
};

int main()
{
    std::function<void()> f;
    {
        B b;
        f = b.getf();
    }
    f();
    return 0;
}

Here it looks like I’m capturing a_ shared pointer by value, but when I run it on Linux (GCC 4.6.1), this is printed:

A(13)
B()
~B()
~A()
0

Obviously, 0 is wrong, because A is already destroyed. It looks like this is actually captured and is used to look up this->a_. My suspicion is confirmed when I change the capture list from [=] to [=,a_]. Then the correct output is printed and the lifetime of the objects is as expected:

A(13)
B()
~B()
13
~A()

The question:

Is this behaviour specified by the standard, implementation-defined, or undefined? Or I’m crazy and it’s something entirely different?

  • 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-05-26T08:22:47+00:00Added an answer on May 26, 2026 at 8:22 am

    Is this behaviour specified by the standard

    Yes. Capturing member variables is always done via capturing this; it is the only way to access a member variable. In the scope of a member function a_ is equivalent to (*this).a_. This is true in Lambdas as well.

    Therefore, if you use this (implicitly or explicitly), then you must ensure that the object remains alive while the lambda instance is around.

    If you want to capture it by value, you must explicitly do so:

    std::function<void()> getf()
    {
        auto varA = a_;
        return [=]() { varA->a(); };
    }
    

    If you need a spec quote:

    The lambda-expression’s compound-statement yields the function-body ( 8.4 ) of the function call operator, but for purposes of name lookup (3.4), determining the type and value of this (9.3.2) and transforming id-expressions referring to non-static class members into class member access expressions using (*this) ( 9.3.1 ),
    the compound-statement is considered in the context of the lambda-expression.

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

Sidebar

Related Questions

Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Consider this: #include <iostream> using namespace std; class A{ protected: void some_function(int params) {
Consider this code: #include <iostream> template<class C> struct time { }; int main() {
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Consider the code : #include <stdio.h> class Base { public: virtual void gogo(int a){
Consider this code: #include <iostream> using namespace std; int main() { bool lock =
Consider this code: #include <vector> void Example() { std::vector<TCHAR*> list; TCHAR* pLine = new
Consider this code: #include <stdio.h> #define N 5 void printMatrix(int (*matrix)[N],int n) { int
Consider this C code: #include stdio.h int main(void) { int count = 5; unsigned

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.