Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you create a class with one single member which is an operator()? [duplicate]

I have tried to find out -out there- a good technical reason for defining a class which contains only one member and this member happens to be a operator().

I stumbled upon someone who -for whatever the reason- created a namespace containing a couple of classes, but each class contains only one operator() as a member.

It is clear to me that these classes might then be used as if they were methods (very likely), but why is this a good technical approach (I assume there is a good one), rather than simply defining a set of different methods within a singleton class which in this particular case would belong to the namespace I mentioned lines above.

The namespace looks something like:

namespace myNamespace {
   class ClassA {
   public:
      void operator()();
   };

   class ClassB {
   public:
      int operator()(arg1, arg2);
   };

   ...
}

Is this simply a matter of personal taste/style or is this the expression of advance/sophisticated design? I assume this design encompass some wise knowledge that I haven't gathered yet, but on the other hand I haven't found a piece of article or question discussing this - which makes me doubt about such "wise knowledge" in the design.

What does the experts here have to say?

Thanks in advance!

EDIT

Hello again! I accept this question to be a REPEATED question, whose answer is found here.

The term Functor was unknown to me and while trying to find an answer to my doubt (prior to this question's submission), I did not see any references to this term.

I still accept the answer from SergeyA, because his answer introduced me to the term and his explanation is answering my question. His answer -from my point of view- is extended by the answer found in the link I stated lines above.

Thanks!

like image 377
eindemwort Avatar asked Aug 01 '17 15:08

eindemwort


1 Answers

This technique mostly comes up with a generic programming and templates. The class which has operator() defined for it is called a functor and the beauty of such a class is that it could be used interchangeably with a function pointer in many cases. For example:

template<class F>
void call_f(F f) {
    f();
}

Will work with both functions and functors. Or, better example would be STL algorithms, such as find_if, copy_if, remove_if, etc. All of them have a template parameter which could be a function, a functor or (a case of a functor actually) a lambda.

The other benefit of such a class is that it's type defines it's objects. A function pointer is just a function pointer which always need to point to a specific function. But for stateless functors (ones which do not have any members) the type is enough to recreate the object as needed. This is how, for example, std::less is used for std::map. It doesn't take the actual object of the comparison, but it's type and creates the object from the type in-place.

like image 145
SergeyA Avatar answered Nov 15 '22 04:11

SergeyA