Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a constructor create a different lambda each time when called?

I have this simple code:

#include <iostream>
#include <functional>

class a {
public:
    a() {
        func = [] {
            static int i = 0;
            i++;
            std::cout << i << std::endl;
        };
    }
    std::function<void()> func;
};

int main()
{
    a a1;
    a1.func();
    a a2;
    a2.func();
}

I expected an output like this:

1
1

But instead it was:

1
2

Then I checked the memory addresses of the lambdas in the two instances of a and they were the same. This means that the lambda is created once and then used in all instances. What I am looking for is to make the constructor create a different lambda every time when it gets called. I turned off the compiler optimizations but there was no effect.

INFO: I am using MSVC.

like image 495
Ivan Venkov Avatar asked May 06 '26 08:05

Ivan Venkov


1 Answers

Conceptually, you do have separate instances of the lambda. What you have witnessed might be a compiler optimization, but the actual issue is that static makes i owned by the function body itself, and thus shared by all instances of your lambda.

What you want instead is to make i a member of each lambda instance, which you can do with an extended capture list:

func = [i = 0]() mutable {
    i++;
    std::cout << i << std::endl;
};
like image 162
Quentin Avatar answered May 08 '26 22:05

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!