Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The > (greater than) operator is a binary relational operator that evaluates whether the left-hand side (LHS) operand is strictly greater in value or sequence than the right-hand side (RHS) operand. It yields a bool prvalue: true if the condition is met, and false otherwise.
lhs > rhs

Built-in Mechanics

For fundamental types, the compiler applies standard arithmetic conversions to bring both operands to a common type before performing the comparison.
  • Arithmetic Types: Compares integers, floating-point numbers, and enumerations based on their numeric values. If comparing signed and unsigned integers, the signed integer may be implicitly converted to unsigned, which can lead to mathematically unintuitive results if the signed value is negative.
  • Pointers: Compares memory addresses. According to the C++ standard, pointer comparison using > is only well-defined if both pointers point to elements within the same array (or one past the last element). Comparing pointers to unrelated objects results in unspecified behavior.

Precedence and Associativity

  • Precedence: The > operator has lower precedence than arithmetic operators (like + and *) and shift operators (<<, >>), but higher precedence than equality operators (==, !=) and logical operators (&&, ||).
  • Associativity: Left-to-right. However, chaining the operator (e.g., a > b > c) does not evaluate as a mathematical sequence. Instead, it evaluates as (a > b) > c, where the boolean result of a > b is implicitly converted to an integer (1 or 0) and then compared against c.

Operator Overloading (Pre-C++20)

For user-defined types, > can be overloaded. Idiomatic C++ dictates that relational operators should be implemented as non-member functions to ensure symmetric implicit conversions for both operands. Furthermore, > is typically implemented in terms of < to reduce code duplication and maintain strict weak ordering.
class Widget {
    int weight;
public:
    // ...
    friend bool operator<(const Widget& lhs, const Widget& rhs) {
        return lhs.weight < rhs.weight;
    }

    friend bool operator>(const Widget& lhs, const Widget& rhs) {
        return rhs < lhs; // Implemented via operator<
    }
};

C++20 Three-Way Comparison Synthesis

In C++20 and later, explicitly overloading the > operator is largely obsolete. The language introduces the three-way comparison operator (<=>, the “spaceship” operator). When the compiler encounters lhs > rhs, it first looks for a dedicated > overload. If none exists, it attempts to rewrite the expression using <=>.
#include <compare>

struct Widget {
    int weight;
    
    // Generates all relational operators (<, <=, >, >=, ==, !=)
    auto operator<=>(const Widget&) const = default; 
};

// Usage:
// Widget a{10}, b{5};
// a > b; // Compiler evaluates this as: (a <=> b) > 0
If lhs <=> rhs returns a comparison category object (such as std::strong_ordering::greater), the compiler evaluates (lhs <=> rhs) > 0 to yield the final boolean result.
Master C++ with Deep Grasping Methodology!Learn More