为什么/何时使用 (!!p) 而不是 (p != NULL)
在下面的代码中,使用 (!!p)
代替 (p != NULL)
有什么好处?
AClass *p = getInstanceOfAClass();
if( !!p )
// do something
else
// do something without having valid pointer
In the following code, what is the benefit of using (!!p)
instead of (p != NULL)
?
AClass *p = getInstanceOfAClass();
if( !!p )
// do something
else
// do something without having valid pointer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它几乎是一样的,尽管我认为
!!p
是不好的风格,并且通常表明编码员试图变得聪明。It is pretty much the same, although I consider the
!!p
to be bad style, and usually indicates a coder trying to be clever.这是风格问题,事实上它们是等效的。请参阅这个非常相似的问题进行讨论。
IMO 与空指针比较更清楚。
That's a matter of style, in fact they are equivalent. See this very similar question for discussion.
IMO comparing against null pointer is clearer.
我认为 GMan 的原始评论应该是公认的答案:
重点是:它没有什么问题,而且这个应该是首选方式。首先,
!!p
“太聪明了”;它也是完全不必要的,因此很糟糕(注意:我们在这里讨论 if 语句中的指针,因此 Anacrolix 的注释虽然通常有效,但并不适用于此!)。p != NULL
也是如此。虽然这是可能的,但只是没有必要。这是更多的代码,完全是冗余代码,因此它使代码变得更糟。杰夫·阿特伍德说过的最真实的话是“最好的代码就是根本没有代码”。避免冗余语法。坚持最小化(仍然传达完整的含义;if (p)
is 完整)。最后,
if (p)
可以说是用 C++ 编写此代码的最惯用的方式。 C++ 竭尽全力为语言中的其他类型(例如数据流)提供相同的行为,但代价是一些非常奇怪的怪癖。该标准的下一版本甚至引入了新的语法来在用户定义类型中实现此行为。对于指针,我们免费获得相同的指针。所以使用它。
/编辑:关于清晰度:sharptooth 写道
我声称这在客观上是错误的:
if (p)
更清楚。在 C++ 中,无论是在此上下文中还是在任何其他上下文中,该语句都不可能表示任何其他含义。I thing GMan’s original comment should be the accepted answer:
The point is: nothing is wrong with it, and this should be the preferred way. First off,
!!p
is “too clever”; it’s also completely unnecessary and thus bad (notice: we’re talking about pointers in anif
statement here, so Anacrolix’ comment, while generally valid, doesn’t apply here!).The same goes for
p != NULL
. While this is possible, it’s just not needed. It’s more code, it’s completely redundant code and hence it makes the code worse. The truest thing Jeff Atwood ever said was that “the best code is no code at all.” Avoid redundant syntax. Stick to the minimum (that still conveys the complete meaning;if (p)
is complete).Finally,
if (p)
is arguably the most idiomatic way to write this in C++. C++ bends over backwards to get this same behaviour for other types in the language (e.g. data streams), at the cost of some very weird quirks. The next version of the standard even introduces new a syntax to achieve this behaviour in user-defined types.For pointers, we get the same for free. So use it.
/EDIT: About clarity: sharptooth writes that
I claim that this is objectively wrong:
if (p)
is clearer. There is no possible way that this statement could mean anything else, neither in this context nor in any other, in C++.据我所知,这只是将其转换为布尔值的更短方法。它适用于!但是,两次比较,而
p != NULL
进行一次比较。所以我想好处只是代码更短,尽管如果您不知道!!p
的含义,那就更神秘了。As far as I can see, it's just a shorter way to convert it into a boolean value. It applies the ! twice, though, whereas
p != NULL
does one comparison. So I guess the benefit is just shorter code, albeit more cryptic if you don't know what!!p
is supposed to mean.它们是相同的,但我建议使用
它更具可读性。
They are the same, but I recommend to use
It is more readable.
给定的示例没有区别。
然而,这适用于所有情况的假设是不正确的。就整数类型而言,a = not not b 与 a = b 不同。
在 C 语言中,
0
为 false。除0
之外的任何内容都是 true。但not 0
是1
,仅此而已。在 C++ 中,true 将1
转换为整数,不仅是为了与 C 向后兼容,而且因为1
不是0
,并且1
是 C bool 类型中最常用的表示 true 的值,包括 官方 C bool 类型,以及 Win32 中使用的BOOL
。虽然对于给出的示例代码,
!!p
是不必要的,因为结果被转换为bool
来评估if
条件,但这并不不排除使用!!
将布尔值转换为预期的整数值。就我个人而言,在这个例子中,为了最大限度地提高类型更改和语义清晰的可能性,我会使用NULL != p
或p != NULL
来绝对清楚什么是的意思。这种技术被称为 double-bang idiom,这个人提供了一些很好的理由。
There is no difference in the given example.
However the assumption that this applies to all cases is incorrect. a = not not b is not the same as a = b, as far as integer types are concerned.
In C,
0
is false. Anything but0
is true. Butnot 0
is1
, and nothing else. In C++, true casts to1
as an integer, not only for backward compatibilty with C, but because1
is not0
, and1
is the most common value used to denote true in C bool types, including the official C bool type, andBOOL
used in Win32.While for the example code given,
!!p
is unnecessary because the result is cast to abool
for evaluation of theif
condition, that doesn't rule out the use of!!
for purposes of casting booleans to expected integer values. Personally in this example, to maximize the probability that type changes and semantics are clear, I would useNULL != p
orp != NULL
to make it absolutely clear what is meant.This technique is known as the double-bang idiom, and this guy provides some good justifications.
不要使用双重否定。一个简单的论点是,由于 C++ 是英语的有限子集,而英语没有双重否定,那么说英语的人将很难解析正在发生的事情。
Do !!NOT use double negation. A simple argument is that since C++ is a limited English subset and english just does not have a double negation then english speakers will have a lot of difficulty to parse what is going on.