I have a class template that needs to be able to compare between two objects, via comparison objects derived from a Compare
class 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 a default comparison objects, assuming 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 that does not have an operator==
overload (even if I provide an object derived from Compare
):
MyObjCmp moc;
Container<MyObj>(&moc);
The compiler complains that the operator doesn't 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 though I don't need it. But now I need a workaround...
Any ideas?