https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92847
Bug ID: 92847
Summary: [C++20] ambiguous overload for ‘operator==’ ?
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: laurent.stacul at gmail dot com
Target Milestone: ---
Hello,
Compiling the following reproducer with the gcc trunk and the option
-std=gnu++2a (c++2a), I get the following error:
#include <iostream>
template<typename T>
class A {
public:
A() {}
template<typename U>
A(const A<U>&) {}
bool operator==(const A<T>&) { return true; }
};
int main(int argc, const char *argv[])
{
A<const std::string> a;
A<std::string> b;
if (a == b) {}
return 0;
}
a.cpp: In function ‘int main(int, const char**)’:
a.cpp:25:11: error: ambiguous overload for ‘operator==’ (operand types are
‘A<const std::__cxx11::basic_string<char> >’ and
‘A<std::__cxx11::basic_string<char> >’)
25 | if (a == b) {}
| ~ ^~ ~
| | |
| | A<basic_string<[...]>>
| A<basic_string<[...]>>
a.cpp:15:10: note: candidate: ‘bool A<T>::operator==(const A<T>&) [with T =
std::__cxx11::basic_string<char>]’
15 | bool operator==(const A<T>&) {
| ^~~~~~~~
a.cpp:15:10: note: candidate: ‘bool A<T>::operator==(const A<T>&) [with T =
const std::__cxx11::basic_string<char>]’
When I compile with gcc 9.2, I have no issue. The generated code is the one I
expect (implicit call to the constructor A<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >
const>::A<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > >(A<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > > const&) then call to the
A<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>
> const>::operator==(A<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const> const&).
Can you tell me which C++ 20 rule I am breaking here ?
Thanks in advance for your reply,
Laurent