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.
& token in C++ is a context-dependent symbol that serves five distinct syntactic and semantic roles: the unary address-of operator, the lvalue reference declarator, the binary bitwise AND operator, the member function reference qualifier, and the lambda capture specifier. The compiler resolves its specific behavior during syntactic and semantic analysis based on operand arity, declaration context, and surrounding grammar.
1. Unary Address-of Operator
When used as a prefix unary operator in an expression,& evaluates to the memory address of its operand.
- Operand Requirement: The operand must be an lvalue (an object or function that occupies an identifiable location in memory). It cannot be applied to a prvalue (temporary) or an xvalue.
- Type Yield: If the operand is of type
T, the expression&operandyields a prvalue pointer of typeT*. - Overloadability: This operator can be overloaded for user-defined types. The standard library provides
std::addressofto bypass overloaded&operators and guarantee the retrieval of the actual memory address.
2. Lvalue Reference Declarator
When appended to a type specifier in a declaration,& acts as a declarator modifier rather than an expression operator. It creates an lvalue reference, which is a direct alias to an existing object.
- Binding: An lvalue reference must be initialized upon declaration and binds permanently to its target lvalue. It cannot be re-seated to refer to a different object.
- Memory Semantics: At the language level, a reference is not guaranteed to occupy its own storage; the compiler often optimizes it out, treating it as an alternative identifier for the bound object.
- Type Yield: Modifies type
Tto typeT&.
3. Binary Bitwise AND Operator
When placed between two operands in an expression,& functions as a binary operator that performs a bitwise AND operation.
- Operand Requirement: Both operands must be of integral or unscoped enumeration types.
- Mechanics: The operator compares the operands bit by bit. The resulting bit is set to
1if and only if both corresponding bits in the operands are1; otherwise, it is set to0. - Type Yield: Yields a prvalue. The type of the result is the common type determined by standard integral promotions and arithmetic conversions applied to the operands.
4. Member Function Reference Qualifier
When appended to the signature of a non-static member function,& acts as a reference qualifier.
- Mechanics: It restricts the invocation of the member function strictly to lvalue instances of the class. If the object is an rvalue, a function qualified with a single
&is added to the initial candidate set during overload resolution, but it is subsequently deemed not viable because the implicit object parameter (an lvalue reference) cannot bind to an rvalue argument.
5. Lambda Capture Specifier
Within the capture list ([]) of a lambda expression, & dictates how automatic variables from the enclosing scope are captured by the closure type.
- Capture-Default: When used alone as
[&], it acts as a capture-default, instructing the compiler to capture all ODR-used automatic variables from the enclosing scope by lvalue reference. - Explicit Capture: When prefixed to a specific variable identifier as
[&var], it explicitly captures only that variable by lvalue reference.
Master C++ with Deep Grasping Methodology!Learn More





