CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

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

Open qchateau opened this issue 3 years ago • 1 comments

[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 ?

qchateau avatar Nov 29 '22 11:11 qchateau

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.

MikeGitb avatar Nov 29 '22 19:11 MikeGitb