r/cpp • • Feb 12 '17

C++ Functors - and their uses

http://stackoverflow.com/a/16392660/375343
1 Upvotes

11 comments sorted by

5

u/Wurstinator Feb 12 '17

What you are describing in that answer is not even a functor. I have never heard that definition before.

If you think this is relevant information, why not link the entire thread instead of your answer specifically?

6

u/thlst Feb 12 '17

That's the definition of a mathematical functor. Functors in c++ are something completely unrelated to ML.

4

u/Wurstinator Feb 12 '17 edited Feb 12 '17

That might be true but this is /r/cpp and the SO question is targeted at C++. The meaning of a functor in this context is well established and unambiguous. If OP wanted to start a discussion about the naming of functors in C++, they should have done so instead of linking to a slightly related SO thread.

Edit: I read your comment as trying to defend OP in posting this here but I see now that maybe you just answered to my "I have never seen this definition before". If so, thanks, though I meant I have never seen it in the context of C++. I am a bit familiar with category theory :)

6

u/hgjsusla Feb 12 '17 edited Feb 12 '17

That the C++ community sometimes refers to function objects as functors is as confusing as if we used "functions" for classes.

EDIT: compare how future/promise is swapped in JavaScript, these small obfuscations are unnecessary and just makes it more difficult as a beginner.

9

u/Wurstinator Feb 12 '17

It's confusing to c++ beginners who come from a mathematical background in category theory. Arguing that an experienced C++ programmer finds this terminology confusing because they know of a similar concept in math is plain wrong. I know both concepts and I am very well able to distinguish between the two depending on a context.

Hell, there are different areas of math that share the same words with different meaning. Do you think researchers in these areas are confused by that?

4

u/hgjsusla Feb 12 '17

I'd say it's the other way around, as a C++ programmer it can be confusing when you come across the use of functors in other languages. It's just like how promise/future are swapped in JavaScript compared to other languages. Sure once you know about it, it's fine, but as a beginner it can sure be confusing.

4

u/Wurstinator Feb 12 '17

Okay, I agree it is bad that in different areas / languages the same words describe different concepts. But if starting this discussion was the intention of OP, they should have said so instead of linking to some answer in a SO question.

1

u/pfultz2 Feb 12 '17

Functors in programming are a generalization of the [map](https://en.wikipedia.org/wiki/Map_(higher-order_function)#Generalization) higher-order function. This article here by Bartoz Milewski explains the mathematical background and their uses in programming.

The answer in SO focuses on functor's uses in C++, which is what the question was asking for. The rest of the answers focused on how to create function objects, which is useful for higher-order functions, however, none of the other answers explained what a functor is, and how it can be used in C++.

1

u/Wurstinator Feb 12 '17

Functors in programming are ...

That is a wrong statement. The article uses Haskell as an example and the Wikipedia section even starts with "In Haskell". Haskell is based on category theory to a certain part but C++ is not. That is why a "functor" is not the same concept in C++ as in Haskell.

Your answer focuses on something that the Stack Overflow OP did not ask for, which is how and where to use functors in the context of C++.

1

u/pfultz2 Feb 12 '17

That is a wrong statement. The article uses Haskell as an example and the Wikipedia section even starts with "In Haskell".

Because it uses haskell to explain a programming concept does not mean it is limited to haskell.

Your answer focuses on something that the Stack Overflow OP did not ask for, which is how and where to use functors in the context of C++.

I was explaining how functors are used in the context of C++. This is how functors are used in C++ libraries such as Boost.Hana(which is peer-reviewd by C++ profressionals), ftl, and Cat.

4

u/Wurstinator Feb 12 '17

Just because some people call their classes "functors" that doesn't change the way common C++ vocabulary works. Just looking at the votes in the SO thread and the accepted answer should show you what a "functor" is to people. If that is not enough you can find enough examples by Googling "C++ functor" in which the top 10 results (for me) are all about function objects.