CKTIS / C++
Division assignment operator /=
The /= operator divides the left operand's value by the right operand and stores the result back. Operand types determine the kind of division, and storing the result may require another conversion. The left operand is evaluated only once.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
lhs /= rhs;The built-in operator lhs /= rhs corresponds to lhs = lhs / rhs, but evaluates lhs only once. The left operand must be a modifiable lvalue of arithmetic type, such as a non-const variable. The right operand is evaluated before the left operand. The expression's result is an lvalue referring to the modified object.
The usual arithmetic conversions determine the division type; the quotient is then converted to the type of lhs. Integer division discards the fractional part toward zero: -17 / 4 gives -4. A floating-point operand enables floating-point division, but int mixed = 9; mixed /= 2.0; stores 4, not 4.5.
Do not allow a zero divisor. Integer division by zero causes undefined behavior. So does integer division whose quotient is not representable in the intermediate type used for the division, as when the minimum int value is divided by -1. If the integer quotient fits in the intermediate type but not in the narrower integer type of lhs, the conversion to the type of lhs does not by itself cause undefined behavior. By contrast, converting a floating-point quotient to an integer type other than bool causes undefined behavior if the value after truncation toward zero is not representable in the destination type, even when the divisor is nonzero. This description concerns the built-in operator, not a custom class implementation of operator/=.
Examples
#include <iostream>
int main() {
int whole = -17;
whole /= 4;
double fraction = 17.0;
fraction /= 4;
int mixed = 9;
mixed /= 2.0;
std::cout << whole << '\n'
<< fraction << '\n'
<< mixed << '\n';
}
Result
-4 4.25 4
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.