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-662d741274b8d902595241/]

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-662d741274b93838058112/]

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

[crayon-662d741274b96268842517/]

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