2 years ago

#67135

test-img

Fedor

std::equality_comparable concept checking in a context where private operator == is accessible

std::equality_comparable allows one to check that class objects can be compared using == and != operators. But what shall it return if the operators exist but available not in all contexts?

In the next program A::operator == is private and accessible only in friend function f. And if one checks that std::equality_comparable<A> is false before f then it will be false in f body as well:

#include <concepts>

class A {
    bool operator ==(const A&) const = default;
    friend void f();
};

static_assert( !std::equality_comparable<A> );
void f() {
    static_assert( !std::equality_comparable<A> );
}

This is accepted by GCC, Clang, and MSVC, demo: https://gcc.godbolt.org/z/Yqc38zobf

But if one first tests the concept inside f then in GCC and Clang it will be true, and will remain true after f:

class A {
    bool operator ==(const A&) const = default;
    friend void f();
};

void f() {
    static_assert( std::equality_comparable<A> );
}
static_assert( std::equality_comparable<A> );

Only MSVC still considers std::equality_comparable<A>==false here, demo: https://gcc.godbolt.org/z/Ko3Kqc9x7

Which compiler is right here?

c++

language-lawyer

c++20

private

c++-concepts

0 Answers

Your Answer

Accepted video resources