问题描述
在更改了代码库之后,我遇到了这个问题:
After some change of the code-base I came accross this gotcha:
#include <string>
void test(const std::string& s){
}
int main()
{
test(NULL);
return 0;
}
这会引发异常。更改为 nullptr无济于事(仍然没有错误或警告)。
This throws an exception. Changing to 'nullptr' helps nothing (still no error or warning).
我想我的问题是,是否有办法在运行前检测或发现此错误?整个源代码?也许有些编译器警告,等等。(使用MSVC VS-2017)
I guess my question is, is there a way to detect or find this error at pre-runtime throughout the sourcecode ? perhaps some compiler warning, etc. (using MSVC VS-2017)
我最终修改了basic_string模板ala。 basic_string(int)=删除;
-这不会捕获所有情况,但确实确实至少捕获了直接情况
basic_string(:: std :: nullptr_t)= delete;
I ended up modifying the basic_string template ala. basic_string(int) = delete; basic_string(::std::nullptr_t) = delete;
- this won't catch all cases but does indeed seem to catch the direct cases at least
推荐答案
运行(版本1.89)将产生以下结果:
Running cppcheck
(version 1.89) on the example file yields:
Checking test.cpp ...
test.cpp:9:10: error: Null pointer dereference [nullPointer]
test(NULL);
^
这篇关于从NULL构造字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!