英文:
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
> 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)
>
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 <=>
operator.
<=>
is the three-way comparison operator, also known as the spaceship operator. Its purpose is to define all types of equality comparisons (<
, >=
, !=
, 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 <=> b
; it's rewritten to use !(a == b)
; see answer to the question that this is marked as a duplicate of.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论