Why use !! when converting int to bool?

前端 未结 10 1802
梦谈多话
梦谈多话 2020-11-30 22:37

What can be a reason for converting an integer to a boolean in this way?

bool booleanValue = !!integerValue;

instead of just



        
10条回答
  •  再見小時候
    2020-11-30 22:55

    The problems with the "!!" idiom are that it's terse, hard to see, easy to mistake for a typo, easy to drop one of the "!'s", and so forth. I put it in the "look how cute we can be with C/C++" category.

    Just write bool isNonZero = (integerValue != 0); ... be clear.

提交回复
热议问题