Double Negation in C++

后端 未结 14 2853
你的背包
你的背包 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 11:16

    It's correct but, in C, pointless here -- 'if' and '&&' would treat the expression the same way without the '!!'.

    The reason to do this in C++, I suppose, is that '&&' could be overloaded. But then, so could '!', so it doesn't really guarantee you get a bool, without looking at the code for the types of variable and api.call. Maybe someone with more C++ experience could explain; perhaps it's meant as a defense-in-depth sort of measure, not a guarantee.

提交回复
热议问题