यह सभी का अधिभार होता है कि मैं सभी ओवरलोडेड तुलना ऑपरेटर को कक्षा में लिखूं, इसलिए मैंने एक टेम्पलेट क्लास लिखा है जो < लागू करता है, < =,> =,! = यदि व्युत्पन्न कक्षा उपकरण == और <। यह काम कर रहा है लेकिन इसमें बहुत सी कलाकार हैं और यह स्पष्ट नहीं है कि "उत्सुकता से आवर्ती टेम्पलेट पैटर्न", तो मुझे आश्चर्य है कि क्या सरल समाधान हैं? संकलक (ऑपरेटर खत्म हो जाएगा ADL कर जो अंदर दिखेगा MyType
और Comparable<MyType>
के लिए MyType a, b; a > b;
देखने का सामना करना पड़ता है जब के रूप मेंटेम्पलेट क्लास कार्यान्वयन तुलना ऑपरेटर
template <typename T>
struct Comparable {
friend bool operator!=(T const & lhs, T const & rhs) { return !(lhs == rhs); }
friend bool operator> (T const & lhs, T const & rhs) { return rhs < lhs; }
// ...
};
class MyType : Comparable<MyType> {
int data;
friend bool operator==(MyType const & lhs, MyType const & rhs) {
return lhs.data == rhs.data;
}
friend bool operator< (MyType const & lhs, MyType const & rhs) {
return lhs.data < rhs.data;
}
public:
// ...
};
:
template <class Derived>
class Comparable
{
public:
bool operator!=(const Comparable<Derived>& other) {
return !(static_cast<Derived*>(this)->operator==
(*static_cast<const Derived*>(&other)));
}
bool operator<=(const Comparable<Derived>& other) {
return (static_cast<Derived*>(this)->operator==
(*static_cast<const Derived*>(&other)))
|| (static_cast<Derived*>(this)->operator<
(*static_cast<const Derived*>(&other)));
}
bool operator>(const Comparable<Derived>& other) {
return !(static_cast<Derived*>(this)->operator==
(*static_cast<const Derived*>(&other)))
&& !(static_cast<Derived*>(this)->operator<
(*static_cast<const Derived*>(&other)));
}
bool operator>=(const Comparable<Derived>& other) {
return !(static_cast<Derived*>(this)->operator<
(*static_cast<const Derived*>(&other)));
}
};
http://www.boost.org/doc/libs/1_58_0/libs/utility/operators.htm – interjay
वहां हैं। मैंने उपरोक्त लिंक @interjay के समान समाधान का प्रस्ताव दिया होगा: कक्षा के अंदर मित्र फ़ंक्शन परिभाषाएं * इनलाइन * डी मुक्त फ़ंक्शंस हैं जिन्हें व्युत्पन्न प्रकार पर एडीएल के माध्यम से पाया जा सकता है। आप इसे ऑपरेटरों को प्रदान करने के लिए इसका उपयोग कर सकते हैं। –