When I compiled the code below using GCC 12, I encountered an error saying "no match for 'operator=='"
, but the code compiles fine with GCC 11.
#include <iostream>
#include <algorithm>
class EqualityChecker
{
public:
template <typename T>
static bool checkEquality(const T lhs, const T rhs)
{
return *lhs == *rhs;
}
};
namespace Foo
{
class ValueEntity
{
private:
int val;
public:
int value() const { return val; }
};
}
bool operator==(const Foo::ValueEntity &lhs, const Foo::ValueEntity &rhs)
{
return lhs.value() == rhs.value();
}
int main()
{
Foo::ValueEntity v1;
Foo::ValueEntity v2;
bool equal = EqualityChecker::checkEquality(&v1, &v2);
std::cout << "equal: " << equal << std::endl;
return 0;
}
Compile command:
g++-12 --std=c++20 main.cpp
Error output:
main.cpp: In instantiation of 'static bool EqualityChecker::checkEquality(T, T) [with T = Foo::ValueEntity*]':
main.cpp:36:46: required from here
main.cpp:10:17: error: no match for 'operator==' (operand types are 'Foo::ValueEntity' and 'Foo::ValueEntity')
10 | return *lhs == *rhs;
Detail compilation result can be checked at https://godbolt.org/z/9jnMbYWvn
It seems the issue is related to C++'s ADL rules, but what causes the difference between different GCC versions? What is the correct behavior supposed to be?
'operator==' should be declared prior to the call site or in namespace 'Foo'
– Millham