Using if (!!(expr)) instead of if (expr)

前端 未结 5 1724
终归单人心
终归单人心 2021-02-03 17:05

While reading the example code provided by Texas Instruments for their SensorTag I came across the following snippet.

void SensorTagIO_processCharChangeEvt(uint8         


        
5条回答
  •  我在风中等你
    2021-02-03 17:21

    In MSVC converting an integer to a bool implicitly in an if statement can generate a warning. Doing so via !! does not. Similar warning may exist in other compilers.

    So supposing the code was compiled with that warning enabled, and a decision to treat all warning as errors, using !! is a short and portable way to say "yes, I want this integer to be a bool".

提交回复
热议问题