NoSenseEtAl
NoSenseEtAl

Reputation: 30026

Why C++20 allows defaulted comparison to compile even when it is deleted?

Consider the following code:

struct A {
};
struct B {
    A a;
    bool operator == (const B& other) const = default;
};

clang gives a nice warning :

warning: explicitly defaulted equality comparison operator is implicitly deleted [-Wdefaulted-function-deleted] bool operator == (const B& other) const = default;

But I wonder why is this code even accepted by the standard. I would assume that if somebody defaults the operator == in his nontemplate struct/class his intention is never to get deleted operator ==.

But this is C++ with a million corner cases so there might a good reason. Maybe not to special case templates?

But clang is smart enough to not warn on this code...

struct A {
};

template<typename T>
struct TS{
    T t;
    bool operator == (const TS& other) const = default;
};
int main() {
    TS<int> ti;
}

... so in theory standard could do the same.

Upvotes: 5

Views: 1925

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

In a template, you may want == if it can exist, and otherwise not.

The same technique is used for copy/move/assign special member functions; =default; can delete the member function as well.

Upvotes: 5

Related Questions