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.
%= (modulo assignment) operator is a compound assignment operator that computes the remainder of the division of a left-hand operand by a right-hand operand, subsequently assigning that computed remainder back to the left-hand operand.
lhs = lhs % rhs, with the strict compiler guarantee that the expression lhs is evaluated exactly once. This single-evaluation property is critical when lhs contains volatile reads or side-effect-producing function calls.
Type Constraints
The C++ standard enforces strict type requirements for the modulo assignment operator:- Integral Types: The
lhsmust resolve to an integral type (e.g.,int,char,long,short,size_t). Therhsmay be an integral type or an unscoped enumeration type (which implicitly promotes to an integer). Thelhscannot be an enumeration type because the underlying%operation yields an integer, and C++ prohibits implicit conversion/assignment from an integer back to an enumeration. - Floating-Point Prohibition: Applying
%=tofloat,double, orlong doubleis illegal and will trigger a compilation error. - Modifiable Lvalue: The
lhsmust be a modifiable lvalue. It cannot beconst-qualified.
Return Value
The operator returns an lvalue reference to the modified left-hand operand (lhs). Because it returns a reference, modulo assignments can be chained, though this is syntactically evaluated from right to left.
Precedence and Associativity
- Precedence: It shares the lowest precedence level (Level 16) with the standard assignment operator (
=) and other compound assignment operators (+=,*=, etc.). - Associativity: Right-to-left. In an expression like
a %= b %= c, the compiler evaluatesb %= cfirst, and the resulting reference ofbbecomes therhsfora %= b.
Sign Rules (C++11 and later)
Following the C++11 standardization of truncated division, the sign of the result assigned tolhs is strictly determined by the sign of the initial lhs (the dividend). The sign of rhs (the divisor) has no effect on the sign of the result.
Undefined Behavior
The%= operator will invoke undefined behavior (UB) under the following conditions:
- Division by Zero: If
rhsevaluates to0, the underlying division cannot be performed, resulting in a hardware trap or unpredictable program execution. - Signed Integer Overflow: According to the C++ standard, if the quotient of
lhs / rhsis not representable in the result type, the behavior of both the division and modulo operations is undefined. Specifically, iflhsholds the minimum representable value for a signed integer type (e.g.,INT_MIN) andrhsis-1, the operationlhs %= -1invokes undefined behavior and frequently causes a hardware trap on architectures like x86.
Operator Overloading
For user-defined types (classes and structs),%= can be overloaded to define custom modulo assignment semantics. It is conventionally implemented as a member function that modifies the object state and returns *this by reference.
Master C++ with Deep Grasping Methodology!Learn More





