Yes you can. Don't. You can use them frequently, but if the expression is more than a few (1 to 7, say) lines then from a readability standpoint you would benefit from writing a separate function and using std::bind if currying arguments is required. Short lambda expressions increase code readability, longer ones decrease it.
Long lambdas do not decrease code readability. The difference between this:
for (auto it=begin(container); it!=end(container); ++it)
{
... 20 lines ...
}and this:
for_each(begin(container),end(container),[](element &x)
{
... 20 lines ...
});is just a matter of taste. I tend to like the for_each version when I can't use range-for.
You can use lambdas of whatever length when the functionality within is a one-off and not needed anywhere else in the code.
If the code is needed several times but only locally, you can still use a lambda and avoid polluting the outer scope with useless stuff:
void someFunc()
{
auto snarf = [](foo &f) { /* snarfing a foo */ };
for_each(begin(manyFoos), end(manyFoos), snarf);
snarf(*begin(manyFoos)); // first foo needs to be snarfed twice
snarf(anotherFoo);
}
Edited by Stroppy Katamari, 06 November 2012 - 01:14 PM.