Direct Initialization-Tutorial
The direct initialization of the variables can be done using the keyword auto in C++17. In this section, I describe how the variable can be directly initialized in if, for, and switch statements. The example with for loop is as follows:
#include <utility>
void Test()
{
auto length = 20;
for (auto [i, ch] = std::pair(0, ' '); i < length; ++i)
{
}
return;
}
The further operations will process using variables i and ch.
Similarly if and switch can also use this feature as follows:
if (auto [key, value] = GetMyMapValue(); value < 20)
{
//do something
}
GetMyMapValue function will return a particular key and value which can be used in the if loop if the value is less than 20. The switch statement uses similar initialization.
Switch(initialization;condition)
Structured binding:
- Binds specific names to a sub-object or elements of the initializer.
- Like reference, Structured binding is an alias to an existing object.
- Can bind tuples, array, and data members.
For more information please refer to this page.
Prev: Structured bindings Intro Next: Template Argument deduction
3 thoughts on “Structured Bindings”
Comments are closed.