The bullet given at
class.compare.default seems to make the following program invalid. I don't think this is the intention. That is, I think it should be valid but the current wording makes it invalid. We see compiler divergence here.
Demo
```
struct A {
friend bool operator==(const A, A) = default; //gcc and edg ok but clang and msvc no
};
That depends on whether you think that a function parameter of type `const C` is a function parameter of type `C`, or not.
Argument in favor: Consider that `int f(C)` and `int f(const C)` declare the same function; so I'd think it reasonable to say that that function's parameter must have the same type as itself.
Argument against: The codegen of that defaulted operator depends on the parameters' const-qualification: if they're const-qualified, it's descending into the members' const-qualified `operator==`, and otherwise it's descending into the members' non-const-qualified `operator==`, which could do something totally different. So if both are legal, then they will be able to have different codegen.
...Which leads inexorably to at least one conclusion: it can't be legal to have both simultaneously.
struct A {
friend bool operator==(A, A) = default;
friend bool operator==(const A, const A) = default;
};
is asking for two functions, with the exact same signature, but different behaviors. That has to be ill-formed.
(
Today: MSVC accepts, GCC prints some nonsense, EDG rejects followed by some nonsense, and Clang correctly rejects.)
Notice that GCC trunk is totally broken in the presence of non-const (mutating) `operator==` candidates:
struct A {
int i;
bool operator==(A&) { i = 42; return true; }
};
struct B {
A a;
bool operator==(const B&) const = default; // This is supposed to be defined-as-deleted, because (this->a == this->a) would be ill-formed.
};
int main() {
B b;
b.a.i = 0;
(void)(b == b);
return b.a.i; // GCC returns 42!
}
I think there are two good ways forward here:
- Make `friend bool operator==(const A, const A) = default;` ill-formed. Arguably, we already have that wording in the Standard; vendors just aren't interpreting it quite right (except for Clang).
- Respecify `friend bool operator==(A, A) = default;` to descend into the const-qualified `operator==`s of its subobjects, even though its two parameters aren't actually const-qualified in the signature — in other words, make a defaulted `operator==(A, A)` behave the same way as a defaulted `operator==(const A, const A)`, so that it doesn't matter which one the programmer writes.
Do you have the ability to ask for a CWG issue?
Notice that this area was slightly modified in C++26 by P2953, and will be slightly modified in C++29 by P2952 — but I don't think either of those papers affect this particular point.
Cheers,
Arthur