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";}
--
Minimal working example:
#include <algorithm>
#include <string_view>
#include <type_traits>
namespace N
{
struct MyClass
{
MyClass() noexcept = default;
template<typename T,
typename = std::void_t<decltype(T{}.data())>,
typename = std::void_t<decltype(T{}.size())>>
MyClass(const T& a_source)
{}
const char* begin() const { return 0; } // stub
const char* end() const { return 0; } // stub
bool f(const std::string_view& a_thing);
};
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));
}
bool MyClass::f(const std::string_view& a_thing)
{
return a_thing.substr(0,2) == "XY";
}
}
Compile with
g++ -std=c++17 example.cpp