mercredi 15 janvier 2020

Ambiguous overload for 'operator==' with own class and std::string_view

I created a class named MyClass in namespace N. Now I define a global operator== in namespace N.

namespace N
{
class MyClass
{
// member function begin()
// member function end()
// content omitted
}

template<typename T>
bool operator==(const MyClass& a_mc, const T& a_other)
{
    using namespace std;
    return std::equal(a_mc.begin(), a_mc.end(), begin(a_other));
}
}

This results in other code failing to compile with (included-from-stack omitted):

error: ambiguous overload for 'operator==' (operand types are 'std::basic_string_view' and 'const char [3]')

note: candidate: 'bool N::operator==(const N::MyClass&, const T&) [with T = char [3]]'

note: candidate: 'constexpr bool std::operator==(std::basic_string_view<_CharT, _Traits>, std::__detail::__idt >) [with _CharT = char; _Traits = std::char_traits; std::__detail::__idt > = std::basic_string_view]' 479 |
operator==(basic_string_view<_CharT, _Traits> __x

Why is N::operator== even considered?

--

Edit first code that hit the problem was

bool N::MyClass::f(const std::string_view& a_thing)
{return a_thing.substr(0,2) == "XY";}

Aucun commentaire:

Enregistrer un commentaire