Why use !! when converting int to bool?

前端 未结 10 1839
梦谈多话
梦谈多话 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:57

    I've never like this technique of converting to a bool data type - it smells wrong!

    Instead, we're using a handy template called boolean_cast found here. It's a flexible solution that's more explicit in what it's doing and can used as follows:

    bool IsWindow = boolean_cast< bool >(::IsWindow(hWnd));
    

提交回复
热议问题