Re: C++0x lambda and capturing computed values
SG wrote:
On 7 Mai, 19:27, Sohail Somani <soh...@taggedtype.net> wrote:
The undesirable alternative is:
for(...)
{
value_type & t = *it;
functions.push_back([t](){whatever(t);});
}
I had to look into the draft again to check whether it actually does
what you seem to think it does due to decltype(t) being a reference.
But yes, it captures the referred-to object by value. So, inside the
lambda's body decltype(t) will not be a reference and it'll be
equivalent to what your first version with boost::bind does.
I had to double-check as well :-)
Just to clarify, your understanding comes from [expr.prim.lambda]/7 ?
It would be ideal if I could do something like:
functions.push-back([t=*it](){whatever(t);});
Any thoughts or suggestions?
Interesting idea. But it'll hardly save you typing. IMHO,
auto&& t = *it;
functions.push_back([=]{whatever(t);});
is an okay-solution which doesn't justify a more complicated grammar
in the capture clause.
Well the actual case is something like:
for(iterator it1, it2, it3 ...)
{
.. some stuff here ..
functions.push_back(bind(whatever,*it1,*it2,*it3));
}
To me, the alternative is quite verbose.
For comparison:
for(iterator it1, it2, it3 ...)
{
auto&& t1=*it1;
auto&& t2=*it2;
auto&& t3=*it3;
functions.push_back([=](){whatever(t);});
}
or
for(iterator it1, it2, it3 ...)
{
functions.push_back([t1=*it1,t2=*it2,t3=*it3]()
{whatever(t1,t2,t3);});
}
Less typing even if it is for 6*3 characters is better if you ask me.
Seems that bind is not obsoleted by lambda (I didn't expect it to be
anyway.)
--
Sohail Somani
http://uint32t.blogspot.com