Why double not? (!!)

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 18 22:58:38 PST 2016


On Saturday, 19 November 2016 at 04:54:22 UTC, Xinok wrote:
> On Saturday, 19 November 2016 at 03:52:02 UTC, Ryan wrote:
>> Why do I see double `not` operators sometimes in D code? An 
>> example it the last post of this thread.
>>
>> http://forum.dlang.org/thread/ktlpnikvdwgbvfaamrsk@forum.dlang.org
>>
>>> import core.sys.windows.windows : GetConsoleCP;
>>> bool hasConsole = !!GetConsoleCP();
>>
>> Thanks.
>
> It's a more concise way of writing:
>     GetConsoleCP() != 0
>
> You can do this in C/C++ as well (and presumably some other 
> languages).

  Hmmm... thinking about it, it does make perfect sense. The first 
! converts it to bool, the other inverts it back to it's 
positive/negative state.

  Although it's a combination of logic I wouldn't have through of 
unless I saw it. But testing the result on any number (float, 
double or real) won't be precise and would take far longer (and 
more complicated) using another method.


More information about the Digitalmars-d-learn mailing list