为什么一个std :: string的前一个带引号的字符串匹配布尔方法的签名? [英] Why does a quoted string match bool method signature before a std::string?
问题描述
鉴于以下方法:
// Method 1
void add(const std::string& header, bool replace);
//Method 2
void add(const std::string& name, const std::string& value);
这样看来,以下code将最终调用,而不是方法2方法1:
It would appear that the following code will end up calling method 1 instead of method 2:
something.add("Hello", "World");
我结束了创建看起来像这样的另一种方法:
I ended up creating another method that looks like this:
//Method 3
void MyClass::add(const char* name, const char* value) {
add(std::string(name), std::string(value));
}
它的工作。所以它似乎是,当方法接受引用的字符串这将在下面顺序匹配
It worked. So it would seem that when a method accepts a "quoted string" it will match in the following order:
-
为const char *
-
布尔
-
的std ::字符串
const char*
bool
std::string
为什么要带引号的字符串被视为布尔
A 的std ::字符串
过吗?这是一般的行为吗?我已经写了code,像样的数目为这个项目,还没有任何其他问题与错误的方法签名被选中...
Why would a quoted string be treated as a bool
before a std::string
? Is this the usual behavior? I have written a decent amount of code for this project and haven't had any other issues with the wrong method signature being selected...
推荐答案
我的猜测是指向布尔的转换是一个隐含的基本类型的转换,在转换为的std ::字符串
需要一个构造函数的调用,一个临时的建设。
My guess is the conversion from pointer to bool is an implicit primitive type conversion, where the conversion to std::string
requires the call of a constructor and the construction of a temporary.
这篇关于为什么一个std :: string的前一个带引号的字符串匹配布尔方法的签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!