On this page
  1. Structured Bindings
  2. Overview
  3. Notes & Pitfalls
  4. Example

Structured Bindings

Prev: nullptr | Up: builtins | Next: Move Semantics
Rel: auto, std::unordered_map
Ref: cppreference — Structured Binding
Tags: #public #prog #cpp #builtins #tier/core


Overview

Structured bindings (C++17) unpack the members of a struct, std::pair, std::tuple, or fixed-size array into named local variables in a single declaration.

Notes & Pitfalls

Example

#include <cassert>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> scores{{"ada", 100}, {"bob", 90}};
    int total = 0;
    for (const auto& [name, score] : scores) {
        total += score;
    }
    assert(total == 190);
}

Linked from

Related notes