void foo (const std::string &s) {}
int main() {
foo(0); //compiles, but invariably causes runtime error
return 0;
}
编译器(g++ 4.4)显然解释0
为char* NULL
,并s
通过调用string::string(const char*, const Allocator &a = Allocator())
. 这当然没用,因为NULL
指针不是指向 c 字符串的有效指针。当我尝试调用时不会出现这种误解foo(1)
,这有助于产生编译时错误。
当我不小心调用类似的函数时,是否有可能在编译时出现这样的错误或警告
void bar(const std::string &s, int i=1);
with bar(0)
, 忘记 the string
, 真正的意思 to have i=0
?