简体   繁体   English

为什么 operator?= 在 C++20 中为许多标准库类型删除?

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

According to cppreference , std::type_info::operator!= gets removed with C++20, however, std::type_info::operator== apparently remains.根据cppreferencestd::type_info::operator!=被 C++20 删除,但是, std::type_info::operator==显然仍然存在。

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.类似地,许多其他标准库类型的operator!= ,包括std::unordered_map::operator!=std::unordered_set::operator!=等容器,将根据 cppreference 在 C++20 中被删除。

Having to write if(!(id1 == id2)) doesn't make any code any clearer compared to if(id1 != id2) , in contrast, just the opposite...if(id1 != id2)相比,必须编写if(!(id1 == id2))不会使任何代码更清晰,相反,恰恰相反......

In C++20 the way that the relational operators work was changed, notably with the introduction of the spaceship <=> operator.在 C++20 中,关系运算符的工作方式发生了变化,特别是引入了 spaceship <=>运算符。 In particular, If you only provide operator== , then a != b is rewritten to !(a == b) .特别是,如果您只提供operator== ,那么a != b将被重写为!(a == b)

From [over.match.oper]/3.4 :来自[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.对于关系 ([expr.rel]) 运算符,重写的候选包括表达式 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.对于关系 ([expr.rel]) 和三向比较 ([expr.spaceship]) 运算符,重写的候选还包括一个合成候选,两个参数的顺序颠倒,对于每个未重写的候选表达式 y <=> x。
  • For the.= operator ([expr,eq]).对于.= 运算符 ([expr,eq])。 the rewritten candidates include all non-rewritten candidates for the expression x == y.重写的候选项包括表达式 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.对于等式运算符,重写的候选还包括一个合成的候选,两个参数的顺序颠倒,用于表达式 y == x 的每个未重写的候选。
  • For all other operators, the rewritten candidate set is empty.对于所有其他运算符,重写的候选集为空。

And [over.match.oper]/9 :[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:如果通过重载决策为运算符 @ 选择了重写的 operator== 候选者,则其返回类型应为 cv bool,并且 x @ y 被解释为:

  • if @ is,= and the selected candidate is a synthesized candidate with reversed order of parameters, !(y == x),如果@是,= 并且选择的候选者是具有相反参数顺序的合成候选者, !(y == x),
  • otherwise, if @ is,=, !(x == y) ,否则,如果 @ 是,=, !(x == y) ,
  • otherwise (when @ is ==), y == x,否则(当 @ 为 == 时),y == x,

in each case using the selected rewritten operator== candidate.在每种情况下使用选定的重写运算符== 候选。

As such, an explicit overload for operator!= is no longer necessary.因此,不再需要operator!=的显式重载。 The removal of the operator has not changed comparison semantics.删除运算符并没有改变比较语义。

All containers have had their operator!= removed, as far as I can tell (check eg the vector synopsis ).据我所知,所有容器都已将其operator!=删除(例如检查向量概要)。 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.唯一的例外是容器适配器std::queuestd::stack :我的猜测是它在与第三方容器一起使用时保持向后兼容性,以防相等运算符不对称。

We don't need a library provided operator!= anymore.我们不再需要库提供的operator!=了。 Providing operator== allows the compiler to do some juggling and evaluate a != b in terms of a == b , all on its own.提供operator==允许编译器根据a == b进行一些杂耍和评估a != b ,这一切都是独立的。

[over.match.oper] [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对于具有 cv 非限定版本为 T1 的类型的操作数的一元运算符 @,以及具有 cv 非限定版本为 T1 的类型的左操作数和 cv-非限定版本的类型的右操作数的二元运算符 @不合格版本为T2,四组候选函数,指定成员候选、非成员候选、内置候选和重写候选,构造如下:

3.4.3 For the.= operator ([expr,eq]). 3.4.3对于.= 运算符([expr,eq])。 the rewritten candidates include all non-rewritten candidates for the expression x == y.重写的候选项包括表达式 x == y 的所有未重写的候选项。

std::type_info and many more library types had their operator!= removed as part of P1614 - The Mothership has Landed . std::type_info和更多库类型的operator!=作为P1614 的一部分被删除 - The Mothership has Landed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM