What is meant by the statement: "operator != has been removed in C++20 for std::pair"?

huangapple go评论81阅读模式
英文:

What is meant by the statement: "operator != has been removed in C++20 for std::pair"?

问题

与上面的问题一样。我正在阅读关于std::pair的内容,该内容位于cppreference上,其中写着:

operator==
operator!=  // (removed in C++20)
operator<   // (removed in C++20)
operator<=  // (removed in C++20)
operator>   // (removed in C++20)
operator>=  // (removed in C++20)
operator<=> // (C++20)

为了验证这一点,我编写了以下代码,在最新的gcc编译器上似乎运行得很完美,而我预期编译器会报语法错误,那么cppreference中写的removed in C++20是什么意思呢?

英文:

Exactly the question above. I was reading about std::pair on cppreference where the following was written:

> cpp
&gt; operator==
&gt; operator!= // (removed in C++20)
&gt; operator&lt; // (removed in C++20)
&gt; operator&lt;= // (removed in C++20)
&gt; operator&gt; // (removed in C++20)
&gt; operator&gt;= // (removed in C++20)
&gt; operator&lt;=&gt; // (C++20)
&gt;

To verify this, I have written the following code, which seems to be running perfectly on the latest gcc compiler, whereas I expected a syntax error from the compiler, so what does cppreference mean by writing removed in C++20 for operator !=?

答案1

得分: 3

线索在于C++20中添加的内容,即<=>操作符。

<=>是_三路比较_操作符,也被称为_飞船_操作符。它的目的是将所有类型的相等比较(<>=!=等等)定义为一个单一的函数。

由于其他比较操作符是使用三路比较操作符隐式提供的,因此不再需要单独的操作符实现,它们已被移除。

编辑:

事实证明,我完全错了。a != b并不被重写为使用a <=> b;它被重写为使用!(a == b);请参见被标记为重复的问题的答案。

英文:

The clue is in what was added in C++20, i.e. the &lt;=&gt; operator.

&lt;=&gt; is the three-way comparison operator, also known as the spaceship operator. Its purpose is to define all types of equality comparisons (&lt;, &gt;=, !=, and so on) as a single function.

Since the other comparison operators are implicitly provided using the three-way comparison operator, the individual operator implementations are no longer needed and have been removed.

EDIT:

Turns out I'm completely wrong here. a != b isn't rewritten to use a &lt;=&gt; b; it's rewritten to use !(a == b); see answer to the question that this is marked as a duplicate of.

huangapple
  • 本文由 发表于 2023年7月13日 18:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定