Bug 350

Summary: Ptr<T> has operator < but no operator >, causes subtle bugs
Product: ns-3 Reporter: Gustavo J. A. M. Carneiro <gjcarneiro>
Component: coreAssignee: Mathieu Lacage <mathieu.lacage>
Status: RESOLVED FIXED    
Severity: minor CC: ns-bugs
Priority: P2    
Version: ns-3.2   
Hardware: All   
OS: All   

Description Gustavo J. A. M. Carneiro 2008-09-16 10:46:31 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.
Comment 1 Mathieu Lacage 2008-10-16 06:17:17 UTC
changeset cb3da9028895