if (NULL == pointer) 与 if (pointer == NULL) 有什么区别?

使用有什么区别:

if (NULL == pointer) 

并使用:

if (pointer == NULL)    

我的教授说要使用前者而不是后者,但我看不出两者之间的区别.

My professor says to use the former over the latter but I don't see the difference between the two.

推荐答案

没有区别.您的教授喜欢的称为 Yoda 条件 另请参阅 《Yoda 条件》、《Pokémon 异常处理》等编程经典.

There is no difference. What your professor prefers is called Yoda conditions also see "Yoda Conditions", "Pokémon Exception Handling" and other programming classics.

它应该防止在比较中错误地使用赋值(=)而不是相等(==),但是现代编译器现在应该警告这一点,所以不需要这种类型的防御性编程.例如:

It is supposed to prevent the usage of assignment(=) by mistake over equality(==) in a comparison, but modern compilers should warn about this now, so this type of defensive programming should not be needed. For example:

if( pointer = NULL )

NULL 分配给 pointer 当程序员真正的意思是:

will assign NULL to pointer when what the programmer really meant was:

if( pointer == NULL )

这应该是一个比较,哎呀.使用 Yoda 条件 将其设为错误,您将(看到它live),并带有与此类似的消息:

it should have been a comparison, Oops. Make this an error using Yoda conditions you will (see it live), with a similar message to this:

错误:表达式不可赋值

正如 jrok 指出的那样:

As jrok points out using:

if (!pointer)

在这种情况下一起避免了这个问题.

avoids this problem all together in this case.

这是一个具体的例子,说明为什么现代编译器不再需要这种技术(现场观看):

Here is a concrete example of why with a modern compilers we don't need this technique anymore(see it live):

#include <iostream>

int main()
{
    int *ptr1 = NULL ;

    if( ptr1 = NULL )
    {
            std::cout << "It is NULL" << std::endl ;
    }

}

注意所有警告:

warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
    if( ptr1 = NULL )
        ~~~~~^~~~~~

note: place parentheses around the assignment to silence this warning
    if( ptr1 = NULL )
             ^
        (          )

use '==' to turn this assignment into an equality comparison
    if( ptr1 = NULL )
             ^
             ==

这使得很难错过这个问题.值得注意的是,在 C++ nullptr 应该优于 NULL,你可以看看 什么是使用 nullptr 的优势? 了解所有细节.

which makes it pretty hard to miss the problem. It is worth noting that in C++ nullptr should be preferred over NULL, you can look at What are the advantages of using nullptr? for all the details.

注意,在 C++ 中,运算符重载的不太可能的情况是可能存在一些人为的情况,它们并不相同.

Note, in C++ there is the unlikely case with operator overloading that there could be some contrived case where they are not the same.

注意,-括号警告 在某些方面会强制选择样式,您要么需要在生成警告的地方放弃潜在有效的赋值使用,例如如果您使用 -Werror 或选择将这些情况括起来,这正如下面的评论所暗示的,有些人可能会觉得丑.我们可以使用 -Wno-parenthesesgccclang 中关闭警告,但我不推荐这种选择,因为警告通常会指示一个真正的错误.

Note, the -Wparentheses warning in some ways forces a style choice, you either need to give up potentially valid uses of assignment in places where the warning is generated, for example if you use -Werror or choose to parenthesize those cases, which some may find ugly as the comment below suggests. We can turn of the warning in gcc and clang using -Wno-parentheses but I would not recommend that choice since the warning in general will indicate a real bug.

相关文章