Skip to content

[C++20] Defaulting a comparison operator on second declaration should be diagnosed #51227

Closed
@Quuxplusone

Description

@Quuxplusone
Bugzilla Link 51885
Version trunk
OS All
CC @zygoloid

Extended Description

https://eel.is/c++draft/class.compare.default#1.sentence-4

A definition of a comparison operator as defaulted that appears in a class shall be the first declaration of that function.

IIUC, this sentence is designed to prevent people from default'ing a "non-hidden" friend, like this:

// https://godbolt.org/z/6Tdac86TP
struct B;
bool operator==(const B&, const B&);
struct B {
friend bool operator==(const B&, const B&) = default;
};
int main() { B b; return b == b; }

However, Clang trunk does not diagnose any problem with this code. Instead, Clang quietly pretends that the friend declaration wasn't there at all.

FWIW, GCC also does not diagnose this code, but GCC quietly accepts the friend declaration (so we don't get any undefined symbol for operator== and the program links fine).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++20clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions