CKTIS / C++
Decrement operator --
The -- operator decreases a numeric value by 1 or moves a pointer back by one element. Prefix --x exposes the modified object, whereas postfix x-- produces a copy of the previous value. Both forms modify their operand.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
--x
x--The built-in -- operator performs decrement. For numbers, it subtracts 1; its operand must be a modifiable lvalue of an arithmetic type other than bool. You therefore cannot apply -- to a literal or a const object. Pointers to complete object types are also allowed. Using a volatile-qualified operand is deprecated.
Prefix --x is equivalent to x -= 1: it changes x, and its result is an lvalue referring to the modified object. Postfix x-- produces the previous value as a prvalue and also decreases x. Computing the previous value is sequenced before the modification; this does not mean that modification waits until the next statement. When the result is discarded, both forms have the same effect for built-in types.
For a pointer, -- moves back by one element, not one byte. A pointer one past the end of a nonempty array can be decremented to point to its last element. Moving before the beginning of an array or decrementing a null pointer causes undefined behavior.
Decrementing an int at its smallest representable value causes undefined behavior. For an unsigned integer object that is not a bit-field, decrementing zero produces the largest value of its type, so take care with countdown loops. A narrow unsigned bit-field can also be decremented, but it cannot hold the largest value of its declared type. In C++23, storing a value that a bit-field cannot represent gives an implementation-defined result. For classes, behavior depends on the operator-- overload; typical member declarations are T& operator--() and T operator--(int), where the extra int parameter distinguishes postfix. Returning a reference and the old value is a convention, not a general language requirement.
In the example, prefix receives 4, then postfix receives the previous value 4, and x ends at 3. This entry targets C++23; the supplied sources are an evolving working draft, not a frozen C++23 publication.
Examples
#include <iostream>
int main() {
int x = 5;
int prefix = --x;
int postfix = x--;
std::cout << "prefix=" << prefix << '\n';
std::cout << "postfix=" << postfix << '\n';
std::cout << "x=" << x << '\n';
}
Result
prefix=4 postfix=4 x=3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.