Optional template (possible correction) att MrMike

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Optional template (possible correction) att MrMike

      This is what the original code says (in the book and source)

      bool const operator == (optional const & other) const
      {
      if ((! valid()) && (! other.valid())) { return true; }
      if (valid() ^ other.valid()) { return false; } // <--
      return ((* * this) == (* other));
      }

      what I find strange is the second expresion, look at this truth table:
      a | other | return
      0 - 0 - 1 so returns false
      1 - 0 - 1 so returns false
      0 - 1 - 0 so executes next line even though a is not valid. !!
      0 - 0 is taken care of by the previous line


      are you sure it will work this way?
    • RE: Optional template (possible correction) att MrMike

      im no mrmike, but I think I can answer this one:

      XOR (^) truth table:

      a|b|c
      0 0 0
      0 1 1
      1 0 1
      1 1 0

      The valid() ^ other.valid() expression will eveluate to true when one is false and the other is not. So if one of the optionals is valid and the other optional is not, then the operator overload will return false (just as it should)

      The post was edited 1 time, last by poridge ().

    • RE: Optional template (possible correction) att MrMike

      The code works - but it is a serious bitch to read, isn't it??? Perhaps a little whitespace and some comments would have been a good idea.

      This is exactly one of those expressions that will tear a hole through your mind if you are searching for a bug - I can see why you focused on it.

      Did you experience a problem of some kind, or were the fleet of operators and parenthesis just too juicy to ignore?
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Snicker - that's a good one.

      The good old XOR isn't seen that much, but man it's a useful thing - you can almost always guarantee that something clever is afoot...
      Mr.Mike
      Author, Programmer, Brewer, Patriot