我对下面的代码很好奇,有人可以解释为什么它首先调用 bool func 吗? “str”不是更适合 arg 类型的字符串吗?
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"
是 const char[4]
类型,它会立即衰减到 const char *
,以及从任何指针的转换bool
的类型被认为是 before 自定义类型的非显式构造函数。
所以,我会说答案是“因为标准是这么说的”。
相关段落应该是13.3.3.2 ¶2:
When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)
- 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 [...]
关于c++ - 为什么它首先调用 bool_function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13554136/