In this piece of code 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 above trying to do at lines pointed by arrows?
I am a beginner in C++ and I understand that we can override / define operator() by defining it.
But then shouldn’t it be defined like this
L& operator() { return lhs_ < rhs_ ? lhs_ : rhs_; }
I am sure this is some differenct syntax since operator() is supposed to be one word. Also, you cannot define two of them with different return types.
No this is type cast operator.
You can define
As operator that allows to cast to the
type. For exampleThe
operator()has a different purpose, it allows to use the class as “function” and this is not the case here