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 is a compound assignment operator in C++ that performs addition on its operands and assigns the resulting value to the left operand. It modifies the left operand in place and guarantees that the left operand’s expression is evaluated exactly once.

Syntax and Semantics

lhs += rhs;
Semantically, A += B is equivalent to A = A + B, with one critical distinction: the expression A is evaluated only once. If the left operand contains side effects or complex memory access (e.g., matrix[calculate_index()] += 5;), the function calculate_index() is executed a single time, whereas A = A + B would execute it twice.

Return Value

For built-in types, the += operator returns an lvalue reference to the modified left operand. While returning an lvalue reference is the standard convention for user-defined types, it is not universally true across all standard library types. For example, std::atomic<T>::operator+= returns the new value of type T by value, not by reference.

Type-Specific Behavior

The compiler resolves the behavior of += based on the types of the operands:
  1. Arithmetic Types: The compiler applies the usual arithmetic conversions to both operands to determine a common type for the addition. The addition is performed using this common type (which may involve promoting the left operand to a wider type, such as double). The result is then implicitly converted—which may result in truncation or demotion—back to the original type of the left operand before assignment.
  2. Pointers: Performs pointer arithmetic. The left operand must be a pointer to a completely defined object type, and the right operand must be of an integral type. The pointer’s memory address is incremented by the right operand multiplied by the byte size of the pointed-to object type (sizeof(T)). Pointer arithmetic is strictly prohibited on function pointers (even though function types are completely defined) and pointers to void.
  3. Standard Library Types: For types like std::string, the operator is overloaded to perform concatenation, appending the right operand’s character sequence to the left operand.

Operator Overloading

For user-defined types, += must be explicitly overloaded. By convention, it is implemented as a member function because it mutates the object’s internal state. The standard signature takes a const reference to the right-hand side and returns a non-const reference to *this.
class Vector2D {
public:
    Vector2D(float x, float y) : x_(x), y_(y) {}

    // Overloading the compound assignment operator
    Vector2D& operator+=(const Vector2D& rhs) {
        this->x_ += rhs.x_;
        this->y_ += rhs.y_;
        return *this; // Return lvalue reference to the modified object
    }

private:
    float x_;
    float y_;
};
Architectural Note: In C++ operator design, it is a standard idiom to implement the binary addition operator (operator+) as a non-member function that delegates its logic to the compound assignment operator (operator+=). This guarantees semantic consistency between A + B and A += B while minimizing code duplication.
Master C++ with Deep Grasping Methodology!Learn More