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.
- operator in C++ functions as both a unary negation operator and a binary arithmetic subtraction operator, depending on the lexical context and the number of operands provided. In both forms, the built-in operator evaluates to a prvalue (pure rvalue).
Unary Minus (Negation)
The unary- operator computes the algebraic negation of its single operand.
Syntax:
- Integral Types: If the operand is of an integral or unscoped enumeration type, it undergoes integral promotion before the operation (e.g., a
shortorcharis promoted toint). The type of the result is the promoted type. - Floating-Point Types: If the operand is of a floating-point type, it does not undergo integral promotion. The result type remains the original floating-point type.
- Unsigned Behavior: If the promoted type is an unsigned integer, the operation is well-defined and computes the two’s complement negation. Specifically,
-xfor an unsigned type evaluates to (where is the number of bits in the promoted type), effectively wrapping around.
Binary Subtraction
The binary- operator computes the difference between its left-hand side (lhs) and right-hand side (rhs) operands. It features left-to-right associativity.
Syntax:
- Usual Arithmetic Conversions: If both operands are of arithmetic or unscoped enumeration types, the compiler applies the usual arithmetic conversions to yield a common type, which becomes the result type. Scoped enumerations (
enum class) do not undergo usual arithmetic conversions and do not support the built-in-operator. - Floating-Point: For floating-point types, it performs IEEE 754 subtraction (or the architecture’s equivalent).
Pointer Arithmetic
The binary- operator has specialized semantics when interacting with pointers. For any pointer arithmetic to be well-formed, the pointed-to type T must be a completely-defined object type. Pointer arithmetic on void* or pointers to incomplete types is ill-formed in standard C++.
Pointer minus Integer:
Subtracting an integral type from a pointer yields a new pointer of the same type. The memory address is decremented by the integer value multiplied by sizeof(T). The resulting decremented pointer must remain within the bounds of the same array object (or one past the end). If the arithmetic results in a pointer outside these bounds, it invokes undefined behavior.
- The result is a signed integer of type
std::ptrdiff_t(defined in<cstddef>). - Both pointers must point to elements of the same array object (or one past the end); otherwise, the behavior is undefined.
Operator Overloading
For user-defined types (classes and structs), the- operator can be overloaded to define custom negation or subtraction semantics. It can be implemented as either a member function or a non-member (friend) function.
Unary Overload Signatures:
Master C++ with Deep Grasping Methodology!Learn More





