Created
March 18, 2018 09:01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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