我在通过重写的相等运算符比较同一接口的模板实现时遇到问题。
Interface* ptr1 = ...; Interface* ptr2 = ...;
*ptr1 == *ptr2;
我想出的唯一解决方案是强制只比较相同实现的对象并像这样实现比较:
class Interface {
public:
virtual ~Interface() {}
virtual bool operator==(const Interface&) const = 0;
};
template <typename T> class Impl : public Interface {
public:
bool operator==(const Interface& rhs) const {
assert(typeid(rhs) == typeid(const Impl&));
const Impl& rhsRef = static_cast<const Impl&>(rhs);
// ...
}
};
这个解决方案的问题是它对我的目的来说太有限了——我希望能够比较不同的实现。如果实现的数量有限,则可以使用双分派模式。但在我的例子中,Impl 是一个模板,所以双重分派是不可能的,因为它需要一个虚函数模板:
// This obviously doesn't work.
class Interface {
public:
virtual ~Interface() {}
virtual bool operator==(const Interface&) const = 0;
template <typename T2> virtual bool operator==(const Impl<T2>&) const = 0;
};
template <typename T> class Impl : public Interface {
public:
bool operator==(const Interface& rhs) const {
return rhs == *this;
}
template <typename T2> bool operator==(const Impl<T2>& rhs) const {
// ...
}
};
有什么解决办法吗?我需要这个来编写可以包装任何 STL 迭代器的 AnyIterator 类。但是我无法比较 AnyIterators,如果它们被包裹在不同的类型中,例如 iterator 和 const_iterator:
std::list<int>::iterator it1 = ...; std::list<int>::const_iterator it2 = ...;
AnyIterator<const int> myIter1 = it1; AnyIterator<const int> myIter2 = it2;
it1 == it2; // This works perfectly.
myIter1 == myIter2; // This doesn't work!