is there a pragmatic reason to use “if (0 == p) ” instead of “if (!p)”?

后端 未结 12 740
难免孤独
难免孤独 2021-02-05 06:18

I\'m inclined to write if statements by using logical negation operator:

if (!p)
    some_code();

Some people around me tend to use explicit co

12条回答
  •  野性不改
    2021-02-05 07:13

    One as-yet-unmentioned benefit to the if (!Foo) version is that it will work with classes that used the safe bool idiom. In classes that implement that, comparison operators of classes will fail (e.g. Foo==0 will be undefined) but !Foo will call a conversion operator on Foo, returning a pointer to member function (or null pointer, if Foo should be treated as false). A number of Boost classes, like shared_ptr, use this technique.

提交回复
热议问题