It is because of the following compare operator defined for std::string
template< class CharT, class Traits, class Alloc >
bool operator==( const basic_string<CharT,Traits,Alloc>& lhs, const CharT* rhs ); // Overload (7)
This allows the comparison between std::string
and the const char*
. Thus the magic!
Stealing the @Pete Becker 's comment:
"For completeness, if this overload did not exist, the comparison
would still work; The compiler would construct a temporary object of
type std::string
from the C-style string and compare the two
std::string
objects, using the first overload of
operator==
template< class CharT, class Traits, class Alloc >
bool operator==( const basic_string<CharT,Traits,Alloc>& lhs,
const basic_string<CharT,Traits,Alloc>& rhs ); // Overload (1)
Which is why this operator(i.e. overload 7) is there: it eliminates
the need for that temporary object and the overhead involved in
creating and destroying it."
std::string
from a c-string. – Uninterested