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++ is a polymorphic token that functions as a binary arithmetic addition operator, a unary integral promotion operator, and a pointer arithmetic offset operator. For built-in types, it triggers standard arithmetic conversions or pointer scaling and yields a prvalue. For user-defined types, it invokes overloaded functions that can return any type and value category.
Unary Plus (+)
The unary + operator precedes a single operand. Its primary technical function is to trigger integral promotion.
- Operands: Must be of arithmetic, unscoped enumeration, or pointer type.
- Mechanics: If the operand is an integral type smaller than
int(e.g.,char,short), it undergoes integral promotion tointorunsigned int. For pointers and larger arithmetic types, it returns the value unmodified. - Precedence: Level 3, evaluated with right-to-left associativity.
Binary Addition (+)
The binary + operator computes the sum of two operands.
- Arithmetic Addition: If both operands are of arithmetic or unscoped enumeration types, standard arithmetic conversions are applied to bring them to a common type before the addition is performed.
- Overflow Rules: For signed integer types, arithmetic overflow results in Undefined Behavior (UB). For unsigned integer types, addition is guaranteed to wrap around using modulo arithmetic (modulo 2n, where n is the number of bits in the value representation).
- Pointer Arithmetic: If one operand is a pointer to a completely-defined object type and the other is of an integral or unscoped enumeration type, the integral value is scaled by the
sizeofthe pointee type. The result is a new pointer of the same type, offset by the scaled amount. Adding two pointers together is ill-formed.- Bounds Checking: When adding an offset to a pointer, the resulting pointer must remain within the bounds of the same array object, or point one past the last element of that array. If the result exceeds these bounds, it results in Undefined Behavior.
- Precedence: Level 6, evaluated with left-to-right associativity.
Operator Overloading
For user-defined types (classes, structs, and enumerations), the+ operator can be overloaded to define custom semantics.
Non-Member Overload (Recommended)
Implementing the binary+ as a non-member function allows implicit type conversions to apply equally to both the left-hand and right-hand operands. It is conventionally implemented by passing the left-hand side by value and utilizing the overloaded += operator.
Member Overload
When implemented as a member function, the left-hand operand is implicitly thethis pointer. This asymmetric design prevents implicit conversions on the left-hand operand.
Unary Overload
The unary+ can also be overloaded. It takes no arguments when defined as a member function, or exactly one argument when defined as a non-member.
Value Category and Constness
- Built-in: Both unary and binary built-in
+operators yield a prvalue (pure rvalue). They do not modify their operands. - Overloaded: The return type and value category of an overloaded
+are technically unrestricted by the compiler (e.g., an overload can return an lvalue reference likeT&). However, standard C++ convention dictates returning a prvalue of the class type (T, notT&orconst T) to facilitate correct expression chaining and prevent dangling references.
Master C++ with Deep Grasping Methodology!Learn More





