Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removed operator!= in C++20 standard library [duplicate]

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...

like image 239
Aconcagua Avatar asked Oct 10 '19 09:10

Aconcagua


People also ask

What does std :: remove do?

std::remove, std::remove_if. Removes all elements satisfying specific criteria from the range [first, last) and returns a past-the-end iterator for the new end of the range. 1) Removes all elements that are equal to value , using operator== to compare them.

How does remove if work C++?

C++ Algorithm remove_if() function is used to eliminate all the elements that satisfy a predicate from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the new end of the range.


2 Answers

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.

like image 159
N. Shead Avatar answered Sep 17 '22 15:09

N. Shead


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.

like image 34
StoryTeller - Unslander Monica Avatar answered Sep 16 '22 15:09

StoryTeller - Unslander Monica