0

Recently, I came across following code snippet, I couldn't know the reason. Any one has any idea, why negate two times (and not just return the value of doAction())? return !!doAction();

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
M. Ahmed
  • 11
  • 3
  • The rightmost ! is `bool operator!()` that returns a bool based on the object which is not a bool. The leftmost ! is a negation. – user2672165 May 17 '20 at 09:54
  • It could be conversion, if doAction returns an `int` (for example) then `!!doAction()` will have a `bool` value. – john May 17 '20 at 10:14
  • If you need such behavior as mentiond above, please implement `operator bool() const` – MyNick May 17 '20 at 11:57
  • 1
    Hard to say for sure. You've not included enough of the code snippet to ascertain the exact reason it's doing `!!doAction();`, but in general it's probably not necessary to do the `!!` part because C++ is pretty promiscuous about what it considers a booly truthy (anything not zero) and booly falsy (anything zero). – Eljay May 17 '20 at 12:56
  • Thank you all. I think i've got the idea. – M. Ahmed May 18 '20 at 12:28

0 Answers0