1

I am trying to create a set using a custom == operator for the set::insert function.

the case is i have an object with 2 members - Source, Target.

The objects == operator looks like this:

  bool Object::operator==(const Object& other) const{
    if (m_first == other.first() && m_second == other.second()) {
      return true;
    }

    if (m_first == other.second() && m_second == other.first()) {
      return true;
    }

    return false;
}

For the specific set i need it to act differently, return true only when

m_first==other.m_first && m_second==other.m_second

according to the std::reference i can only create a set using a custom compare function for weak strict ordering, which is not the case.

I cannot edit the original function, as this behavior is needed in other place in the code.

Right now i am using a vector for a workaround, but i am looking for a simpler more readable solution.

Thank you

Dinari
  • 2,487
  • 13
  • 28
  • Making the semantics of equality a moving target probably isn't such a good idea. – πάντα ῥεῖ Nov 08 '15 at 09:22
  • 3
    As for your question you can pass a function pointer or functor class type as comparator, as [described here](http://en.cppreference.com/w/cpp/container/set). The default is `std::less` BTW, not `std::equal_to`. – πάντα ῥεῖ Nov 08 '15 at 09:29
  • this is used for the set ordering, not the insert function,the insert function first check if the key exists, and only then uses std::less for the ordering of the new item in the set – Dinari Nov 08 '15 at 09:33
  • 2
    @OrDinari The equality operator is not used for that. The check is whether the two items are equivalent under the ordering, i.e. that neither is less than the other. See http://en.cppreference.com/w/cpp/container/set. – Alan Stokes Nov 08 '15 at 09:39
  • I see what you mean, thank you, i could use that to solve the problem. – Dinari Nov 08 '15 at 09:43

0 Answers0