Issue with:
template <typename T>
bool operator== (const String<T>& a, const String<T>& b )
is the deduction of T
, and for "hello" == String<char>("hello")
, "hello"
doesn’t match const String<T>&
, so that overload is discarded.
What you can do is to make it non template:
template <typename T>
class String
{
// non template function.
friend bool operator==(const String& lhs, const String& rhs)
{
return UtilString<T>::Compare(a.m_buff, b.m_buff) == 0;
}
// ...
};
So "hello" == String<char>("hello")
would consider overload (thanks to rhs)
operator==(const String<char>& lhs, const String<char>& rhs)
and would construct String<char>
from "hello"
as constructor is viable.
solved how to code “hello” == obj without overload the operator==? [closed]