I am trying to learn the concept of closures in C++. I have the following code.
std::function<void(void)> closureWrapper2()
{
int x = 10;
return [&x](){x += 1; std::cout << "Value in the closure: " << x << std::endl;};
}
int main()
{
std::function<void(void)> func2 = closureWrapper2();
// std::cout << "---" << std::endl;
func2();
func2();
func2();
}
Output
Value in the closure: 11
Value in the closure: 12
Value in the closure: 13
Now if I uncomment the cout
statement I get the following output.
Output
---
Value in the closure: 32765
Value in the closure: 32766
Value in the closure: 32767
Can anyone please explain why printing something before the function calls changes the output?
Aucun commentaire:
Enregistrer un commentaire