C++ Structured Binding

Learn some syntax added to C++ recently, which makes dealing with our structs a little cleaner
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a fantasy pirate character
Ryan McCombe
Ryan McCombe
Posted

Often, we'll have a struct, and we'll want to extract all of its members to standalone variables:

void SomeFunction(Vector3 SomeVector) {
  float a { SomeVector.x };
  float b { SomeVector.y };
  float c { SomeVector.z };
  
  // ...
}

C++ offers a convenient shortcut to this, called structured binding.

Destructing and Deconstructing

Those with experience in other programming languages may already be familiar with this concept. Other common names for it include destructuring and deconstructing.

The C++ syntax looks like this:

void SomeFunction(Vector3 SomeVector) {
  auto [a, b, c] { SomeVector };
  
  // ...
}

To break this down, first we declare the type of our new variables. When we're creating variables using structured binding, we need to use the keyword auto here.

As we covered in the previous lesson, this asks the compiler to figure out what the type of each variable is. With structured binding, we must use auto, even if all our variables have the same underlying type.

After auto, we open up a set of square brackets - [ and ]. Within these brackets, we specify the name we want to use for each variable we create.

Finally, we provide the struct we want to use to initialise these variables.

The above code will set a, b and c to be the first, second and third public members of our struct, respectively.

Structured Binding with Classes

Because structs and classes are almost the same, we can also use destructuring with our classes, as long as the variables are public:

class Character {
public:
  string Name { "Goblin Warrior" };
  int Level { 5 };
  int Health { 100 };
}

Character MyCharacter;
auto [
  CharacterName,
  CharacterLevel,
  CharacterHealth
] { MyCharacter };

Limitations and Overloading Structured Binding

Out of the box, structured binding might seem a bit limited. For example, to use it, we need to create a variable for every public member of our object, even if we only want the first few.

This is likely to make it useless for larger classes. However, we do have the ability to overload the structured binding operators, to let us take control of the behaviour.

Overloading structured binding is a bit more complicated than the examples we've previously seen. It also relies on a few new concepts that are out of the scope of a beginner course, but we cover it in the next course.

For now though, it's still useful when working with simple structs like our Vector3.

Additionally, even with a basic familiarity with the concept, when we see auto [...] in other code, we'll have some intution as to what's happening.

With our new knowledge of all things struct, lets move onto the next lesson!

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Namespaces, Enums and Structs
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

C++ Constants with const and constexpr

Learn about constants in C++, and how we can use the const and constexpr keywords to make our code better
3D art showing a fantasy maid character
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved