10

A quote from the standard regarding std::basic_string_view equality comparison operators (see http://eel.is/c++draft/string.view#comparison):

[Example 1: A sample conforming implementation for operator== would be:

template<class charT, class traits>
constexpr bool operator==(basic_string_view<charT, traits> lhs,
                        basic_string_view<charT, traits> rhs) noexcept {
    return lhs.compare(rhs) == 0;
}
template<class charT, class traits>
constexpr bool operator==(basic_string_view<charT, traits> lhs,
                        type_identity_t<basic_string_view<charT, traits>> rhs) noexcept {
    return lhs.compare(rhs) == 0;
}

— end example]

Won't the second comparison operator be sufficient for all use cases? If the answer is no please provide the example code that will stop working (or will work differently) if the first comparison operator is removed. If the answer is yes then why does the C++ standard explicitly require the first operator to be defined?

2
  • wandbox.org/permlink/v3eaSEgJUmBYhFU0 I think the second operator including std::type_identity_t won't work for this case.
    – frozenca
    Jan 25, 2022 at 14:04
  • 1
    @frozenca For that case only the second overload works. The first one would fail to deduce the second parameter. Jan 25, 2022 at 14:31

1 Answer 1

10

I think this is insufficient reduction as a result of the adoption of <=> in P1614. Before that paper, there were three ==s in the example:

template<class charT, class traits>
    constexpr bool operator==(basic_string_view<charT, traits> lhs,
                              basic_string_view<charT, traits> rhs) noexcept {
      return lhs.compare(rhs) == 0;
    }
  template<class charT, class traits>
    constexpr bool operator==(basic_string_view<charT, traits> lhs,
                              type_identity_t<basic_string_view<charT, traits>> rhs) noexcept {
      return lhs.compare(rhs) == 0;
    }
 template<class charT, class traits>
   constexpr bool operator==(type_identity_t<basic_string_view<charT, traits>> lhs,
                             basic_string_view<charT, traits> rhs) noexcept {
     return lhs.compare(rhs) == 0;
   }

At the time, we needed three operators. The type_identity ones handle stuff like sv == literal and literal == sv, and then you need the homogeneous one to disambiguate sv == sv.

With the <=> adoption (or, more precisely, the == changes from P1185), == becomes symmetric so you don't need both type_identity operators to handle literal == sv, just the one suffices. I was basically mechanically going through and dropping unnecessary == and != overloads, so I removed that second.

But what I did not realize is with the other one gone, we now no longer need the homogeneous comparison to disambiguate from the other two (we don't have other two anymore, just other one) - it's enough to just have the one type_identity overload.

You could open an editorial issue to remove the homogeneous one. Or not, it's just an example anyway.

1
  • 3
    That unnecessary first comparison operator is present not only in the example but also the mandatory [string.view.synop] section of the standard. If it really is not necessary it must be removed - making library code shorter and rising less questions from users like me. Could you pretty please deal with opening an editorial issue yourself since even if I open it you probably end up being the one fixing the standard text anyway?
    – PowerGamer
    Jan 25, 2022 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.