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 -- (decrement) operator is a unary operator that subtracts one from the value of its operand. It requires a modifiable lvalue of an arithmetic type (explicitly excluding bool, which the C++ standard forbids), a pointer type, or a class type that explicitly overloads the operator. The operator exists in two distinct forms, which differ in their evaluation order and resulting value categories:

Prefix Decrement (--x)

The prefix form decrements the operand’s value and evaluates to the modified object. For built-in types, the expression does not have a reference type; rather, it evaluates as an lvalue of the operand’s underlying type.
  • Type: T (where T is the type of the operand).
  • Value Category: Lvalue.
  • Evaluation: The decrement operation is sequenced before the value computation of the expression.
int x = 5;
int y = --x; // x is decremented to 4; y is initialized to 4

Postfix Decrement (x--)

The postfix form evaluates to the original value of the operand, then decrements the operand’s underlying value.
  • Type: T (where T is the type of the operand).
  • Value Category: Prvalue.
  • Evaluation: The value computation of the expression is sequenced before the decrement operation.
int x = 5;
int y = x--; // y is initialized to the prvalue 5; x is decremented to 4

Pointer Arithmetic Semantics

When applied to a pointer, the -- operator does not simply subtract the integer 1 from the memory address. Instead, it decrements the address by the byte size of the pointed-to type (sizeof(T)). The operand must be a pointer to a completely defined object type; it cannot be applied to void* or function pointers. To avoid Undefined Behavior, pointer arithmetic is only well-defined when the pointer points to an element of an array object (or one past the end of it).
#include <cstdint>

int32_t arr[2] = {10, 20};
int32_t* ptr = &arr[1]; // ptr points to the second element of the array
ptr--;                  // Address is decremented by sizeof(int32_t) (4 bytes)
                        // ptr now points to arr[0]

Operator Overloading

For user-defined types, both forms of the -- operator can be overloaded. The C++ compiler differentiates between the prefix and postfix overloads via a dummy int parameter injected into the postfix signature during overload resolution. The C++ language does not enforce specific return types for overloaded operators; they can legally return void, int, or any other type. However, returning T& for prefix and T for postfix is a strong convention required to satisfy standard library concepts (such as iterators) and to mimic built-in semantics.
class Decrementable {
    int state{0};
public:
    // Prefix decrement overload
    // Conventionally returns an lvalue reference to the modified object
    Decrementable& operator--() {
        this->state -= 1;
        return *this;
    }

    // Postfix decrement overload
    // Conventionally returns a prvalue copy of the object prior to modification.
    // The 'int' parameter is a language-mandated dummy variable for overload resolution.
    Decrementable operator--(int) {
        Decrementable temp = *this; // Copy current state
        --(*this);                  // Delegate to prefix overload
        return temp;                // Return unmodified copy
    }
};
Master C++ with Deep Grasping Methodology!Learn More