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
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
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.
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) .
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.
Source: https://ru.stackoverflow.com/questions/620912/
All Articles