Problem with Null <std :: string>

I have developed below Null class for general programming and I can do something like if TA = Null () everything works fine except for std :: string where the compiler cannot find the correct == operator and give me a lot of errors ... The problem is, why do other types work well? Did I do something wrong?

struct Null
{
    operator std::string() const { return std::string{}; }
    operator int() const { return 0; }
};

int main() {
    std::string s = "hello";
    Null n;

    std::cout << (0 == n) << std::endl; // works
    std::cout << (n == 0) << std::endl; // works
    std::cout << (s == n) << std::endl; // error: no match for operator==
}

      

+3


source to share


1 answer


Used ==

in fact:

template< class CharT, class traits, class Alloc >
bool operator==( const basic_string<CharT,Traits,Alloc>& lhs, 
             const basic_string<CharT,Traits,Alloc>& rhs );

      



Custom conversion sequences are not counted for template type inference, so it cannot output parameter CharT

(or others) here.

To fix this, you might have to define your own custom template operator==

.

+4


source







All Articles