Quickly Write Efficient Code With Modern Structured Bindings Available In C++17 On Windows With C++Builder

C++17 has a new feature that consolidates syntactic sugar and automatic type deduction: structured bindings. This helps to assign values from tuples, pairs, and structs into individual variables. In another programming language, you can find this as unpacking.

Applying a structured binding to specify various variables from one bundled structure is one step. Structured bindings always applied with the same pattern:

  • The list of variables var1, var2 – must exactly match the number of variables contained by the expression being assigned from
  • Then there should be one of the following
    • std::pair
    • std::tuple
    • struct
    • or array
  • The type can be auto, const auto, or even auto&&

If you write too many or not enough variables between the square brackets, the compiler will give an error, telling us about our mistake

Here is a complete real example of a use case for structured bindings:

As you can see, by using structured bindings, you can write more efficient and clean code.

Be sure to check out the official documentation on structured bindings here!

Check out the structured bindings demo for C++Builder on GitHub!