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 + 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.
+ 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.
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.
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.
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.
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.
Master C++ with Deep Grasping Methodology!Learn More