Why is operator!= removed in C++20 for many standard library types?

C++C++ Standard-LibraryComparison OperatorsC++20Equality Operator

C++ Problem Overview


According to cppreference, std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains.

What's the reasoning behind? I might agree on comparing for inequality being meaningless, but then comparing for equality would be just as meaningless as well, wouldn't it?

Similarly, operator!= of many other standard library types, including containers such as std::unordered_map::operator!= and std::unordered_set::operator!= will be removed in C++20 according to cppreference.

Having to write if(!(id1 == id2)) doesn't make any code any clearer compared to if(id1 != id2), in contrast, just the opposite...

C++ Solutions


Solution 1 - C++

In C++20 the way that the relational operators work was changed, notably with the introduction of the spaceship <=> operator. In particular, If you only provide operator==, then a != b is rewritten to !(a == b).

From [over.match.oper]/3.4:

> The rewritten candidate set is determined as follows: > > - For the relational ([expr.rel]) operators, the rewritten candidates include all non-rewritten candidates for the expression x <=> y. > - For the relational ([expr.rel]) and three-way comparison ([expr.spaceship]) operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y <=> x. > - For the != operator ([expr.eq]), the rewritten candidates include all non-rewritten candidates for the expression x == y. > - For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == x. > - For all other operators, the rewritten candidate set is empty.

And [over.match.oper]/9:

> If a rewritten operator== candidate is selected by overload resolution for an operator @, its return type shall be cv bool, and x @ y is interpreted as: > > - if @ is != and the selected candidate is a synthesized candidate with reversed order of parameters, !(y == x), > - otherwise, if @ is !=, !(x == y), > - otherwise (when @ is ==), y == x, > > in each case using the selected rewritten operator== candidate.

As such, an explicit overload for operator!= is no longer necessary. The removal of the operator has not changed comparison semantics.

All containers have had their operator!= removed, as far as I can tell (check e.g. the vector synopsis). The only exceptions are the container adaptors std::queue and std::stack: my guess is that it is to preserve backwards compatibility when used with third-party containers, in case the equality operators are not symmetric.

Solution 2 - C++

We don't need a library provided operator!= anymore. Providing operator== allows the compiler to do some juggling and evaluate a != b in terms of a == b, all on its own.

> [over.match.oper] > > 3 For a unary operator @ with an operand of a type whose > cv-unqualified version is T1, and for a binary operator @ with a left > operand of a type whose cv-unqualified version is T1 and a right > operand of a type whose cv-unqualified version is T2, four sets of > candidate functions, designated member candidates, non-member > candidates, built-in candidates, and rewritten candidates, are > constructed as follows: > > 3.4.3 For the != operator ([expr.eq]), the rewritten candidates > include all non-rewritten candidates for the expression x == y.

std::type_info and many more library types had their operator!= removed as part of P1614 - The Mothership has Landed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAconcaguaView Question on Stackoverflow
Solution 1 - C++N. SheadView Answer on Stackoverflow
Solution 2 - C++StoryTeller - Unslander MonicaView Answer on Stackoverflow