以'!“在C / C ++很好的做法,它是常见的? [英] Is using '!!' in C/C++ good practice and is it common?

查看:86
本文介绍了以'!“在C / C ++很好的做法,它是常见的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  在C ++中code 双重否定


据我所知,没有C / C ++书籍教程或手册提到了这一技术。也许是因为它只是一个小小的东西,不值一提。

我使用它,因为C / C ++混合使用的int,long,指针,双等bool类型...在一起。这是很常见的需要转换为非布尔为bool。这不是安全使用(布尔)值要做到这一点,所以我用 !! 来做到这一点。

例如:

 布尔bValue = !! otherValue;


解决方案

我觉得 !! 是相较于其他一些选择,如相当明确:

 如果(富)
  巴= 1;
其他
  巴= 0;

酒吧= foo的? 1:0;

由于 !! 不正是一件事情,我觉得很明确的。

Possible Duplicate:
Double Negation in C++ code

As 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屋!

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