Is there a reason why in c++ std::string
is not implicitly converted to bool? For example
std::string s = \"\"
if (s) { /* s in not empty */ }
This article mentions some reasons why operator bool()
can lead to surprising results.
Note that std::string
is just a typedef for std::basic_string
. There is also std::wstring
for multi-byte characters. An implicit conversion would let you write:
std::string foo = "foo";
std::wstring bar = "bar";
if (foo == bar) {
std::cout << "This will be printed, because both are true!\n";
}