Skip to content

Structured Bindings (C++17) ​

In a Nutshell ​

A single line of syntax that destructures the elements of a tuple, pair, struct, or array into independent variables at once, sparing you std::get and field-by-field access.

None (language feature)

Core API Quick Reference ​

Binding FormSyntaxDescription
Bind by valueauto [a, b] = expr;Copies the elements into new variables
Lvalue referenceauto& [a, b] = expr;Binds references to the original object
Read-only referenceconst auto& [a, b] = expr;Const reference, avoids copying
Forwarding referenceauto&& [a, b] = expr;Perfect forwarding semantics
Array destructuringauto [a, b, c] = arr;Binds to array elements (count must match)
Pair destructuringauto [key, val] = *map_iter;Binds to a pair's first/second
Tuple destructuringauto [x, y, z] = tup;Binds to get<I> of tuple-like types
Struct destructuringauto [x, y] = point;Binds to public data members (in declaration order)

Minimal Example ​

Expand codeCollapse23 lines
C++
// Standard: C++17
#include <iostream>
#include <map>
#include <tuple>

struct Point { double x, y; };

int main() {
    // Struct destructuring
    Point p{1.0, 2.0};
    auto [px, py] = p;
    std::cout << px << ", " << py << "\n"; // 1, 2

    // Pair destructuring (map iteration)
    std::map<int, const char*> m{{1, "one"}, {2, "two"}};
    for (const auto& [key, val] : m) {
        std::cout << key << ": " << val << "\n";
    }

    // Tuple destructuring
    auto [a, b, c] = std::make_tuple(10, 20, 30);
    std::cout << a + b + c << "\n"; // 60
}

Embedded Applicability: High ​

  • Pure compile-time syntactic sugar with zero runtime overhead; the generated code is fully equivalent to manual field access
  • Simplifies unpacking multi-field structures such as register banks and sensor data, improving readability
  • Combine with const auto& to avoid copies — ideal for read-only access to hardware-mapped structs
  • Well supported in mainstream embedded toolchains (GCC 7+, ARM Clang 6+) since C++17

Compiler Support ​

GCCClangMSVC
74.019.11

See Also ​


Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0

pdf-latest-4-g85128cc · 85128cc · 2026-10-05