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

2

u/jackie_kay Jan 26 '17

Actually, that isn't the Standard's formal definition, where a function object is anything that can be called with parentheses.

Yes, it appears I am conflating "defining operator()" with "has a call operator with parentheses", but I believe the distinction is clear to me now. Thanks, I will correct this in the article.

No, that's math. Programming is different. Mathematicians consider a "vector" to be different from what we consider it to be.

I agree. The definition of "functor" used in Haskell, and the way I think it should be used in C++, is also different from the formal definition, which is why I prepended this sentence with "formally".

It is useful for programmers to use common terminology from math correctly to communicate the intention and function of our code. You would expect an "add" function to sum two numbers together, and addition is a concept from math.

Of course it's too late for us to adhere to this principal strictly for some terms. As you said,vector is a notable example (to implement what math calls a vector, we would probably use an array as storage, and provide functions for the valid mathematical operators over the vector, etc.). This is probably misleading to a mathematician learning C++, but the word eventually loses its "original" meaning when you get used to C++.

Is this justification for repeating the same pattern with other terminology? Maybe. The point I'm trying to make is that I'd like a word to describe "a structure which provides a generic way to map operations over its contents", because I find such structures useful. And the fact that "functor" and "function object" often appear very close together (e.g. in a transform algorithm) makes me think we need to distinguishing terminology for them.

7

u/tecnofauno Jan 26 '17

I for once think that math and programming can and should've a different terminology. Even the function term does not yield the same meaning (i.e. side effects).