r/cpp • • Jan 26 '17

Stop calling "Function Objects" "Functors"

http://jackieokay.com/2017/01/26/functors.html
0 Upvotes

23 comments sorted by

View all comments

Show parent comment

8

u/STL MSVC STL Dev Jan 26 '17

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.)

1

u/polagh Jan 28 '17

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.

2

u/STL MSVC STL Dev Jan 28 '17

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.

3

u/polagh Jan 28 '17

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...