Bugzilla – Bug 350
Ptr<T> has operator < but no operator >, causes subtle bugs
Last modified: 2008-10-16 06:17:17 UTC
I had a comparison operator involving smart pointer fields that looked like this: bool PyViz::TransmissionSampleKey::operator < (PyViz::TransmissionSampleKey const &other) const { if (this->transmitter < other.transmitter) { return true; } if (this->transmitter > other.transmitter) { return false; } if (this->receiver < other.receiver) { return true; } if (this->receiver > other.receiver) { return false; } if (this->channel < other.channel) { return true; } else { return false; } } I was using these PyViz::TransmissionSampleKey values as keys of a std::map, and the std::map was going berserk, with completely erroneous operation. After many hours of debugging I tracked it down to the above comparison function. It turns out that we have a custom Ptr<T> operator < function, but no operator >. It seems that C++ is autogenerating some operator > function, but the autogenerated function and the manual function are not self-consistent. To fix my problem I am changing the > comparisons to !=, but I think this might bite someone else in the future. In principle, defining the remaining operators should be enough to solve the problems.
changeset cb3da9028895