75 Structured Bindings in C++
1. Structured Bindings (C++17 Only)
Structured binding is a new feature that allows us to handle multiple return values more effectively (for multiple return values, refer to 52 Handling Multiple Return Values in C++). This is an extension of the method discussed in Lesson 52, particularly focusing on how to handle tuples, pairs, and similar return types. Structured binding simplifies our code, making it more concise than previous approaches.
In Lesson 52, Cherno mentioned that he prefers using a struct instance to handle multiple return values. With the introduction of structured bindings, this approach might change.
Previous Approach
#include <tuple>
std::tuple<std::string, int> CreatePerson() // Since there are only two parameters, std::pair could also be used, but tuples are more extensible
{
return { "Cherno", 24 };
}
int main()
{
auto person = CreatePerson();
std::string& Name = std::get<0>(person);
int age = std::get<1>(person);
}
Alternatively, the std::tie
method can be used, which doesn't require creating an instance and looks cleaner, but using a struct is still preferable:
Structured bindings solve these issues, making the code look much cleaner (Note: VS defaults to C++14, so you need to set the C++ language standard to 17 in the project properties):