Why double not? (!!)

Patrick Schluter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 19 07:56:54 PST 2016


On Saturday, 19 November 2016 at 15:50:26 UTC, Adam D. Ruppe 
wrote:
> On Saturday, 19 November 2016 at 15:40:38 UTC, Ryan wrote:
>> Wouldn't this just be the same as
>> auto hasConsole = cast(bool) GetConsoleCP(); ?
>
> Yes, it is in D, though the habit often comes from C where 
> things are different. But people also may prefer !! for just 
> being shorter and once you know the pattern, you'll see them as 
> meaning the same thing.
>
Yes it is a C idiom to coerce the boolean value to be 0 or 1. 
Since C99 C has the built in type bool which will also coerce the 
value.

     bool hasConsole = (bool)GetConsoleCP();

will do. If the code is to be compiled on a pre C99 compiler then 
it is problematic as the value depend on the definition of the 
type bool. In that case you have to use the !! or the !=0 idiom. 
That's why C code still often use them because of the risk of 
being compiled on an old compiler (and Microsoft doesn't 
implement C99 anyway in MSVC, so for real portable code it's kind 
of required).


More information about the Digitalmars-d-learn mailing list