String literal matches bool overload instead of std::string

"Hello World" is a string literal of type “array of 12 const char” which can be converted to a “pointer to const char” which can in turn be converted to a bool. That’s precisely what is happening. The compiler prefers this to using std::string‘s conversion constructor.

A conversion sequence involving a conversion constructor is known as a user-defined conversion sequence. The conversion from "Hello World" to a bool is a standard conversion sequence. The standard states that a standard conversion sequence is always better than a user-defined conversion sequence (§13.3.3.2/2):

a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence

This “better conversion sequence” analysis is done for each argument of each viable function (and you only have one argument) and the better function is chosen by overload resolution.

If you want to make sure the std::string version is called, you need to give it an std::string:

Output::Print(std::string("Hello World"));

Leave a Comment