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

时间:2023-01-26 11:48:07

In the following code, what is the benefit of using (!!p) instead of (p != NULL)?

在下面的代码中,使用(!!p)而不是(p != NULL)有什么好处?

AClass *p = getInstanceOfAClass();
if( !!p )
  // do something
else 
  // do something without having valid pointer

8 个解决方案

#1


9  

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.

IMO对空指针的比较更加清晰。

#2


11  

It is pretty much the same, although I consider the !!p to be bad style, and usually indicates a coder trying to be clever.

差不多是一样的,虽然我认为!!p表示不好的风格,通常表示程序员想要变得聪明。

#3


7  

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

我认为GMan最初的评论应该是被接受的答案:

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

我想知道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!).

关键是:它没有任何问题,这应该是首选的方式。首先,! !p是“太聪明”;它也完全没有必要,因此很糟糕(注意:我们这里讨论的是if语句中的指针,所以Anacrolix的注释虽然通常有效,但在这里不适用!)

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

同样的,p != NULL。虽然这是可能的,但并不需要。更多的代码,完全是冗余的代码,因此会使代码变得更糟。Jeff Atwood曾经说过的最真实的事情是“最好的代码根本就是没有代码”。“避免冗余的语法。坚持最低限度(仍然传达完整的意思;是否完成)(p)。

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.

最后,if (p)可以说是用c++写这个的最惯用的方式。c++为了获得语言中的其他类型(例如数据流)的相同行为,付出了一些非常奇怪的代价。标准的下一个版本甚至引入了新的语法来实现用户定义类型中的这种行为。

For pointers, we get the same for free. So use it.

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

/EDIT: About clarity: sharptooth writes that

/编辑:关于清晰度:夏普顿写的

IMO comparing against null pointer is clearer.

IMO对空指针的比较更加清晰。

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)更清楚的话。无论在本上下文中还是在其他任何场合,在c++中,这个语句都不可能有其他含义。

#4


3  

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.

在我看来,它只是一种将它转换成布尔值的更短的方法。它应用!但是,p != NULL进行了两次比较。所以我猜好处是代码更短,尽管如果你不知道会发生什么,它会更神秘!p应该是。

#5


0  

See this and this.

看看这个和这个。

#6


0  

They are the same, but I recommend to use

它们是一样的,但是我推荐使用

NULL != p

It is more readable.

它是更具可读性。

#7


-1  

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.

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

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.

在C中,0是假的。除了0之外的任何数都是正确的。但不是0是1,其他都不是。在c++中,true将一个整数转换为一个整数,而不仅仅是对C的向后兼容,但是因为1不是0,而1是C bool类型中最常用的值,包括C bool类型和Win32中使用的bool。

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.

而对于给出的示例代码,!!p是不必要的,因为结果被投到一个bool用来评估if条件,不排除使用!!为了将布尔值转换为期望的整数值。在本例中,为了最大限度地提高类型更改和语义的可能性,我将使用NULL != p或p != NULL,以使它完全清楚什么是真正的含义。

This technique is known as the double-bang idiom, and this guy provides some good justifications.

这种技术被称为“双重爆炸”习语,这个家伙提供了一些很好的理由。

#8


-2  

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.

做! !不使用双重否定。一个简单的论点是,既然c++是一个有限的英语子集,而英语只是没有双重否定,那么说英语的人就会有很多困难去解析正在发生的事情。

#1


9  

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.

IMO对空指针的比较更加清晰。

#2


11  

It is pretty much the same, although I consider the !!p to be bad style, and usually indicates a coder trying to be clever.

差不多是一样的,虽然我认为!!p表示不好的风格,通常表示程序员想要变得聪明。

#3


7  

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

我认为GMan最初的评论应该是被接受的答案:

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

我想知道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!).

关键是:它没有任何问题,这应该是首选的方式。首先,! !p是“太聪明”;它也完全没有必要,因此很糟糕(注意:我们这里讨论的是if语句中的指针,所以Anacrolix的注释虽然通常有效,但在这里不适用!)

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

同样的,p != NULL。虽然这是可能的,但并不需要。更多的代码,完全是冗余的代码,因此会使代码变得更糟。Jeff Atwood曾经说过的最真实的事情是“最好的代码根本就是没有代码”。“避免冗余的语法。坚持最低限度(仍然传达完整的意思;是否完成)(p)。

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.

最后,if (p)可以说是用c++写这个的最惯用的方式。c++为了获得语言中的其他类型(例如数据流)的相同行为,付出了一些非常奇怪的代价。标准的下一个版本甚至引入了新的语法来实现用户定义类型中的这种行为。

For pointers, we get the same for free. So use it.

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

/EDIT: About clarity: sharptooth writes that

/编辑:关于清晰度:夏普顿写的

IMO comparing against null pointer is clearer.

IMO对空指针的比较更加清晰。

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)更清楚的话。无论在本上下文中还是在其他任何场合,在c++中,这个语句都不可能有其他含义。

#4


3  

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.

在我看来,它只是一种将它转换成布尔值的更短的方法。它应用!但是,p != NULL进行了两次比较。所以我猜好处是代码更短,尽管如果你不知道会发生什么,它会更神秘!p应该是。

#5


0  

See this and this.

看看这个和这个。

#6


0  

They are the same, but I recommend to use

它们是一样的,但是我推荐使用

NULL != p

It is more readable.

它是更具可读性。

#7


-1  

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.

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

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.

在C中,0是假的。除了0之外的任何数都是正确的。但不是0是1,其他都不是。在c++中,true将一个整数转换为一个整数,而不仅仅是对C的向后兼容,但是因为1不是0,而1是C bool类型中最常用的值,包括C bool类型和Win32中使用的bool。

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.

而对于给出的示例代码,!!p是不必要的,因为结果被投到一个bool用来评估if条件,不排除使用!!为了将布尔值转换为期望的整数值。在本例中,为了最大限度地提高类型更改和语义的可能性,我将使用NULL != p或p != NULL,以使它完全清楚什么是真正的含义。

This technique is known as the double-bang idiom, and this guy provides some good justifications.

这种技术被称为“双重爆炸”习语,这个家伙提供了一些很好的理由。

#8


-2  

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.

做! !不使用双重否定。一个简单的论点是,既然c++是一个有限的英语子集,而英语只是没有双重否定,那么说英语的人就会有很多困难去解析正在发生的事情。