'In this syntax, what is the actual type of auto?
In the C++17 for loop syntax for(auto [key, val]: students), what is auto replacing?
If students was, for example std::map<int,char*>, what would be written if not auto? I don't understand what it even is taking the place of. [int,char*]?
Solution 1:[1]
The std::map is filled with std::pair<const int, char*>, so one way to write this loop would be
for (std::pair<const int, char*> pair : students)
We could also separate the pair with std::tie, but this syntax cannot be used in the loop:
int key;
char* value;
std::pair<int, char*> pair;
std::tie(key, value) = pair;
Statement auto [key, val] replaces std::tie. This syntax is called structured binding.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
