> ## 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++ Addition

The `+` operator in C++ is a built-in polymorphic token that functions as a unary arithmetic promotion operator, a binary arithmetic addition operator, and an offset operator for pointer arithmetic. It evaluates to a prvalue (pure rvalue) and is fully overloadable for user-defined types.

## Unary Plus

The unary `+` operator returns the value of its operand. For narrow integer and unscoped enumeration types, it performs integral promotion. For floating-point types and pointers, it yields the exact same type without promotion.

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

* **Type Promotion:** If the operand is a narrower integer type (e.g., `char`, `short`, `bool`) or an unscoped enumeration, it is promoted to `int` (or `unsigned int`). Floating-point types (e.g., `float`, `double`) and pointer types are not promoted to `int`.
* **Pointer Decay:** When applied to an lvalue of an array or function type, the unary `+` forces an immediate decay to a prvalue pointer type.
* **Precedence:** Level 3 (evaluated right-to-left).

## Binary Addition

The binary `+` operator computes the sum of two arithmetic or unscoped enumeration operands. Scoped enumerations (`enum class`) are not supported by the built-in binary `+` operator.

```cpp theme={"dark"}
expression1 + expression2
```

* **Usual Arithmetic Conversions:** Before the addition occurs, C++ applies the usual arithmetic conversions to bring both operands to a common type. For example, adding an `int` and a `double` results in the `int` being implicitly converted to a `double`, yielding a `double` prvalue.
* **Associativity:** Left-to-right.
* **Precedence:** Level 6.

## Pointer Arithmetic

When one operand is a pointer to a completely-defined object type and the other is of an integral type, the `+` operator performs pointer arithmetic.

```cpp theme={"dark"}
pointer + integer
integer + pointer
```

* **Scaling:** The integral value is implicitly multiplied by the `sizeof` the type the pointer points to.
* **Result:** It yields a new pointer of the same type, offset by the scaled integer.
* **Commutativity:** The operation is commutative; the integer can appear on either the left or the right side of the operator.

## Operator Overloading

For user-defined types (`class`, `struct`, `enum`), the `+` operator can be overloaded to define custom addition semantics. It can be implemented as either a member function or a non-member function.

**Non-Member Function (Preferred)**
Implementing `operator+` as a non-member (often a `friend`) is the standard practice because it allows symmetric implicit conversions for both the left-hand side (LHS) and right-hand side (RHS) operands.

```cpp theme={"dark"}
class T {
public:
    // Declaration of operator+= is required to implement + in terms of +=
    T& operator+=(const T& rhs); 

    // Non-member overload
    friend T operator+(const T& lhs, const T& rhs) {
        T result = lhs;
        result += rhs; // Idiomatic: implement + in terms of +=
        return result;
    }
};
```

**Member Function**
If implemented as a member function, the LHS is implicitly the `this` pointer. This prevents implicit conversions on the LHS operand.

```cpp theme={"dark"}
class T {
public:
    // Declaration of operator+= is required to implement + in terms of +=
    T& operator+=(const T& rhs);

    // Member overload
    T operator+(const T& rhs) const {
        T result = *this;
        result += rhs;
        return result;
    }
};
```

* **Return Type:** Overloads should return a new object by value (a prvalue), not by reference, to prevent dangling references and to adhere to the standard value-category semantics of the built-in `+` operator.

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