I have a class template that should be able to compare between two objects, through comparison objects obtained from the Compare
class, which I have:
template<typename T> class Container { public: template<typename A, typename B> class Compare { public: virtual bool eq(const A&, const B&) const = 0; };
I provide default comparison objects if type T
has the ==
operator:
template<typename A, typename B> class Default : public Compare<A,B> { public: bool eq(const A& a, const B& b) const { return a==b; } }; private: Compare<T,T>* comparison_object; bool uses_default; Container() : comparison_object(new Default<T,T>()), uses_default(true) {} Container(Compare<T,T>& cmp) : comparison_object(&cmp), uses_default(false) {} ~Container() { if(uses_default) delete comparison_object; } };
However, when I try to compile this with a custom class, it does not have < operator==
overloads (even if I provide an object derived from Compare
):
MyObjCmp moc; Container<MyObj>(&moc);
The compiler complains that the statement does not exist:
error: no match for 'operator==' (operand types are 'const MyObj' and 'const MyObj')
This makes sense because the Default
class still needs to be created, even if I don't need it. But now I need a workaround ...
Any ideas?
source share