Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

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

write efficient code with modern c17 feature structured bindings

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:

[crayon-673fa3fa4f032513662562/]

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

[crayon-673fa3fa4f03a464007471/]

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

[crayon-673fa3fa4f03f828333189/]

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!

Exit mobile version