title: std::map not removing duplicates with custom object
body: <p>all.</p>
  
  <p>So, I have created a Comparable base class (a la Java) which does the job of overloading comparison operators in C++. Here it is:</p>
  
  <pre><code>template &lt;class T&gt;
  class Comparable {
  public:
    bool operator== (const T&amp; rhs) const { return this-&gt;compare(rhs) == 0; }
    bool operator!= (const T&amp; rhs) const { return !(*this == rhs); }
    bool operator&lt;  (const T&amp; rhs) const { return this-&gt;compare(rhs) &lt; 0; }
    bool operator&lt;= (const T&amp; rhs) const { return (*this == rhs) || (*this &lt; rhs); }
    bool operator&gt;  (const T&amp; rhs) const { return this-&gt;compare(rhs) &gt; 0; }
    bool operator&gt;= (const T&amp; rhs) const { return (*this == rhs) || (*this &gt; rhs); }
  protected:
    virtual int compare (const T&amp; rhs) const = 0;
  };
  </code></pre>
  
  <p>The subclass objects have both an ID, and data that needs to be a key for sorting. I've implemented each so that objects with the same ID return 0, and if they're not the same ID, they sort according to their data. Here's an example:</p>
  
  <pre><code>int Foo::compare(const Foo&amp; rhs) const
  {
      if (_id == rhs._id)
          return 0;
      // _value is integer data; comparison should sort on this
      if (_value == rhs._value)
      {
          // OK, same data, now sort on ID's
          return _id.compare(rhs._id);
      }
      // Sort on value
      return _value - rhs._value;
  }
  </code></pre>
  
  <p>So far, so good, I think.</p>
  
  <p>However, when I attempt to store Foo objects in a std::set container, the set will NOT eliminate duplicates. That is, there will still be objects in it that contain the same ID, even though that should be considered equal.</p>
  
  <p>Does anyone have any idea what is going on?</p>
  
  <p>EDIT: There are a lot of questions about why the code is designed this way.</p>
  
  <p>There are two conditions I need to satisfy:</p>
  
  <ol>
  <li>Objects with the same ID must be considered the "same" object, regardless of value.</li>
  <li>Objects that do not have the same ID must be sorted according to their value (what that value is, depends upon the type of the object).</li>
  </ol>
  
  <p>This is because the data that creates these objects is coming from an unverified source. If that source gives data with the same ID but a different value, that's invalid data.</p>
  
  <p>EDIT 2: Regarding strict weak ordering for std::map. Let's say you have two Foo objects, A and B.</p>
  
  <p>If their ID's are equal, then A &lt; B is false, and B &lt; A is false.
  If their ID's are not equal, then A &lt; B is true or false depending upon their values, and B &lt; A is the opposite of A &lt; B.</p>
  
  <p>Should this not satisfy the strict ordering rules?</p>
  
  <p>In any case, if std::set uses the less-than operator (as it does by default), shouldn't it work as designed?</p>
  
  <p>EDIT 3: <code>std::set</code>, not <code>std::map</code>. That was really stupid of me. Apologies.</p>