Double Negation in C++

后端 未结 14 2969
你的背包
你的背包 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

    It side-steps a compiler warning. Try this:

    int _tmain(int argc, _TCHAR* argv[])
    {
        int foo = 5;
        bool bar = foo;
        bool baz = !!foo;
        return 0;
    }
    

    The 'bar' line generates a "forcing value to bool 'true' or 'false' (performance warning)" on MSVC++, but the 'baz' line sneaks through fine.

提交回复
热议问题