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 *= (multiplication assignment) operator is a compound assignment operator that multiplies the current value of a modifiable lvalue (the left operand) by the value of the right operand, storing the computed product back into the left operand.

Syntax

lvalue *= expression;

Semantic Equivalence

Semantically, A *= B is equivalent to A = A * B, with one critical mechanical difference: the left operand A is evaluated exactly once.
// 'index()' is called only once when using the compound operator
array[index()] *= 5; 

// 'index()' is called twice in the expanded form
array[index()] = array[index()] * 5; 

Technical Characteristics

  • Operands: For built-in types, the left operand must be a modifiable lvalue of an arithmetic type. The right operand must be of an arithmetic type or an unscoped enumeration type (which implicitly promotes to an arithmetic type). For user-defined types, the operator must be explicitly overloaded.
  • Return Value: The operator returns an lvalue reference to the modified left operand (A&). This allows for operator chaining, though chaining assignment operators is generally discouraged for readability.
  • Precedence and Grouping: It shares the same low precedence level as all other assignment operators (e.g., =, +=, -=). Precedence dictates parsing and grouping, meaning the entire right-hand expression is grouped as a single unit before the assignment operation is applied.
  • Associativity: Right-to-left.
  • Evaluation Order: Since C++17, the evaluation of the right operand (and all its side effects) is strictly sequenced before the evaluation of the left operand.
int a = 10;
int b = 2;
int c = 3;

a *= b + c; 
// Precedence dictates grouping: a *= (b + c)
// Result: a = 50 (NOT 23)

Type Conversion and Promotion

When dealing with built-in arithmetic types, the usual arithmetic conversions are applied to establish a common type before the multiplication takes place.
  • If both operands are of integer types narrower than int (e.g., short or char), both are promoted to int.
  • If the operands are of different types, the operand with the lower conversion rank is converted to the type of the operand with the higher rank (e.g., an int will be converted to a double).
After the multiplication is performed using this common type, the computed result is implicitly converted back to the original type of the left operand for assignment. This final conversion can result in truncation or loss of precision if the left operand is of a narrower type or an integer type receiving a floating-point result.
int a = 5;
double b = 2.5;

a *= b; 
// 1. 'a' is converted to double (5.0) to match 'b'.
// 2. Multiplication occurs in double precision (5.0 * 2.5 = 12.5).
// 3. The result (12.5) is truncated to int (12) and assigned back to 'a'.

Operator Overloading

For user-defined types (classes and structs), the *= operator can be overloaded. By convention, it is implemented as a member function because it modifies the state of the object it is called on. It should return a reference to *this to maintain parity with built-in types.
class Matrix {
public:
    // Overload signature for multiplication assignment
    Matrix& operator*=(const Matrix& rhs) {
        // Implementation of matrix multiplication logic here
        
        return *this; // Return lvalue reference to the modified object
    }
};
When overloading both * and *=, the standard C++ idiom is to implement operator*= first, and then implement operator* as a non-member function that creates a copy, applies operator*=, and returns the result by value.
Master C++ with Deep Grasping Methodology!Learn More