Structured bindings were introduced in C++17 as a way to decompose objects into their constituent elements. They have proven to be a powerful feature for working with complex types and making code more readable. In C++20, structured bindings were further enhanced to be used directly in if
statements, offering even more flexibility and expressiveness.
Let’s take a closer look at how structured bindings can be leveraged in if
statements to improve code readability and eliminate the need for temporary variables.
The Old Way
Before the introduction of structured bindings in if statements, you would need to create temporary variables to hold the decomposed values if you wanted to use them in an if
condition. Here’s an example:
std::map<int, std::string> myMap;
// ...
auto it = myMap.find(42);
if (it != myMap.end()) {
int key = it->first;
std::string value = it->second;
// Use key and value...
}
In the code snippet above, we had to create key
and value
variables to store the decomposed values of the std::map
iterator. This approach introduces unnecessary clutter and makes the code less concise.
The New Way
Thanks to the enhanced structured bindings in C++20, we can directly use the decomposed values in the if
statement itself. This eliminates the need for temporary variables and simplifies the code. Here’s the updated version of the previous example:
std::map<int, std::string> myMap;
// ...
if (auto [it, inserted] = myMap.try_emplace(42, "Hello"); !inserted) {
int key = it->first;
std::string value = it->second;
// Use key and value...
}
In the revised code above, the structured binding [it, inserted]
is used directly in the if
statement. The try_emplace
function returns a pair of values, and we can now directly extract and use those values within the if
condition itself. This not only reduces the verbosity but also makes the intent of the code clearer.
By integrating structured bindings into if
statements, C++20 allows us to write more concise and expressive code. The improved syntax provides a natural way to access decomposed values without creating unnecessary temporary variables.