Chr*_*egg 3 c++ warnings g++ compiler-warnings clang++
我不明白为什么我没有收到警告(用g ++或clang ++)来返回NULL作为newtstr()
下面的对象:
#include<iostream>
using namespace std;
string newstr();
int main()
{
string s = newstr();
cout << s << endl;
return 0;
}
string newstr()
{
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
.
$ g++ -Wall -Wextra -pedantic testptr.cpp
$
Run Code Online (Sandbox Code Playgroud)
但是,我得到一个运行时错误:
$ ./a.out
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted
$
Run Code Online (Sandbox Code Playgroud)
.
$ g++ --version
g++ (GCC) 4.8.0
Run Code Online (Sandbox Code Playgroud)
std::string
有一个构造函数 basic_string( const CharT* s, const Allocator& alloc = Allocator() );
,它接受一个const char*
用于构造新字符串的指针(见(5)).该行return NULL;
导致对该构造函数的隐式调用,其中NULL指针作为参数 - 这是有效代码但显然无法正确运行.
归档时间: |
|
查看次数: |
85 次 |
最近记录: |