CKTIS / C++
Logical OR operator ||
The built-in || operator returns bool: true when at least one operand is true, and false otherwise. It short-circuits: when the left operand is true, the right operand is not evaluated.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
lhs || rhsThe built-in operator lhs || rhs means inclusive logical OR, not exclusive OR: two true operands also produce true. Its operands are contextually converted to bool, and its result has type bool.
The left operand is evaluated first. If it is true, the right operand is skipped, including all its side effects. Otherwise, the right operand is evaluated; evaluation of the left operand is sequenced before evaluation of the right operand. Do not put actions that must always run on the right-hand side.
The operator || has lower precedence than && and is left-associative: a || b || c groups as (a || b) || c, whereas a || b && c groups as a || (b && c). Bitwise | is not a substitute: it performs a bitwise operation and does not provide short-circuit evaluation.
User-defined types can overload operator||. Calling such an overload does not provide short-circuit evaluation: both operands are evaluated, although operator notation still sequences the left operand before the right. The description of a bool result applies to the built-in operator.
In the example, the first check() is skipped, so checks remains 0. The second call increments checks to 1. This explanation targets C++23 use; the source document is an evolving working draft, not a frozen C++23 edition.
Examples
#include <iostream>
int main() {
int checks = 0;
auto check = [&checks]() {
++checks;
return true;
};
const bool first = true || check();
std::cout << std::boolalpha << first << ' ' << checks << '\n';
const bool second = false || check();
std::cout << second << ' ' << checks << '\n';
}
Result
true 0 true 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.