TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-= operator is the compound subtraction assignment operator in C++. It subtracts the value of the right operand from the left operand and directly assigns the resulting value to the left operand.
E1 -= E2 is equivalent to E1 = E1 - E2, with one critical distinction: the left operand (E1) is evaluated exactly once. If the left operand contains an expression with side effects (such as a function call or a post-increment operation), the side effect occurs only once.
Return Value
The operator modifies the left operand in place and returns an lvalue reference to that modified left operand. This allows for operator chaining, which is evaluated right-to-left.Type Mechanics
Arithmetic Types When applied to standard arithmetic types (integers, floating-point numbers), the usual arithmetic conversions are applied to both operands to determine a common type. The subtraction is performed using this common type, and the resulting value is subsequently converted to the type of the left operand before assignment. For example, if the left operand is anint and the right operand is a double, the int is promoted to a double for the subtraction. The resulting double is then truncated back to an int when assigned to the left operand.
Pointer Arithmetic
When the left operand is a pointer and the right operand is an integral type, the operator performs pointer arithmetic. It decrements the memory address held by the pointer by expression * sizeof(T), where T is the type the pointer addresses. The right operand must be an integer; subtracting a floating-point value or another pointer via -= is ill-formed.
Operator Overloading
For user-defined types (classes and structs), the-= operator can be overloaded by defining operator-=. By convention, this is implemented as a member function because it mutates the state of the object it is called on. It should return an lvalue reference to the current object (*this) to maintain parity with built-in semantics.
operator-) should be implemented in terms of the compound assignment operator (operator-=) to ensure logical consistency and reduce code duplication.
Master C++ with Deep Grasping Methodology!Learn More





