0%

C++ 缺陷

basic_string::_M_construct null not valid

C++有一个缺陷,请看以下代码:

1
2
3
4
5
6
7
8
//cpp defeat: basic_string::_M_construct null not valid
void fun(string s) {
cout << "fun" << endl;
}

int main() {
fun(0); // Run-time error: basic_string::_M_construct null not valid
}

其中,fun(0)0会被视为const char*类型,也就是nullptr,所以在编译期可以通过。
但是运行期会触发string对象的构造错误“basic_string::_M_construct null not valid”。

隐蔽一点的代码:

1
2
3
4
5
6
7
8
9
10
11
char * get_a_string() {
return nullptr;
}

int main() {
// Attention: Alaways take care that a parameter to a string should not be NULL!
fun(get_a_string()); // Run-time error: basic_string::_M_construct null not valid
// Better code
char * str = get_a_string();
fun(str != NULL? str : "");
}