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 && token in C++ serves multiple distinct syntactic and semantic roles depending on its context: as a runtime logical operator, a type declarator for rvalue and forwarding references, a ref-qualifier for member functions, and a compile-time constraint conjunction.

Logical AND Operator

When used in an expression between two operands, && acts as the binary logical AND operator. It evaluates to true if both operands are contextually converted to bool and yield true; otherwise, it evaluates to false.
bool result = expression1 && expression2;
Technical Characteristics:
  • Short-Circuit Evaluation: The compiler guarantees left-to-right evaluation. If the left operand evaluates to false, the right operand is strictly not evaluated.
  • Sequence Point: The evaluation of the left operand is sequenced before the evaluation of the right operand. All side effects of the left expression are guaranteed to be complete before the right expression is evaluated.
  • Alternative Token: C++ provides the and keyword as a standard alternative token for && in logical expressions.
  • Operator Overloading: While operator&& can be overloaded for user-defined types, doing so is discouraged because it disables the short-circuiting mechanism. Since C++17, overloaded operator&& is guaranteed to be evaluated strictly left-to-right (restoring the sequence point guarantee), but both operands are still unconditionally evaluated.

Rvalue Reference Declarator

When appended to a fully specified type in a declaration, && denotes an rvalue reference. It is a compound type that binds specifically to objects designated by rvalue expressions (prvalues and xvalues).
Type&& variable_name = static_cast<Type&&>(object);
Technical Characteristics:
  • Binding Rules: An rvalue reference (T&&) cannot bind to an object designated by an lvalue expression. When an lvalue is explicitly cast (e.g., via std::move or static_cast<T&&>), the resulting expression is an xvalue (an “expiring” value, which is a category of rvalue). The rvalue reference then binds to the underlying object designated by this xvalue expression, which is the exact same object designated by the original lvalue expression.
  • Reference Collapsing Rules: When type deduction or typedefs cause references to references, C++ applies collapsing rules. The && token only persists if both the original type and the applied modifier are rvalue references:
    • T& & collapses to T&
    • T& && collapses to T&
    • T&& & collapses to T&
    • T&& && collapses to T&&

Forwarding Reference Declarator

In contexts involving type deduction, the && token does not strictly denote an rvalue reference. Instead, it acts as a forwarding reference (historically called a universal reference) that can bind to either an lvalue or an rvalue.
template <typename T>
void deduce_type(T&& forwarding_ref);

auto&& variable = expression;
Technical Characteristics:
  • Deduction Contexts: Forwarding references occur specifically with un-cv-qualified template parameters (T&&) and auto type deduction (auto&&).
  • Type Resolution: If initialized with an lvalue of type X, the deduced type becomes X&, and reference collapsing yields an lvalue reference. If initialized with an rvalue of type X, the deduced type becomes X, yielding an rvalue reference (X&&).

Member Function Ref-Qualifier (C++11)

When appended to the declarator of a non-static member function, && acts as a ref-qualifier. It restricts the implicit object parameter (the object instance the method is called on), ensuring the function can only participate in overload resolution when the object expression is an rvalue.
struct MyStruct {
    void my_method() &&; 
};
Technical Characteristics:
  • Overload Resolution: Ref-qualifiers participate in overload resolution, allowing a class to provide different implementations of a method depending on whether the object instance is an lvalue (using &) or an rvalue (using &&).
  • Pointer Semantics: The ref-qualifier does not restrict or modify the this pointer itself, which remains a prvalue pointer of type X* or const X*.

Constraint Conjunction (C++20)

In C++20 concepts and requires clauses, && acts as a constraint conjunction. It forms a logical AND of two constraints, evaluated entirely at compile-time.
template <typename T>
requires ConceptA<T> && ConceptB<T>
void constrained_function();
Technical Characteristics:
  • Compile-Time Evaluation: Unlike the runtime logical AND operator, constraint conjunctions are evaluated by the compiler during template instantiation to verify type requirements.
  • Constraint Subsumption: The compiler uses the && token to determine constraint subsumption (e.g., a constraint A && B is recognized as strictly more constrained than A alone), which dictates template overload resolution.
Master C++ with Deep Grasping Methodology!Learn More