Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2018 09:01
#include <iostream>
using namespace std;
void funcA()
{
static int x = 3;
static int y = 10;
// thre WRONG way to capture static variables
auto lmbda = [=]()
{
std::cout << x << '\n';
};
++x;
lmbda();
//the RIGHT way to capture static variables
auto lmbda2 = [yLmbda=y]()
{
std::cout << yLmbda << '\n';
};
++y;
lmbda2();
}
int main() {
// your code goes here
funcA();
return 0;
}
/*Output
4
10
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment