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

# C++ Less Than

The `<` (less than) operator is a binary relational operator that evaluates whether its left-hand operand is strictly less than its right-hand operand, returning a `bool` (`true` or `false`).

```cpp theme={"dark"}
lhs < rhs
```

## Mechanics for Built-in Types

For fundamental types, the compiler generates direct comparison instructions based on the operand types:

* **Arithmetic Types:** Evaluates numeric values. If operands are of different types (e.g., `int` and `double`), the compiler applies standard arithmetic conversions to bring them to a common type before comparison.
  * *Warning:* These conversions create a notorious pitfall when comparing signed and unsigned integers. Expressions like `-1 < 1u` evaluate to `false` because the signed integer (`-1`) is implicitly converted to a large unsigned value before the comparison. C++20 introduces `std::cmp_less` (`<utility>`) as a safe alternative that correctly compares integers of different signedness without unexpected conversions.
* **Pointers:** Evaluates memory addresses. According to the C++ standard, built-in pointer comparison using `<` yields defined behavior only if both pointers point to elements within the same array (or one past the last element), or if they point to different non-static data members of the same object. In the latter case, the later-declared member compares greater, provided both members share the same access control and are not part of a `union`. Comparing pointers to unrelated objects results in unspecified behavior.
  * *Strict Total Ordering:* Because the built-in `<` operator yields unspecified behavior for unrelated pointers, the standard library provides `std::less<T*>`. This template specialization guarantees a strict total order across all valid pointers of the same type, which is exactly why `std::less` is the default comparator for associative containers like `std::map` and `std::set`.
* **Enumerations:** Evaluates the underlying integer values of the enumerators.

## Operator Overloading

For user-defined types (classes and structs), the `<` operator must be explicitly overloaded. It can be implemented as either a member function or a non-member (often `friend`) function.

**Member Function Syntax:**

```cpp theme={"dark"}
class Type {
    int value;
public:
    bool operator<(const Type& rhs) const {
        return this->value < rhs.value;
    }
};
```

**Non-Member Function Syntax:**

```cpp theme={"dark"}
class Type {
    int value;
public:
    // Constructor and other members...
    friend bool operator<(const Type& lhs, const Type& rhs);
};

bool operator<(const Type& lhs, const Type& rhs) {
    return lhs.value < rhs.value;
}
```

## Strict Weak Ordering Requirement

When overloading the `<` operator, the implementation must satisfy the mathematical properties of **Strict Weak Ordering**. The C++ Standard Library relies heavily on this contract. The properties are:

1. **Irreflexivity:** `x < x` must always evaluate to `false`.
2. **Asymmetry:** If `x < y` is `true`, then `y < x` must evaluate to `false`.
3. **Transitivity:** If `x < y` is `true` and `y < z` is `true`, then `x < z` must evaluate to `true`.
4. **Transitivity of Equivalence:** If `x` is equivalent to `y` (meaning `!(x < y) && !(y < x)`), and `y` is equivalent to `z`, then `x` must be equivalent to `z`.

Failure to adhere to strict weak ordering results in undefined behavior when the type is passed to standard library algorithms or associative containers.

## C++20 and the Spaceship Operator

Starting in C++20, the explicit overloading of the `<` operator is largely superseded by the three-way comparison operator (`<=>`).

If a class defines or defaults `operator<=>`, the compiler will automatically synthesize the `<` operator (along with `>`, `<=`, and `>=`) by rewriting the expression `lhs < rhs` as `(lhs <=> rhs) < 0`.

```cpp theme={"dark"}
#include <compare>

struct Point {
    int x;
    int y;
    
    // Compiler automatically generates operator< based on this
    auto operator<=>(const Point&) const = default; 
};
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
