Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Multiplication assignment operator (*=)

The *= operator multiplies the left operand’s current value by the right operand and stores the result back. The left operand is evaluated only once, and converting the result to its type can discard a fractional part.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

lhs *= rhs

The built-in *= operator combines multiplication and assignment: lhs *= rhs behaves like lhs = lhs * rhs, except that lhs is evaluated only once. This matters for side effects: values[index++] *= 4 increments index only once. The right operand is evaluated before the left operand.

The left operand must be a modifiable lvalue of arithmetic type, such as an int or double variable, but not a const object or pointer. The right operand may have arithmetic or unscoped enumeration type. The usual arithmetic conversions apply before multiplication; the result is then converted to the left operand’s type. Thus, given int count = 5;, the statement count *= 1.5; stores 7: the fractional part of 7.5 is discarded.

Overflow during multiplication in a signed integer type causes undefined behavior. Arithmetic in an unsigned type operates modulo 2^N, where N is that type’s width; however, promotions can cause multiplication to occur in a signed type instead. Converting a floating-point result to an integer type other than bool discards the fractional part. If the truncated value cannot be represented in the destination type, the behavior is undefined. Conversion to bool is a Boolean conversion: zero becomes false, and any nonzero value becomes true. If b is a bool with the value true, b *= 2.0 leaves b as true without causing undefined behavior.

The result of built-in *= is an lvalue referring to the modified left operand. Assignment operators group right-to-left: a *= b *= c means a *= (b *= c). For class types, operator*= can be overloaded; its behavior need not represent multiplication, and it is not automatically generated from operator*. The supplied source is an evolving standard draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    int values[] = {6, 9};
    int index = 0;
    values[index++] *= 4;

    int count = 5;
    count *= 1.5;

    std::cout << values[0] << ' ' << values[1] << ' ' << index << '\n';
    std::cout << count << '\n';
}

Result

24 9 1
7

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources