I am curious about the following code, somebody could explain why does it call bool func first ? isn`t “str” more suitable for arg type string ?
void a(bool input)
{
cout<<"I amd first"<<endl;
cout<<input<<endl;
}
void a(const string &input)
{
cout<<"I amd second"<<endl;
cout<<input<<endl;
}
int main( )
{
a("str"); // call void a(bool input)
a(string("str")); //call void a(const string &input)
return 0;
}
"str"is of typeconst char[4], which decays immediately toconst char *, and the conversion from any pointer type toboolis considered before non-explicit constructors to custom types.So, I’d say that the answer is "because the standard says so".
The relevant passage should be 13.3.3.2 ¶2: