Hacker News new | past | comments | ask | show | jobs | submit login

> Last week I spent 4 hours trying to find what turned out to be a missing = because C++ will not complain, but will behave very differently, when = is confused with ==.

Uh, all reasonable compilers warn about ambiguous use of = as a truth value.

  $ g++ -Wall -c a.cc
  a.cc: In function β€˜int foo(int, int)’:
  a.cc:2:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

  $ clang++ -Wall -c a.cc          # output is colored
  a.cc:2:9: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
    if (x = y) return 0;
        ~~^~~
  a.cc:2:9: note: place parentheses around the assignment to silence this warning
    if (x = y) return 0;
          ^
        (    )
  a.cc:2:9: note: use '==' to turn this assignment into an equality comparison
    if (x = y) return 0;
          ^
          ==
  1 warning generated.



You're assuming simple single-operator comparison. It compiles if ( x = y && p ) return -1; with joy.


Correction: if ( (x = y) && p ) return -1; to avoid -Wall warnings.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: