void *而不是bool [英] void * instead of bool

查看:77
本文介绍了void *而不是bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




为什么会有void *转换来检查std :: istream故障位?为什么不

a转换为bool?


当我尝试从实际返回bool的函数返回一个istream时,

我得到以下警告(在VC .NET中):


警告C4800:''void *'':强制值为bool''true''或''false''

(性能警告)


我可以通过返回(stream!= 0)或(!! stream)来摆脱它,但看起来好像

丑陋的我。除此之外,我怀疑它可能会生成

不理想的代码。编译器不知道void *值在流的情况下只保留0或1

,并添加额外的代码以防止值超出

bool范围。至少那是警告试图说的。


有没有理由避免自动转换为bool和prever

转换为void *相反?


Marcin

解决方案



" Marcin Kalicinski" < KA **** @ poczta.onet.pl>在消息中写道

news:cb ********** @ korweta.task.gda.pl ...



为什么有void *转换来检查std :: istream故障位?为什么
不能转换为bool?


如果你有转换为bool,那么下面的废话会被编译


float f = cin;


转换为void *不太可能被意外调用。

当我尝试从实际返回
bool的函数返回istream时,我收到以下警告(in VC .NET):

警告C4800:''void *'':强制值为bool''true''或''false''
(性能警告)

我可以通过返回(stream!= 0)或(!! stream)来摆脱它,但它对我来说看起来很难看。除此之外,我怀疑它可能会产生不理想的代码。编译器不知道在流的情况下void *值仅保留0或1
,并添加额外的代码以防止值超出
bool范围。至少那是警告试图说的。


你可以这样做


返回some_stream.good();

有没有理由避免自动转换为bool和prever
转换为void *而不是?


见上文。

Marcin



john


Marcin Kalicinski写道:



为什么有void *转换来检查std :: istream失败位?为什么不转换为bool?


因为你绝对不希望这个编译:


ifstream if;

int j;


j = 2 + if;

当我尝试从实际返回bool的函数返回istream时,
我收到以下警告(在VC .NET中):

警告C4800:''void *'':强制值为bool''true''或''false''
(性能警告)
我可以通过返回(stream!= 0)或(!! stream)来摆脱它,但它看起来对我来说很难看。


你可以在VC ++的帮助下完全关闭那个警告吗

除此之外,我怀疑它可能会产生
代码不合适。


别担心。你永远不会注意到差异。

你的电脑每秒可以做1 * 10 ^ 9次操作而且你需要或多或少地担心1或2个指令。 />
编译器不知道void *值在流的情况下仅保留0或1,并添加额外的代码以防止值超出
bool范围。至少那是警告试图说的。


它是。

但实际上这根本不是问题。

有没有理由避免自动转换为bool和prever
转换为void *而不是?




见上文。

-

Karl Heinz Buchegger
kb******@gascad.at


> >除此之外,我怀疑它可能会产生

不是最优的代码。



不要担心。你永远不会注意到这种差异。
你的电脑每秒可以做大约1 * 10 ^ 9次操作,而你或多或少地担心1或2条指令。




我一直认为C ++比其他语言的主要优点是几乎可以从它生成最优代码。


不是这样的在这里,但是2个额外的指令有时会产生差异,没有

重要的是CPU会变得多快,因为总会有问题,因为它们仍然太慢了。

它们仍然太慢。


幸运的是,John已经指出stream.good()会在没有警告的情况下最佳地执行
和额外的2条指令,因此C ++会被保存

:-)

Marcin


Hi,

Why is there void* conversion to check for std::istream failure bit? Why not
a conversion to bool?

When I try to return an istream from a function that actually returns bool,
I get the following warning (in VC .NET):

warning C4800: ''void *'' : forcing value to bool ''true'' or ''false''
(performance warning)

I can get rid of it by returning (stream != 0) or (!!stream), but it looks
ugly to me. Besides that, I suspect that it might perhaps be generating
unoptimal code. Compiler does not know that void* value holds only 0 or 1 in
the case of streams, and adds extra code to prevent the value going out of
bool range. At least that''s what the warning is trying to say.

Is there any reason to avoid automatic conversion to bool and prever
conversion to void* instead?

Marcin

解决方案


"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:cb**********@korweta.task.gda.pl...

Hi,

Why is there void* conversion to check for std::istream failure bit? Why not a conversion to bool?
If you have a conversion to bool then the following nonsense would compile

float f = cin;

Conversion to void* is less likely to be called accidentally.

When I try to return an istream from a function that actually returns bool, I get the following warning (in VC .NET):

warning C4800: ''void *'' : forcing value to bool ''true'' or ''false''
(performance warning)

I can get rid of it by returning (stream != 0) or (!!stream), but it looks
ugly to me. Besides that, I suspect that it might perhaps be generating
unoptimal code. Compiler does not know that void* value holds only 0 or 1 in the case of streams, and adds extra code to prevent the value going out of
bool range. At least that''s what the warning is trying to say.
You could do this

return some_stream.good();

Is there any reason to avoid automatic conversion to bool and prever
conversion to void* instead?

See above.
Marcin



john


Marcin Kalicinski wrote:


Hi,

Why is there void* conversion to check for std::istream failure bit? Why not
a conversion to bool?
Because you definitily don''t want this to compile:

ifstream if;
int j;

j = 2 + if;

When I try to return an istream from a function that actually returns bool,
I get the following warning (in VC .NET):

warning C4800: ''void *'' : forcing value to bool ''true'' or ''false''
(performance warning)

I can get rid of it by returning (stream != 0) or (!!stream), but it looks
ugly to me.
YOu can turn off that warning completely in VC++ with the help of a pragma
Besides that, I suspect that it might perhaps be generating
unoptimal code.
Don''t worry about that. You will never notice the difference.
Your PC can do around 1 * 10^9 operations per second and you
worry about 1 or 2 instructions more or less.
Compiler does not know that void* value holds only 0 or 1 in
the case of streams, and adds extra code to prevent the value going out of
bool range. At least that''s what the warning is trying to say.
It is.
But in practice this isn''t a problem at all.

Is there any reason to avoid automatic conversion to bool and prever
conversion to void* instead?



See above.
--
Karl Heinz Buchegger
kb******@gascad.at


> > Besides that, I suspect that it might perhaps be generating

unoptimal code.



Don''t worry about that. You will never notice the difference.
Your PC can do around 1 * 10^9 operations per second and you
worry about 1 or 2 instructions more or less.



I always thought that C++ main advantage over other languages is that almost
optimal code can be generated from it.

Not the case here, but 2 extra instructions make difference sometimes, no
matter how fast will CPUs become, as there will always be problems where
they are still too slow.

Fortunately, John has already pointed out that stream.good() will do
optimally without warning and those extra 2 instructions, so C++ is saved
:-)

Marcin


这篇关于void *而不是bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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