Is function a functor?

The function can be used as a functor in STL algorithms, but for some reason the books persistently prove that a function is a function, and a functor is an object

  • What is a functor? - user227465

3 answers 3

There is no such term as a functor in the C ++ standard. Usually in books, this concept implies the so-called function object - a term that is actually defined in the C ++ standard.

From the standard C ++ (20.9 Function objects)

1 function object type function (3.9). Function call (5.2.2, 13.3.1.1) .230 function object type. In the whereabouts of the interface, there is a profile object. This function not only makes it possible .

Therefore, under the functors, the authors of the books most likely mean the objects of functions. Since functions are not related to object types, they are not included in this concept.

On the other hand, footnote 230 is written regarding the type of the postfix function call expression

230) This is a function of the type of a pointer.

However, algorithms can take functions by reference, and not necessarily function pointers.

The given quotation itself can be interpreted ambiguously. I think that the key to its correct interpretation is the following phrase from the above citta.

In the whereabouts of the interface, there is a profile object.

It is here, probably, the authors of the books draw the watershed between the objects that provide the operator with a function, and simple pointers to functions, calling the first functors.

  • Maybe a functional object ? - ߊߚߤߘ
  • @Arhad I think that in this case the English phrase functional object would be closer. However, it is the function object that is used in the standard. - Vlad from Moscow

The main difference between a function and a functor is that the function has no state, and the functor, which is an object, can have a state.

Of course, it is possible to propose for consideration a particular case when a static variable is defined inside a function, or some external (global) variable is used. But such functions cannot be used in two different contexts, since condition will be common. Using a functor (that is, an object with an overloaded operator() ) allows you to separate the state for different calls.

The same types of functors can be used as template parameters. Different types of functors will allow you to get different types of instantiated objects, even if the operator() signatures are the same. And if you have two different functions with the same signature, for example, int g(double) , int f(double) , then the type of these functions will still be the same int(double) .

  • one
    the function has no state - what about the static variables inside the function? Yes, it's not quite like objects, but still. - KoVadim
  • you have an abstraction of the notion of a functor and implementation merged. How a functor is implemented is another. Who said that all functors are required to have state variables? - rikimaru2013
  • @KoVadim in the second paragraph is written about it. Or did you mean something else? - αλεχολυτ
  • @ rikimaru2013 I said that he may have a state, but not obliged . - αλεχολυτ
  • @alexolut I only lead to the fact that your arguments are tied to the shortcomings of the implementation. Not a word in the standard about the internal state or the mandatory difference in signatures - rikimaru2013

Yes, easily.
It's just the callee .
Is the function called? undoubtedly. An object? Why not? What is a pointer but an object of a certain type?

In short, a function is a special case of a functor, that's all.