How do assignment (=) and equality comparison (==) differ in expressions, and why can accidental assignment in a condition compile?
Show answer and explanation
Built-in assignment stores the right operand into the left object and the expression yields the assigned left object; equality instead compares two operands and yields bool without modifying them. An assignment can therefore compile in if or while when its result is contextually convertible to bool, so if (status = next()) tests the newly assigned value rather than equality. Enable compiler warnings and treat them as errors. Write == for comparison; when assignment inside a condition is intentional, make the intent unmistakable with parentheses and an explicit comparison. Overloaded operators can define domain-specific behavior, but should preserve these conventional expectations.
Built-in assignment stores the right operand into the left object and the expression yields the assigned left object; equality instead compares two operands and yields bool without modifying them. An assignment can therefore compile in if or while when its result is contextually convertible to bool, so if (status = next()) tests the newly assigned value rather than equality. Enable compiler warnings and treat them as errors. Write == for comparison; when assignment inside a condition is intentional, make the intent unmistakable with parentheses and an explicit comparison. Overloaded operators can define domain-specific behavior, but should preserve these conventional expectations.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.