Search code examples
c++c++17structured-bindings

What are the rules for unpacking array elements to multiple variables in C++?


I've seen code where something like this is done:

auto [a, b, c] = some_array_ptr;

What are the rules and proper terminology for this type of assignment?


Solution

  • (Translating @BessieTheCow's comment into an answer) in C++, this is called "structured binding". Read about it at cppreference.com, and you may also be interested in this question here on StackOverflow:

    Understand structured binding in C++17 by analogy

    Note that not everything you might interpret as "multiple values" can actually be used in structured binding (e.g. - a pointer may point to many values but it won't work, as @Peter points out.)