Language feature: Structured binding

Syntax

 

Tuples and data fetching

Tuples are suggested for returning multiple values from a function. They are in a strong relation with std::tie, which is used for retrieving the values.

The only problem is that we have to have all those variables previously declared.

But what if we wanted to get the reference to those values instead? We cannot do that easily either. The following code will not compile:

In order to do so, we had to use std::get instead.

Structured bindings

Structured Bindings give us the ability to declare multiple variables initialized from a tuple or struct.

Using them, the code looks so much clearer and shorter, and we can either copy the data or access them by reference.

 

Other than that, they can also be used with structs in the same way:

You may also like...

Leave a Reply