Search code examples
c++c++20std-pair

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


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

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 !=?


Solution

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