My understanding is that using "x" will create a const char[2] (see What is the difference between 'a' and "a"?).
So if one were to write
#include <string>
int main(int argc, char* argv[]) {
std::string testString("test");
if (testString == "test") {
cout << "Success" << endl;
}
else {
cout << "Failure" << endl;
}
}
then my understanding is that you'd be comparing between types std::string and const char[2].
Is this true, is it good practice, and does it work with all C++ compilers?
I found a useful page on this site, strcmp or string::compare?, which features an answer talking about "overloaded" operators, though even after researching I am still unsure what exactly they are. My educated guess is that the compiler recognises "==" after a string and so converts the const char[2] to a std::string and then uses string::compare.