0

When defining the equality operator,

class foo
{
    bool operator ==(const foo& other) const; //explicit
}

why is the inequality operator not implicitly defined unless another is explicitly defined. By default

"a == b" <==> "!(a != b)" 

and the following definition can be used

bool operator !=(const foo& other) const
{
    return !(this->operator==(other));
}

But why should does this definition need to be added every time?

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • 2
    Although posted for Python, [this post](https://stackoverflow.com/questions/9452536/why-does-python-have-an-ne-operator-method-instead-of-just-eq) makes some good language agnostic points that the opposite of `==` isn't strictly `!=` – Cory Kramer Sep 28 '15 at 12:14
  • 2
    I'm not sure why it is designed this way, but there is a proposal (N4126 - http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2014/n4126.htm ) to change C++ behavior to allow that. – tfinniga Sep 28 '15 at 12:16
  • @dasblinkenlight thanks forgot to use the duplicate form rather the normal one to find questions – UmNyobe Sep 28 '15 at 12:18
  • The standard library has had [`std::rel_ops`](http://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp) since the original SGI implementation but nobody ever uses it. – Blastfurnace Sep 28 '15 at 12:34

0 Answers0