There are several ways to pass callable objects as parameters or to store them for future use. You can create a class with operator()
, you can define a function and pass a pointer to it, and, since C++11, you can define a lambda via [](){}
syntax.
I appreciate lambda syntax as a shortcut in expressions such as find_if
that often beg for a compact callable expression. What I don't understand about lambda is the desire to use them outside the point of their declaration and risk introducing dangling references and such. C++ already has a powerful way to pass callable objects around which is much safer then lambda, and in those situation there is no benefit of compact expression of lambda.
Thus the question: why does C++11 allow use of a lambda outside the function that declares is or the functions called from it (and therefore introduces the risk of dangling references, etc)? Could you give an example where keeping lambda live outside the declaring function would be desirable?
Aucun commentaire:
Enregistrer un commentaire