Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

or — alternative spelling of logical OR

`or` is a language-level alternative spelling of the C++ operator `||`. For the built-in operator, the result is `bool`, and the right operand is evaluated only when the left operand is `false`. Do not confuse it with bitwise OR: `|` or `bitor`.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

left_expression or right_expression

or is an alternative token for ||: except for spelling, it has the same meaning in the language. It requires neither a header nor a macro definition. It is reserved and cannot be used as a variable or function name.

The built-in or operator contextually converts its operands to bool and returns true when at least one is true; otherwise, it returns false. It short-circuits: the left operand is evaluated first, and the right operand is evaluated only if the left evaluates to false. If the right operand is evaluated, the left operand's evaluation is sequenced before it.

or has the same precedence as ||, lower than and / &&, and groups left-to-right. Thus, a or b and c means a or (b and c). The bitwise operator |, alternatively spelled bitor, is not a substitute for logical OR and does not provide short-circuit evaluation.

The short-circuit guarantee applies to the built-in operator. When an overload of operator|| is selected for user-defined types, both operands are evaluated; spelling it or does not change that. In the example, the operands have type bool, so the first expression skips check(), and the second calls it. The result is true true 1.

The example targets C++23 compilation mode. The displayed source was run unchanged in an isolated environment, and its standard output matched the declared output exactly. This does not attest to behavior in the production runtime. The supplied source is an evolving working draft of the standard, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    int checks = 0;
    const auto check = [&checks]() {
        ++checks;
        return true;
    };

    const bool first = true or check();
    const bool second = false or check();

    std::cout << std::boolalpha
              << first << ' ' << second << ' ' << checks << '\n';
}

Result

true 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.

Sources