TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
|| (Logical OR) operator is a binary operator in C++ that performs a logical disjunction on two expressions. It yields a prvalue of type bool, returning true if at least one of its operands evaluates to true, and false strictly when both operands evaluate to false.
Operand Conversion
Before the operation is performed, bothexpression1 and expression2 are contextually converted to bool. For arithmetic and pointer types, any non-zero value or non-null pointer evaluates to true, while zero or null evaluates to false.
Short-Circuit Evaluation
The|| operator enforces strict left-to-right evaluation and utilizes short-circuiting.
expression1(the left operand) is evaluated first.- If
expression1evaluates totrue, the overall expression is guaranteed to betrue. Consequently,expression2(the right operand) is not evaluated at all. - If
expression1evaluates tofalse,expression2is evaluated to determine the final result.
Precedence and Associativity
- Associativity: Left-to-right. An expression like
a || b || cis parsed as(a || b) || c. - Precedence: The
||operator has lower precedence than the Logical AND operator (&&), equality operators (==,!=), and relational operators (<,>), but higher precedence than assignment operators (=,+=) and the ternary conditional operator (?:).
Alternative Token
C++ provides the alternative spellingor defined in the standard. It is a primary token and behaves identically to || at the compiler level.
Operator Overloading
The|| operator can be overloaded for user-defined types by defining operator||. However, doing so fundamentally alters the operator’s evaluation semantics:
- Loss of Short-Circuiting: When overloaded, the operator behaves as a standard function call. Both operands (arguments) are evaluated before the function is invoked, destroying the short-circuit property.
- Sequencing Changes: Prior to C++17, the evaluation order of the operands in an overloaded
||was unsequenced. Since C++17, the left operand is sequenced before the right operand, but both are still unconditionally evaluated.
Master C++ with Deep Grasping Methodology!Learn More





