> ## 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++ Unary Minus

The `-` operator in C++ functions as both a unary negation operator and a binary arithmetic subtraction operator, depending on the lexical context and the number of operands provided. In both forms, the built-in operator evaluates to a prvalue (pure rvalue).

## Unary Minus (Negation)

The unary `-` operator computes the algebraic negation of its single operand.

**Syntax:**

```cpp theme={"dark"}
-expression
```

**Mechanics:**

1. **Integral Types:** If the operand is of an integral or unscoped enumeration type, it undergoes integral promotion before the operation (e.g., a `short` or `char` is promoted to `int`). The type of the result is the promoted type.
2. **Floating-Point Types:** If the operand is of a floating-point type, it does not undergo integral promotion. The result type remains the original floating-point type.
3. **Unsigned Behavior:** If the promoted type is an unsigned integer, the operation is well-defined and computes the two's complement negation. Specifically, `-x` for an unsigned type evaluates to $2^n - x$ (where $n$ is the number of bits in the promoted type), effectively wrapping around.

## Binary Subtraction

The binary `-` operator computes the difference between its left-hand side (lhs) and right-hand side (rhs) operands. It features left-to-right associativity.

**Syntax:**

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

**Mechanics:**

1. **Usual Arithmetic Conversions:** If both operands are of arithmetic or *unscoped* enumeration types, the compiler applies the usual arithmetic conversions to yield a common type, which becomes the result type. Scoped enumerations (`enum class`) do not undergo usual arithmetic conversions and do not support the built-in `-` operator.
2. **Floating-Point:** For floating-point types, it performs IEEE 754 subtraction (or the architecture's equivalent).

## Pointer Arithmetic

The binary `-` operator has specialized semantics when interacting with pointers. For any pointer arithmetic to be well-formed, the pointed-to type `T` must be a **completely-defined object type**. Pointer arithmetic on `void*` or pointers to incomplete types is ill-formed in standard C++.

**Pointer minus Integer:**
Subtracting an integral type from a pointer yields a new pointer of the same type. The memory address is decremented by the integer value multiplied by `sizeof(T)`. The resulting decremented pointer must remain within the bounds of the same array object (or one past the end). If the arithmetic results in a pointer outside these bounds, it invokes undefined behavior.

```cpp theme={"dark"}
T* ptr = /* ... */;
T* decremented_ptr = ptr - 3; // Moves backward by 3 * sizeof(T) bytes
```

**Pointer minus Pointer:**
Subtracting two pointers of the same type (ignoring cv-qualifiers) yields the distance between them in terms of the number of elements, not bytes.

* The result is a signed integer of type `std::ptrdiff_t` (defined in `<cstddef>`).
* Both pointers must point to elements of the same array object (or one past the end); otherwise, the behavior is undefined.

```cpp theme={"dark"}
T* ptr1 = /* ... */;
T* ptr2 = /* ... */;
std::ptrdiff_t distance = ptr2 - ptr1;
```

## Operator Overloading

For user-defined types (classes and structs), the `-` operator can be overloaded to define custom negation or subtraction semantics. It can be implemented as either a member function or a non-member (friend) function.

**Unary Overload Signatures:**

```cpp theme={"dark"}
class T {
public:
    // Member function (no parameters, operates on *this)
    T operator-() const; 
};

// Non-member function (one parameter)
T operator-(const T& op);
```

**Binary Overload Signatures:**

```cpp theme={"dark"}
class T {
public:
    // Member function (one parameter for rhs, *this is lhs)
    T operator-(const T& rhs) const; 
};

// Non-member function (two parameters)
// Preferred for binary operators to allow symmetric implicit conversions
T operator-(const T& lhs, const T& rhs);
```

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