title: Equality operator overloads: Is (x!=y) == (!(x==y))?
body: <p>Does the C++ standard guarantee that <code>(x!=y)</code> always has the same truth value as <code>!(x==y)</code>?</p>
  <hr />
  <p>I know there are <em>many</em> subtleties involved here: The operators <code>==</code> and <code>!=</code> may be overloaded. They may be overloaded to have different return types (which only have to be implicitly convertible to <code>bool</code>). Even the <code>!</code>-operator might be overloaded on the return type. That's why I handwavingly referred to the &quot;truth value&quot; above, but trying to elaborate it further, exploiting the implicit conversion to <code>bool</code>, and trying to eliminate possible ambiguities:</p>
  <pre><code>bool ne = (x!=y);
  bool e = (x==y);
  bool result = (ne == (!e));
  </code></pre>
  <p><strong>Is <code>result</code> guaranteed to be <code>true</code> here?</strong></p>
  <p>The C++ standard specifies the equality operators in section 5.10, but mainly seems to define them <em>syntactically</em> (and some semantics regarding pointer comparisons). The <em>concept</em> of being <a href="http://en.cppreference.com/w/cpp/concept/EqualityComparable" rel="nofollow noreferrer">EqualityComparable</a> exists, but there is no dedicated statement about the relationship of its operator <code>==</code> to the <code>!=</code> operator.</p>
  <p>There exist <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3950.html" rel="nofollow noreferrer">related documents from C++ working groups</a>, saying that...</p>
  <blockquote>
  <p>It is vital that equal/unequal [...] behave as boolean negations of each other. After all, the world would make no sense if both operator==() and operator!=() returned false! As such, it is common to implement these operators in terms of each other</p>
  </blockquote>
  <p>However, this only reflects the Common Sense™, and does not <em>specify</em> that they have to be implemented like this.</p>
  <hr />
  <p><sub>Some background: I'm just trying to write a function that checks whether two values (of unknown type) are equal, and print an error message if this is not the case. I'd like to say that the required concept here is that the types are <code>EqualityComparable</code>. But for this, one would still have to write <code>if (!(x==y)) {…}</code> and could <strong>not</strong> write <code>if (x!=y) {…}</code>, because this would use a different operator, which is not covered with the concept of <code>EqualityComparable</code> at all, and which might even be overloaded differently...</sub></p>
  <hr />
  <p>I know that the programmer basically <em>can</em> do whatever he wants in his custom overloads. I just wondered whether he is really <em>allowed</em> to do everything, or whether there are rules imposed by the standard. Maybe one of these subtle statements that suggest that deviating from the usual implementation causes undefined behavior, like the one that <a href="https://stackoverflow.com/questions/35943551/equality-operator-overloads-is-x-y-x-y/35943728?noredirect=1#comment59544090_35943728">NathanOliver mentioned in a comment, but which seemed to only refer to certain types</a>. For example, the standard <strong>explicitly</strong> states that for <em>container types</em>, <code>a!=b</code> is equivalent to <code>!(a==b)</code> (section 23.2.1, table 95, &quot;Container requirements&quot;).</p>
  <p>But for general, user-defined types, it currently seems that there are no such requirements. The question is tagged <code>language-lawyer</code>, because I hoped for a definite statement/reference, but I know that this may nearly be impossible: While one could point out the section where it said that the operators <em>have</em> to be negations of each other, one can hardly prove that none of the ~1500 pages of the standard says something like this...</p>
  <p>In doubt, and unless there are further hints, I'll upvote/accept the corresponding answers later, and for now assume that for comparing not-equality for <code>EqualityComparable</code> types should be done with <code>if (!(x==y))</code> to be on the safe side.</p>