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.

Array binding, formally a subset of C++17 structured binding, is a language mechanism that unpacks the elements of a raw C-style array into distinct, named identifiers. It enables the simultaneous declaration and initialization of multiple variables directly from an array’s contiguous memory layout.

Syntax

cv-auto ref-operator [ identifier-list ] = array_expression;
  • cv-auto: The auto keyword, optionally modified by const or volatile.
  • ref-operator: Optional reference declarators (& or &&).
  • identifier-list: A comma-separated list of variable names.
  • array_expression: The source raw array being unpacked.

Core Mechanics

When an array is bound, the compiler generates a hidden, anonymous entity initialized by the array_expression. The identifiers in the bracketed list do not become standard reference variables; rather, they become compiler-level aliases to the individual elements of that hidden entity. Arity Matching: The number of identifiers in the identifier-list must exactly match the number of elements in the array. If the array has N elements, exactly N identifiers must be provided. Failure to match the arity results in a compile-time error. Type Deduction: The type of each identifier is deduced exactly as the type of the corresponding array element. Array-to-pointer decay does not occur during structured binding.

Binding Modes

The behavior of the bound identifiers depends entirely on the cv-qualifiers and ref-operators applied to auto. These qualifiers apply to the hidden array entity, not directly to the individual identifiers.

1. Binding by Value (auto)

Creates a complete, element-wise copy of the source array into the hidden entity. The identifiers alias the elements of the copy.
int arr[3] = {1, 2, 3};
auto [x, y, z] = arr; 

x = 10; // Modifies the hidden copy's first element. 'arr[0]' remains 1.

2. Binding by Lvalue Reference (auto&)

The hidden entity becomes an lvalue reference to the source array. The identifiers alias the elements of the original array, allowing direct mutation.
int arr[3] = {1, 2, 3};
auto& [refX, refY, refZ] = arr;

refX = 10; // Modifies 'arr[0]'. 'arr' is now {10, 2, 3}.

3. Binding by Rvalue Reference (auto&&)

Used when the array_expression is an rvalue (either a prvalue or an xvalue). The hidden entity becomes an rvalue reference, extending the lifetime of the temporary array to the scope of the binding. A prvalue of a raw array type can be created directly using a type alias and brace initialization.
using IntArray = int[2];
auto&& [rval1, rval2] = IntArray{100, 200};

// IntArray{100, 200} is a prvalue.
// rval1 and rval2 alias the elements of the temporary array, 
// whose lifetime is extended to the scope of the binding.

4. CV-Qualified Binding (const auto&)

Applies const to the hidden entity, rendering the aliased elements read-only. Because structured binding identifiers are aliases and not standard reference variables, the deduced type of the identifier is strictly the const-qualified element type, not a reference type.
int arr[3] = {1, 2, 3};
const auto& [cx, cy, cz] = arr;

// decltype(cx) is strictly 'const int', not 'const int&'.
// cx = 10; // Compile-time error: assignment of read-only variable 'cx'

Multidimensional Arrays

When applying structured binding to a multidimensional array, the binding operates strictly on the first dimension. The resulting identifiers alias the nested arrays. Even when bound with a reference operator, the deduced type of the identifiers is strictly the element type of the array, not a reference type.
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

auto& [row1, row2] = matrix;
// decltype(row1) is strictly int[3] (the first row).
// decltype(row2) is strictly int[3] (the second row).
// The identifiers act as aliases to the nested arrays.

row1[0] = 99; // Modifies matrix[0][0]
Master C++ with Deep Grasping Methodology!Learn More