为什么要使用! INT转换时为bool? [英] Why use !! when converting int to bool?

查看:216
本文介绍了为什么要使用! INT转换时为bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么可能是一个原因,这样一个整数转换成一个布尔?

What can be a reason for converting an integer to a boolean in this way?

bool booleanValue = !!integerValue;

而不仅仅是

bool booleanValue = integerValue;

我所知道的是,在VC ++ 7,后者将导致 C4800警告和前不会。有没有两者之间的任何其他区别?

All I know is that in VC++7 the latter will cause C4800 warning and the former will not. Is there any other difference between the two?

推荐答案

与!!的问题成语是它的简洁,很难看到,易误认为一个错字,容易砸!的之一,等等。我把它放在看,我们可怎么可爱与C / C ++类别。
搜索结果
只要写布尔isNonZero =(integerValue!= 0); ...很清楚

The problems with the "!!" idiom are that it's terse, hard to see, easy to mistake for a typo, easy to drop one of the "!'s", and so forth. I put it in the "look how cute we can be with C/C++" category.

Just write bool isNonZero = (integerValue != 0); ... be clear.

这篇关于为什么要使用! INT转换时为bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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