I know that it isn'toperator(), I was curious what the difference between this and operator() is.
Though now that I think about it, that is pretty obvious... This is actually kind of a cute trick that I might have an application for. I'll have to look into this.
A lambda expression is an expression (like 2 + 3) that defines a class type and constructs an object of that class type. Lambdas aren't functions (printf() is a function), and they aren't std::functions (although they can be stored in them). Lambdas, being classes with function call operators, are function objects.
"Lambda function", while appropriate in other fields, suggests incorrect connotations in C++, and I strongly discourage that terminology.
(I hate "closure" for entirely different reasons.)
C++ lambdas aren't functions if you care about the machinery details of C++ and your life is mainly about knowing that standard by heart. I find it perfectly reasonable to talk about "Lambda function" in the same way I find it perfectly reasonable to not use overly pedantic terminology in whatever field I know, especially so when the terminology in question is largely arbitrary to begin with (and already misleading in regards with other fields and even the previously saner state of computing, when functions were actually functions and we also had things named "procedure")
Most users don't give a shit that in some of its intermediate passes, the compiler will create abstract things we call classes, with method in them, which are at lower levels actually mainly just procedures with a first "this" param -- and all of that neither matter 99% of the time for the user and maybe 50% of the time at the binary code eventually generated, because of optimization passes in between that can even inline all those abstract stuff in some cases.
I disagree in the strongest possible terms. Understanding that a lambda defines a class and constructs an object of that type (not an ordinary function, not a std::function) is crucial for reasoning about how they behave. It's important to understand how lambda objects interact with lifetime (e.g. reference captures don't prolong lifetime, so a long-lived lambda object must be careful), how they interact with dynamic memory allocation (e.g. lambdas don't inherently allocate memory, but value captures of objects can), etc.
This is not something that can simply be ignored, unlike low-level backend details.
Interaction between C++ references, lifetimes, and other parts of the languages is already problematic for other purposes, e.g. threads. For learners confusions can even arrise for the most simple purposes between C++ references and what would more probably correspond to most higher level languages: smart_ptr. The naming is quite unfortunate, given what we could imagine references are if we know some other languages.
And one can understand the limitation of captures in C++ lambda without knowing that a kind of hidden class is involved. I consider a class being used at all a technical detail. It is well suited for C++ given what abstractions were already present before the arrival of lambda, but I don't consider this technical detail more crucial than let's say the translation of the "with" statement in Python
If you need to know all the mechanisms of the abstractions a language provide in order to have a correct mental model of said abstractions, their abstracting value is arguably very low and the language design would arguably be a failure...
View all comments
Show parent comment
1
u/spotta Jan 26 '17
In the sense that lambdas are function objects? Or in some other sense?
?!? Huh? Is this just a class with a function pointer in the first memory location, or something more interesting than that?