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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:18:17+00:00 2026-06-12T05:18:17+00:00

I have trouble understanding lambda functions and the mechanism to capture variables, so I

  • 0

I have trouble understanding lambda functions and the mechanism to capture variables, so I did a little testing and arrived at really weird conclusion. that is:

class ClassA
{
public:
  std::function<void()> lambda;
  void DoYourStuff()
  {
    int x;
    x = 1;
    lambda = [=] () { printf("A %d\n", x);};
    lambda();
    x = 2;
    lambda(); 
  }
};

class ClassB
{
public:
  std::function<void()> lambda;
  int x;
  void DoYourStuff()
  {
    x = 1;
    lambda = [=] () { printf("B %d\n", x);};
    lambda();
    x = 2;
    lambda(); 
  }
};

Note: only difference is in placement of x variable;
ClassA and ClassB’s functions have different output!

A 1
A 1
B 1
B 2

So my questions are:

  1. Is this the desired behaviour?
  2. if i used [&] instead of [=] in ClassA, would those lambdas be identical?
  3. Is there some general rule as to when [=] actually makes copy?
  4. When exactly is the capturing of variables by lambdas supposed to happen?
  5. Can I force lambda to do a re-capture of variables?

Thanks

  • 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-12T05:18:18+00:00Added an answer on June 12, 2026 at 5:18 am

    The first captures the local variable x by value; so it prints 1 both times, since its copy hasn’t changed.

    The second captures the local quasi-variable this, not the member variable x. So the body is equivialent to printf("%d\n", this->x);, and will print the new value of 2 after you change x.

    If you were to explicitly capture x (i.e. [x](){...}), then you will see the same result as the first.

    Is this the desired behaviour?

    It’s the behaviour specified by the language standard.

    if i used [&] instead of [=] in ClassA, would those lambdas be identical?

    No, but both would produce the same output. The first would capture the local x by reference, so you will see the change to it.

    Is there some general rule as to when [=] actually makes copy?

    Yes, it copies the variable when the lambda is created.

    When exactly is the capturing of variables by lambdas supposed to happen?

    When the lambda is created.

    Can I force lambda to do a re-capture of variables?

    No. Once it’s captured by value, it has its own copy of the variable, with no way to access the original. If you need to see changes to the original, then capture by reference (and take care with object lifetimes).

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

Sidebar

Related Questions

I have little trouble understanding why variables aren't working properly in the following code.
I'm having trouble understanding using D3 events and dispatch functions. I have a chart
The idea of MVC itself seems clear to me but I have trouble understanding
I have a 64-tap FIR filter whose output format I am having trouble understanding.
We have trouble implementing a nice session mechanism in JSP. One of our requirement
I have trouble understanding the following thing: in my localized application I have an
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
Im new to obj-c and have trouble understanding the function autorelease. could someone explain
I have some trouble understanding the need for std::result_of in C++0x. If I understood
I have trouble understanding this chunk of code: let sieve (p:xs) = p :

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.