On 8/2/2012 9:22 AM, Juha Nieminen wrote:
The emptiness means that everything is captured by value. See
[expr.prim.lambda]/14. I think that's what you're asking.
What? I tought that "[=]" was used to capture everything by value.
Sorry, you're right. Misread the Standard. Unless captured by copy,
any implicitly captured entity is captured by reference.
I believe you were actually correct the first time but not this one.
The exception appears to be 'this', which must be captured explicitly or
a default must be explicitly supplied. There are good reasons not to do
so by using '[=]':
Yes, that portion is kind of confusing. Here are the relevant quotes
from the Standard:
[expr.prim.lambda]/15:
<<An entity is captured by reference if it is implicitly or explicitly
captured but not captured by copy.>>
and
[expr.prim.lambda]/14:
<<An entity is captured by copy if it is implicitly captured and the
capture-default is = or if it is explicitly
captured with a capture that does not include an &.>>
What I understand from those two sentences is that if you need to
capture by copy, you have to either say '[=]' (which means everything is
captured by copy - capture default), or put the name of the captured
variable without the '&' in front of it, like '[a]'.
Now, my experimentation shows that unless any capture mode is specified,
nothing is actually captured. Is that what you're saying?
Should the following compile? And if so, what is the expected output?
Will 'a' be captured by reference in 'byref' closure?
#include <iostream>
int main()
{
int a = 42;
auto byref = []() { std::cout << "R:" << &a << a << '\n'; };
auto bycopy = [=]() { std::cout << "C:" << &a << a << '\n'; };
auto boo = [&a]() { a = 42; };
std::cout << " :" << &a << a << "\n";
byref(); bycopy(); boo(); byref(); bycopy();
}
Or should 'a' not be captured at all? Comeau online chokes on lambdas
altogether. VC++ 2010 refuses to compile the 'byref' initialization
(complaining about 'a' capture), unless I write [&], in which case
everything works what's expected.