问题描述
根据, std: :type_info :: operator!= 被C ++ 20删除,但是 std :: type_info :: operator == 显然仍然存在。 / p>
背后的原因是什么?我可能会同意比较不平等是没有意义的,但是然后比较相等也同样是没有意义的,不是吗?
类似地, operator!= 其他许多标准库类型,包括和将根据cppreference在C ++ 20中删除。 / p>
必须编写 if(!(id1 == id2))不会使任何代码更清晰相比之下, if(id1!= id2)正好相反...
在C ++ 20中,关系运算符的工作方式发生了变化,特别是引入了飞船< => 运算符。特别是,如果仅提供 operator == ,则 a!= b 将被重写为!(a == b)。
来自:
和:
因此, operator!= $的显式超载! c $ c>不再是必需的。删除操作符并没有改变比较语义。
所有容器都删除了 operator!= ,因为据我所知(检查)。唯一的例外是容器适配器 std :: queue 和 std :: stack :我的猜测是与相等的运算符不对称时,与第三方容器一起使用时可以保持向后兼容性。
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...
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:
And [over.match.oper]/9:
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.
这篇关于为什么在C ++ 20中对于许多标准库类型都删除了operator!=?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!