Saturday, 15 May 2010

function - How to use lambda as lexical scope in C++ -



function - How to use lambda as lexical scope in C++ -

the codes this:

int = 1; auto f = [a] {return a;}; = 100; std::cout << f() << endl; homecoming 0;

i expected see 100 result. however, a freezed when captured in f. result 1. there way maintain a consistent when a changes?

you need capture a reference:

auto f = [&a] {return a;};

or capture whole environment reference:

auto f = [&] {return a;};

c++ function lambda functional-programming lexical-scope

No comments:

Post a Comment