S
Sohail Somani
Hello,
I'm pretty sure the answer is "no" but is it possible to capture
computed values?
Specifically, I am thinking of the following case:
vector<function<void()>> functions;
for(iterator it = ...)
{
functions.push_back(boost::bind(whatever,*it);
}
Here, the "*it" sub expression passes in a computed value to boost::bind
which is then stored by value. If I change it to the following:
for(...)
{
functions.push_back([it](){whatever(*it);});
}
This is a potential bug since the iterator can become invalidated by the
time the function is called.
The undesirable alternative is:
for(...)
{
value_type & t = *it;
functions.push_back([t](){whatever(t);});
}
It would be ideal if I could do something like:
functions.push-back([t=*it](){whatever(t);});
Any thoughts or suggestions?
Thanks in advance!
I'm pretty sure the answer is "no" but is it possible to capture
computed values?
Specifically, I am thinking of the following case:
vector<function<void()>> functions;
for(iterator it = ...)
{
functions.push_back(boost::bind(whatever,*it);
}
Here, the "*it" sub expression passes in a computed value to boost::bind
which is then stored by value. If I change it to the following:
for(...)
{
functions.push_back([it](){whatever(*it);});
}
This is a potential bug since the iterator can become invalidated by the
time the function is called.
The undesirable alternative is:
for(...)
{
value_type & t = *it;
functions.push_back([t](){whatever(t);});
}
It would be ideal if I could do something like:
functions.push-back([t=*it](){whatever(t);});
Any thoughts or suggestions?
Thanks in advance!