以'!“在C / C ++很好的做法,它是常见的? [英] Is using '!!' in C/C++ good practice and is it common?
问题描述
可能重复:结果
在C ++中code 双重否定
块引用>据我所知,没有C / C ++书籍教程或手册提到了这一技术。也许是因为它只是一个小小的东西,不值一提。
我使用它,因为C / C ++混合使用的int,long,指针,双等bool类型...在一起。这是很常见的需要转换为非布尔为bool。这不是安全使用(布尔)值要做到这一点,所以我用
!!
来做到这一点。例如:
布尔bValue = !! otherValue;
解决方案我觉得
!!
是相较于其他一些选择,如相当明确:如果(富)
巴= 1;
其他
巴= 0;或
酒吧= foo的? 1:0;
由于
!!
不正是一件事情,我觉得很明确的。Possible Duplicate:
Double Negation in C++ codeAs far as I know, no C/C++ books tutorials or manuals mention this technique. Maybe because it's just a tiny little thing, not worth mentioning.
I use it because C/C++ mixes bool type with int, long, pointer, double etc...together. It's very common to need to convert a non-bool to bool. It's not safe to use (bool)value to do that, so I use
!!
to do it.Example:
bool bValue = !!otherValue;
解决方案I think
!!
is quite clear in comparison to some of the other choices such as :if (foo) bar = 1; else bar = 0;
or
bar = foo ? 1 : 0;
Since
!!
does exactly one thing, I find it very unambiguous.这篇关于以'!“在C / C ++很好的做法,它是常见的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!