我正在编写一个程序,该程序应该处理c字符串(char *)和c++字符串(std::string)。我出于关注而孤立了下面的示例。
#include <iostream>
#include <string>
void hello(std::string s) {
std::cout << "STRING FUNCTION" << std::endl;
}
void hello(char* c) {
std::cout << "CHAR FUNCTION" << std::endl;
}
int main(int argc, char* argv[]) {
hello("ambiguous");
hello((std::string)"string");
hello((char*)"charp");
return 0;
}
当我编译该程序时,我得到警告:
test.cpp:14: warning: deprecated conversion from string constant to ‘char*’
关于首次调用
hello
。运行该程序可以得到:
CHAR FUNCTION
STRING FUNCTION
CHAR FUNCTION
表明对
hello
的第一次调用与签名hello(char* c)
相匹配。我的问题是,如果作为c++程序,字符串文字(
"ambiguous"
)是std::string,为什么将其强制转换为char*
然后匹配函数hello(char* c)
而不是保留为std::string并匹配hello(std::string s)
?我知道我可以进行杂注或-发出警告(并且我可以将char *转换为字符串而无需担心),但是我想知道为什么编译器甚至会费心地执行此转换,以及是否有一种方法可以告诉它不要。我正在使用g++ 4.4.3进行编译。
谢谢。
最佳答案
像"ambiguous"
这样的字符串文字的类型不是std::string
。 std::string
是仅库类型,没有任何语言魔力。字符串文字的类型实际上是const char[N]
,其中N
是文字的长度。
由于历史原因(向后兼容),字符串文字会隐式转换为char*
(违反const-correctness)。与“用户定义”转换为std::string
相比,此内置转换更可取,因此它会调用char*
函数并向您发出警告。
如果将hello
的签名更改为hello(const char* c)
,它可能不会再发出警告(但仍不会调用std::string
版本,这样做需要手动强制转换)。
关于c++ - C++将字符串隐式转换为char *匹配错误的函数签名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15535729/