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

后端 未结 12 742
难免孤独
难免孤独 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:03

    I would place a space before and after !: ( ! p) so ! stands out. But I would restrict this usage to only integer types including pointers. I would use == for floating points because it will cause you and others to pause and think if 0.0 == p is really appropriate versus specifying a tolerance.

    If p is an instance of a class, ( ! p) should be used by defining operator! to avoid an implicit conversion with 0.0 == p.

提交回复
热议问题