Address-of operator (&)
The unary & operator obtains the address of an object or function. Learn the resulting pointer type, the operator's restrictions, and the difference between taking an address, declaring a reference, and bitwise AND.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
The unary & operator obtains the address of an object or function. Learn the resulting pointer type, the operator's restrictions, and the difference between taking an address, declaring a reference, and bitwise AND.
The alignof operator queries a type's alignment requirement in bytes at compile time. It returns a std::size_t value, not the object's size. For an array it reports the element alignment; for a reference type it reports the alignment of the referenced type.
The [] operator selects an array element or an element relative to a pointer. Array indices start at zero, and the built-in operator does not check bounds. Classes can define their own operator[], whose behavior depends on its implementation.
The = operator assigns a value to an existing object. Learn the left-operand requirements, value conversion, chained assignment, and the distinction between assignment, initialization, and comparison with ==.
The binary & operator computes bitwise AND: a result bit is 1 only when both corresponding operand bits are 1. Use it to select bits with a mask; it is not a replacement for logical &&.
The &= operator performs bitwise AND and stores the result in its left operand. It keeps only bits that were set in both operands. A common use is retaining selected bits with a mask.
The unary ~ operator flips the bits of a value after integral promotions. It is not the logical negation operator !. A mask lets you retain only selected bits of the complemented result.
The | operator combines corresponding bits of two values: a result bit is 1 when at least one input bit is 1. It is useful for combining flag masks and is distinct from the logical || operator.
The |= operator computes the bitwise inclusive OR of the left operand's current value and the right operand, then stores the result in the left operand. One common use is setting the bits selected by a mask.
The ^ operator computes XOR for corresponding bits: a result bit is 1 exactly when the operand bits differ. It is useful for toggling selected bits with a mask; it does not mean exponentiation.
The ^= operator performs bitwise exclusive OR. In the XOR calculation, ones in the mask flip the corresponding bits and zeros leave them unchanged. Before storing the result, the operator converts it to the left operand’s type.
The co_await expression uses the awaiter protocol to optionally suspend a coroutine and then obtain a result. It does not itself start a thread or provide scheduling; resumption must be arranged by the implementation being used.
The built-in comma operator evaluates its left operand before its right operand, discards the left result, and yields the right result while preserving its type and value category. Do not confuse it with commas separating arguments or declarators.
The ?: operator selects one of two expressions based on a condition. It evaluates only the selected branch, but the result type depends on both branches. In suitable cases, the result can designate an existing object rather than produce a new value.
const_cast explicitly changes const and volatile qualifiers in permitted pointer and reference conversions. It does not change the actual object: removing const from an access type does not permit modifying an object that is actually const.
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.
A delete expression destroys a single object created by new, while delete[] destroys an array created by new[]. Choosing the matching form is mandatory; mixing the forms causes undefined behavior.
The unary * operator denotes the object or function addressed by a pointer. It enables reading and, when the type permits, modifying the pointed-to object; it does not create a copy.
The / operator calculates a quotient. For integer operands it discards the fractional part toward zero; a floating-point operand can change the calculation type. Convert before dividing to preserve a fractional result.
The /= operator divides the left operand's value by the right operand and stores the result back. Operand types determine the kind of division, and storing the result may require another conversion. The left operand is evaluated only once.
dynamic_cast converts pointers and references within class hierarchies, checking conversions at runtime where required. A failed pointer cast returns nullptr, whereas a failed reference cast throws std::bad_cast.
The == operator tests operands for equality. Built-in comparison returns bool, while classes can define comparison through operator==. Do not confuse == with assignment = or pointer comparison with comparison of the pointed-to values.
Parentheses () following an expression form a call with the supplied arguments. This syntax calls functions and callable objects; a class can provide it through an operator() member function.
The >= operator checks whether the left operand is greater than or equal to the right operand. Built-in comparisons return bool. Numeric comparisons require attention to type conversions, particularly when mixing signed and unsigned integers.