5.9. Confusing the Equality (==) and Assignment (=) Operators
There’s one error that C++ programmers, no matter how experienced, tend to make so frequently that we feel it requires a separate section. That error is accidentally swapping the operators
== (equality) and
= (assignment). What makes this so damaging is that it ordinarily does not cause syntax errors—statements with these errors tend to compile correctly and the programs run to completion, often generating incorrect results through runtime logic errors. Some compilers issue a warning when
= is used in a context where
== is expected.
Two aspects of C++ contribute to these problems. One is that any expression that produces a value can be used as a condition—if the value of the expression ...