jeudi 31 mars 2016

Lambda-specific variable

Is there any way to create variable that will be unique for some lambda function and will last between launches of lambda?
More careful description: I want lambda with variable initialized to some value, and that variable should last between launches:

std::function<void(void)> a=[]()
{
    /*here we declare variable X and initialize it to 0*/;
    std::cout<<X++;
};
a();a();

So this should print out 01

But also I need to be sure that "X" is unique for "a", so after previous part this

std::function<void(void)> b=a;
b();b();

should print out 01.

I tried using static variables, but they are shared between copies(so these two parts print out 0123).

So, is there any way to do it?

Aucun commentaire:

Enregistrer un commentaire