Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++RAD Studio

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:

  • 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!

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Software Developer | CS(CyberSec) Undergrad at APU Malaysia | Delphi/C++ Builder Enthusiast | Microsoft Learn Student Ambassador | Microsoft Azure Certified

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES