I\'ve been thinking about storing C++ lambda\'s lately. The standard advice you see on the Internet is to store the lambda in a std::function object. However, none of this adv
Using C++14, you can avoid the copies altogether:
int main()
{
Simple test( 5 );
std::function f =
[test = std::move(test)] ()
{
return test.Get();
};
printf( "%d\n", f() );
}
To produce the output:
Constructing simple!
Moving simple!
Moving simple!
Destroying simple!
5
Destroying simple!
Destroying simple!
Note the following line:
[test = std::move(test)]
Here, the first appearance of "test" is in a different scope than the second.