Why use !! when converting int to bool?

前端 未结 10 1767
梦谈多话
梦谈多话 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.

    0 讨论(0)
  • 2020-11-30 22:56

    !! is an idiomatic way to convert to bool, and it works to shut up the Visual C++ compiler's sillywarning about alleged inefficiency of such conversion.

    I see by the other answers and comments that many people are not familiar with this idiom's usefulness in Windows programming. Which means they haven't done any serious Windows programming. And assume blindly that what they have encountered is representative (it is not).

    #include <iostream>
    using namespace std;
    
    int main( int argc, char* argv[] )
    {
        bool const b = static_cast< bool >( argc );
        (void) argv;
        (void) b;
    }
    
    > [d:\dev\test]
    > cl foo.cpp
    foo.cpp
    foo.cpp(6) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    
    [d:\dev\test]
    > _
    

    And at least one person thinks that if an utter novice does not recognize its meaning, then it's ungood. Well that's stupid. There's a lot that utter novices don't recognize or understand. Writing one's code so that it will be understood by any utter novice is not something for professionals. Not even for students. Starting on the path of excluding operators and operator combinations that utter novices don't recognize... Well I don't have the words to give that approach an appropriate description, sorry.

    0 讨论(0)
  • 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));
    
    0 讨论(0)
  • 2020-11-30 22:58

    It is used because the C language (and some pre-standard C++ compilers too) didn't have the bool type, just int. So the ints were used to represent logical values: 0 was supposed to mean false, and everything else was true. The ! operator was returning 1 from 0 and 0 from everything else. Double ! was used to invert those, and it was there to make sure that the value is just 0 or 1 depending on its logical value.

    In C++, since introducing a proper bool type, there's no need to do that anymore. But you cannot just update all legacy sources, and you shouldn't have to, due to backward compatibility of C with C++ (most of the time). But many people still do it, from the same reason: to remain their code backward-compatible with old compilers which still don't understand bools.

    And this is the only real answer. Other answers are misleading.

    0 讨论(0)
提交回复
热议问题