Question about STL

Pages: 12
> what's the difference between [&, targetNumbers](int i) and [&](int i)

In [&, targetNumbers](int i), targetNumbers is captured by value, everything else (inputNumbers here) is captured by reference.

In [&](int i), everything is captured by reference (both inputNumbers and targetNumbers here).



> Does [&, targetNumbers](int i) pass targetNumbers like const targetNumbers ?

Yes. With [&, targetNumbers](int i) { /*...*/ } the lambda expression is not allowed to modify targetNumbers

With [&, targetNumbers](int i) mutable { /*...*/ } the lambda expression is allowed to modify the copy of targetNumbers that it has captured by value (the modification is on the copy, not the original.)

See: http://en.cppreference.com/w/cpp/language/lambda

Summary (MSDN):
. Reference captures can be used to modify variables outside, but value captures cannot. (mutable allows copies to be modified, but not originals.)

. Reference captures reflect updates to variables outside, but value captures do not.

. Reference captures introduce a lifetime dependency, but value captures have no lifetime dependencies.

https://msdn.microsoft.com/en-us/library/dd293608.aspx
Last edited on
Hi JLBorges,

Thanks a lot.
Of course I want to modify the argument and should capture by reference.
Thanks again.


Topic archived. No new replies allowed.
Pages: 12