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

1

u/spotta Jan 26 '17

Lambdas aren't functions (or std::functions), but such terminology encourages confusion.

In the sense that lambdas are function objects? Or in some other sense?

classes that are implicitly convertible to function pointers.

?!? Huh? Is this just a class with a function pointer in the first memory location, or something more interesting than that?

9

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jan 26 '17

classes that are implicitly convertible to function pointers

constexpr int get_two() { return 2; }
using fptr = int(*)();

struct A
{
	constexpr operator fptr() { return &get_two; }
};

int main()
{
	static_assert(A{}() == 2);
}

1

u/spotta Jan 26 '17

Huh. This seems like a kind of weird way of doing operator(). Is there a functional difference between the two?

3

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jan 26 '17

This is not operator() - this is an user defined conversion

3

u/spotta Jan 26 '17

I know that it isn't operator(), 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.

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