指向std :: string的空指针 [英] Empty pointer to std::string

查看:123
本文介绍了指向std :: string的空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我发现了一些有趣的东西。

我的意思是你怎么看待这个:


char * p = 0;

std :: string str = p;


为什么std :: string不检查空指针?


为什么不做一些检查:


class String:public std :: string

{

public:

String():std :: string(){}

String(const char * str):std :: string() {

if(str == 0)

* this = std :: string();

else

* this = std :: string(str);

}

String(const std :: string& str):std :: string(str){}

};


这只是一个例子,'因为我不想从头开始写

字符串类,所以我只是派生出来的。 :)

Hi!

I just found something interesting.
I mean what do you think about this:

char *p = 0;
std::string str = p;

Why std::string doesn''t check null pointers?

Why not doin'' some checking:

class String : public std::string
{
public:
String():std::string() {}
String(const char *str):std::string() {
if(str == 0)
*this = std::string();
else
*this = std::string(str);
}
String(const std::string& str):std::string(str) {}
};

This is only an example, ''cause i didn''t want to write from scratch
string class, so i just derived. :)

推荐答案

mr_sorcerer写道:
mr_sorcerer wrote:

嗨!


我发现了一些有趣的东西。

我的意思是你怎么看待这个:


char * p = 0 ;

std :: string str = p;


为什么std :: string不检查空指针?
Hi!

I just found something interesting.
I mean what do you think about this:

char *p = 0;
std::string str = p;

Why std::string doesn''t check null pointers?



因为标准更喜欢未定义的行为浪费CPU周期。

Because the standard prefers undefined behavior to wasting CPU cycles.


为什么不这样做''有些检查:


class String:public std :: string

{

public:

String():std :: string(){}

字符串(const char * str):std :: string(){

if(str == 0)

* this = std :: string();

else

* this = std :: string(str);

}

String(const std :: string& str):std :: string(str){}

};


这只是一个例子,'因为我不想从头开始写

字符串类,所以我只是派生了。 :)
Why not doin'' some checking:

class String : public std::string
{
public:
String():std::string() {}
String(const char *str):std::string() {
if(str == 0)
*this = std::string();
else
*this = std::string(str);
}
String(const std::string& str):std::string(str) {}
};

This is only an example, ''cause i didn''t want to write from scratch
string class, so i just derived. :)



随意这样做。我更喜欢断言(str!= 0)。这样,我将使用String进行测试,并且当我确信我成功避免UB时,可以用std :: string替换它。


顺便说一句:我发现有一个命名空间调试有点整洁,它提供了

debug :: vector<>,debug :: string,...具有已定义的行为(断言) )相反

of UB。

Best


Kai-Uwe Bux

Feel free to do that. I would prefer an assert( str != 0 ). That way, I
would use String for testing and could replace it with std::string when I
am convinced that I avoided UB successfully.

BTW: I find it is somewhat neat to have a namespace debug that provides
debug::vector<>, debug::string, ... with defined behavior (assert) instead
of UB.
Best

Kai-Uwe Bux


On Wed,04 Apr 2007 06:24:40 -0400,Kai-Uwe Bux写道:
On Wed, 04 Apr 2007 06:24:40 -0400, Kai-Uwe Bux wrote:

> mr_sorcerer写道:
>mr_sorcerer wrote:

>我刚发现了一些有趣的东西。
我的意思是你怎么看待这个:

char * p = 0;
std :: string str = p;

为什么std :: string不检查空指针?
>I just found something interesting.
I mean what do you think about this:

char *p = 0;
std::string str = p;

Why std::string doesn''t check null pointers?


因为标准更喜欢未定义的行为浪费CPU周期。


Because the standard prefers undefined behavior to wasting CPU cycles.



OTOH,公共成员函数应验证输入。我会考虑好的风格


OTOH, a public member function should validate input. I''d consider
that good style.


>顺便说一句:我发现提供一个命名空间调试有点整洁
debug :: vector<>,debug :: string,...定义了行为(断言),而不是UB。
>BTW: I find it is somewhat neat to have a namespace debug that provides
debug::vector<>, debug::string, ... with defined behavior (assert) instead
of UB.



断言由NDEBUG #define启用/禁用。断言使用''命名空间

NDEBUG'';-)

-

Roland Pibinger

"最好的软件简单,优雅,充满戏剧性。 - Grady Booch

assert is en-/disabled by the NDEBUG #define. assert uses ''namespace
NDEBUG'' ;-)
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch


Roland Pibinger写道:
Roland Pibinger wrote:

2007年4月4日星期三06:24:40 - 0400,Kai-Uwe Bux写道:
On Wed, 04 Apr 2007 06:24:40 -0400, Kai-Uwe Bux wrote:

>> mr_sorcerer写道:
>>mr_sorcerer wrote:

>> ;我刚发现了一些有趣的东西。
我的意思是你怎么看待这个:

char * p = 0;
std :: string str = p;

为什么std :: string没有检查空指针?
>>I just found something interesting.
I mean what do you think about this:

char *p = 0;
std::string str = p;

Why std::string doesn''t check null pointers?


因为标准更喜欢未定义的行为浪费CPU周期。


Because the standard prefers undefined behavior to wasting CPU cycles.



OTOH,公共成员函数应验证输入。我会考虑好的风格



OTOH, a public member function should validate input. I''d consider
that good style.



是的,但这是一个实施质量问题。标准没有

要求标准容器的公共成员功能执行任何操作。

当然,您可以购买/实施然后使用标准

库实现,包括你想要的assert()语句。

True, but that is a quality of implementation issue. The standard does no
require public member functions of standard containers to do any of that.
Of course, you could just purchase / implement and then use a standard
library implementation that includes the assert() statements you want.


>> BTW :我发现有一个命名空间调试有点整洁,它提供了
debug :: vector<>,debug :: string,...具有定义的行为(断言)而不是UB。
>>BTW: I find it is somewhat neat to have a namespace debug that provides
debug::vector<>, debug::string, ... with defined behavior (assert) instead
of UB.



断言由NDEBUG #define启用/禁用。断言使用''命名空间

NDEBUG'';-)


assert is en-/disabled by the NDEBUG #define. assert uses ''namespace
NDEBUG'' ;-)



是的。但是,只有当您使用标准库

实现输入验证,范围检查等时才有效。

否则,您必须滚动自己的代码。

最好


Kai-Uwe Bux

Yes. However, that only works if you are using an standard library
implementation that does input validation, range checking, and so on.
Otherwise, you have to roll your own code.
Best

Kai-Uwe Bux


这篇关于指向std :: string的空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆