Skip to main content
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: 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.

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.

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.

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.

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.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More