Search⌘ K
AI Features

Binding

Explore how C++17 structured bindings enable you to bind elements from arrays, tuples, and classes, enhancing expressiveness and readability in your code. This lesson explains practical uses, such as iterating through maps with named keys and values, improving clarity in modern C++.

Initializers That Support Structured Bindings

Structured Binding is not only limited to tuples, we have three cases from which we can bind from:

1. If the initializer is an array:

C++ 17
#include <iostream>
int main() {
// works with arrays:
double myArray[3] = { 1.0, 2.0, 3.0 };
auto [a, b, c] = myArray;
std::cout << a << " " << b << " " << c;
}

In this case, an array is copied into a temporary object, and a, b and c refers to copied elements from the array.

The number of identifiers must match the number of elements in the array.

2. If the initializer supports std::tuple_size<>, provides get<N>() and also ...