What does this operator () syntax do?

In this code snippet taken from http://drdobbs.com/cpp/184403774 :

template <class L, class R> class MinResult { L& lhs_; R& rhs_; public: operator L&() { return lhs_ < rhs_ ? lhs_ : rhs_; } // <---- operator R&() { return lhs_ < rhs_ ? lhs_ : rhs_; } // <---- MinResult(L& lhs, R& rhs) : lhs_(lhs), rhs_(rhs) {} }; 

What is the code you are trying to do in the lines indicated by arrows?

I am starting in C ++, and I understand that we can override / define operator() by defining it.

But then it should not be defined as

 L& operator() { return lhs_ < rhs_ ? lhs_ : rhs_; } 

I am sure this is some kind of different syntax, since operator() should be one word. In addition, you cannot define two of them with different types of returned data.

+4
source share
1 answer

No, this is a type operator.

You can define

 operator type() const 

Like an operator that allows cast to type . for instance

 class date { public: operator time_t() const; // convert to time_t }; 

operator() has a different purpose, it allows you to use the class as a "function", and here it is not.

+7
source

Source: https://habr.com/ru/post/1402080/


All Articles