From Jason Turner
Watch on YouTube

Summary

In this episode, Jason explains why you should never overload the logical operators && and || in C++. Using a simple example with Compiler Explorer, he demonstrates how overloaded logical operators cannot replicate the essential short-circuit behavior of built-in operators. Jason shows how with built-in boolean operations, evaluation stops as soon as the result is determined (e.g., stopping after a 'true' in an OR operation), but with overloaded operators, all operands must be evaluated regardless of outcome. This fundamental difference makes it impossible to maintain the expected semantics of logical operations when overloading these operators.

Resources

Tags