isocpp / CppCoreGuidelines

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
Other
42.58k stars 5.43k forks source link

[C++20] C.86: Make == symmetric with respect to operand types #2001

Open qchateau opened 1 year ago

qchateau commented 1 year ago

[intentionally ignoring "and noexcept" because it's unrelated]

As far as I understand, with C++20 rewritten expressions, an operator== defined as a member function is now symmetric and the assertion

B’s comparison accepts conversions for its second operand, but not its first.

in the guidelines does not hold anymore.

I would still teach people to define their operators as non-member because other operators are not symmetric and it's easier to have just one general rule "define operators as non-member functions".

But is my understand correct here ? If it is, do you agree we should change the wording of C.86 ?

MikeGitb commented 1 year ago

As far as I understand, with C++20 rewritten expressions, an operator== defined as a member function is now symmetric and the assertion

I don't think that is entirely true. AFAIK, the compiler can rewrite A == B into B == A, but that doesn't change the fact that member operator== wil not treat left and right operator identical.

However, I'm not sure if/when that makes a practical difference.