11

I just found this in Microsoft's guiddef.h header file:

__inline bool operator==(REFGUID guidOne, REFGUID guidOther)
{
    return !!IsEqualGUID(guidOne,guidOther);
}

Is there any point to the !!, or was some dev just feeling cute that day?

pnuts
  • 58,317
  • 11
  • 87
  • 139
fieldtensor
  • 3,972
  • 4
  • 27
  • 43

2 Answers2

13

It turns off Visual C++ silly performance warning for the conversion to boolean.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    Yeap, the warning is C4800. They could just as well use `return IsEqualGUID(guidOne, guidOther) != 0;` which effectively does the same. – sharptooth Aug 14 '14 at 14:12
  • @sharptooth: Regarding why `!= 0` is not the common convention, it has difference precedence and placement and does not lend itself to up-front visual recognition. So there are good reasons why they could not just as well do that. They could do that, technically, yes, but not as well. ;-) – Cheers and hth. - Alf Aug 14 '14 at 14:36
  • Given the semantics of the C++ standard type `bool`, neither `!!` nor `!=0` is necessary; the conversion will be done implicitly, and it will behave the same way as the `!!`. The code, or at least the idiom, may date back to before C++ had `bool` as a built-in type, or at least before it behaved as it does in modern C++. An explicit conversion to `bool` (using whichever flavor of cast is most appropriate) might have been clearer and would probably also have inhibited the warning. – Keith Thompson Sep 16 '14 at 19:31
  • @KeithThompson: nope, other means do not suppress the warning. – Cheers and hth. - Alf Sep 16 '14 at 21:27
  • An explicit cast doesn't inhibit a conversion warning? *sigh* – Keith Thompson Sep 16 '14 at 21:50
  • @KeithThompson: be glad we're not back in vc 6.0 days, then i could tell you about double destructor calls and much else interesting stuff. but now that you got me thinking, at the time the vc behavior was really annoying because it meant one could not use rethrow exception handling idiom to centralize treatment of various exception types. then it was important due to e.g. ibm lotus notes api, which threw integers, and some other apis. now that that can be handled reasonably, it's no longer an issue (for me at least). hm. – Cheers and hth. - Alf Sep 16 '14 at 22:03
5

In this particular case, Alf is probably right.

Otherwise it is common idiom to standardize integer values to either 1 (if they start nonzero) or 0 (if they start as zero) for logical operations.

Xarn
  • 3,460
  • 1
  • 21
  • 43