Double Negation in C++

后端 未结 14 2861
你的背包
你的背包 2020-11-22 10:40

I just came onto a project with a pretty huge code base.

I\'m mostly dealing with C++ and a lot of the code they write uses double negation for their boolean logic.

14条回答
  •  执念已碎
    2020-11-22 10:58

    As Marcin mentioned, it might well matter if operator overloading is in play. Otherwise, in C/C++ it doesn't matter except if you're doing one of the following things:

    • direct comparison to true (or in C something like a TRUE macro), which is almost always a bad idea. For example:

      if (api.lookup("some-string") == true) {...}

    • you simply want something converted to a strict 0/1 value. In C++ an assignment to a bool will do this implicitly (for those things that are implicitly convertible to bool). In C or if you're dealing with a non-bool variable, this is an idiom that I've seen, but I prefer the (some_variable != 0) variety myself.

    I think in the context of a larger boolean expression it simply clutters things up.

提交回复
热议问题