Dan*_*zar 3 c++ compiler-construction stl compiler-warnings
说我有这段代码.
#include <string>
int main()
{
std::string(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
写入std::string(0)结果std::basic_string<char>::basic_string(const char*)被调用,0作为此构造函数的参数,尝试将参数视为指向C字符串的指针.
运行此代码显然会导致std::logic_error被抛出.但我的问题是:为什么GCC和MSVC 8.0都没有发出任何警告?我希望看到"从没有强制转换的整数制作指针"这一行.
0是一个整数常量表达式,其值为0,因此它是一个空指针常量.使用0值常量作为空指针不是强制转换.
C++ 11引入nullptr(和nullptr_t),但0作为空指针的处理在不久的将来不太可能改变,因为大量的代码依赖于它.