为什么/何时使用 (!!p) 而不是 (p != NULL)

发布于 2024-08-08 08:25:45 字数 213 浏览 2 评论 0原文

在下面的代码中,使用 (!!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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一江春梦 2024-08-15 08:25:45

它几乎是一样的,尽管我认为 !!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.

微暖i 2024-08-15 08:25:45

这是风格问题,事实上它们是等效的。请参阅这个非常相似的问题进行讨论。

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.

半山落雨半山空 2024-08-15 08:25:45

我认为 GMan 的原始评论应该是公认的答案:

我想知道 if (p) 有什么问题

重点是:它没有什么问题,而且这个应该是首选方式。首先,!!p“太聪明了”;它也是完全不必要的,因此很糟糕(注意:我们在这里讨论 if 语句中的指针,因此 Anacrolix 的注释虽然通常有效,但并不适用于此!)。

p != NULL 也是如此。虽然这是可能的,但只是没有必要。这是更多的代码,完全是冗余代码,因此它使代码变得更糟。杰夫·阿特伍德说过的最真实的话是“最好的代码就是根本没有代码”。避免冗余语法。坚持最小化(仍然传达完整的含义;if (p) is 完整)。

最后,if (p) 可以说是用 C++ 编写此代码的最惯用的方式。 C++ 竭尽全力为语言中的其他类型(例如数据流)提供相同的行为,但代价是一些非常奇怪的怪癖。该标准的下一版本甚至引入了新的语法来在用户定义类型中实现此行为。

对于指针,我们免费获得相同的指针。所以使用它。

/编辑:关于清晰度:sharptooth 写道

IMO 与空指针的比较更清楚。

我声称这在客观上是错误的:if (p) 更清楚。在 C++ 中,无论是在此上下文中还是在任何其他上下文中,该语句都不可能表示任何其他含义。

I thing GMan’s original comment should be the accepted answer:

I wonder what's wrong with just if (p)

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 an if 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

IMO comparing against null pointer is clearer.

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++.

一曲爱恨情仇 2024-08-15 08:25:45

据我所知,这只是将其转换为布尔值的更短方法。它适用于!但是,两次比较,而 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.

‖放下 2024-08-15 08:25:45

它们是相同的,但我建议使用

NULL != p

它更具可读性。

They are the same, but I recommend to use

NULL != p

It is more readable.

优雅的叶子 2024-08-15 08:25:45

给定的示例没有区别。

然而,这适用于所有情况的假设是不正确的。就整数类型而言,a = not not ba = b 不同。

在 C 语言中,0 为 false。除 0 之外的任何内容都是 true。但not 01,仅此而已。在 C++ 中,true 将 1 转换为整数,不仅是为了与 C 向后兼容,而且因为 1 不是 0,并且 1 是 C bool 类型中最常用的表示 true 的值,包括 官方 C bool 类型,以及 Win32 中使用的 BOOL

虽然对于给出的示例代码,!!p 是不必要的,因为结果被转换为 bool 来评估 if 条件,但这并不不排除使用 !! 将布尔值转换为预期的整数值。就我个人而言,在这个例子中,为了最大限度地提高类型更改和语义清晰的可能性,我会使用 NULL != pp != 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 but 0 is true. But not 0 is 1, and nothing else. In C++, true casts to 1 as an integer, not only for backward compatibilty with C, but because 1 is not 0, and 1 is the most common value used to denote true in C bool types, including the official C bool type, and BOOL used in Win32.

While for the example code given, !!p is unnecessary because the result is cast to a bool for evaluation of the if 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 use NULL != p or p != 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.

街道布景 2024-08-15 08:25:45

不要使用双重否定。一个简单的论点是,由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文