Are nested structured bindings possible?

C++C++17Structured Bindings

C++ Problem Overview


Assume I have an object of type

std::map<std::string, std::tuple<int, float>> data;

Is it possible to access the element types in a nested way (i.e. when used in ranged for loop) like this

for (auto [str, [my_int, my_float]] : data) /* do something */

 

C++ Solutions


Solution 1 - C++

No, it is not possible.

I distinctly remember reading somewhere that nested structured bindings are not allowed for C++17, but they are considering allowing it in a future standard. Can't find the source though.

Solution 2 - C++

No, they aren't possible; but this is:

for (auto&& [key, value] : data) {
  auto&& [my_int, my_float] = value;
}

which is close at least.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimoView Question on Stackoverflow
Solution 1 - C++bolovView Answer on Stackoverflow
Solution 2 - C++Yakk - Adam NevraumontView Answer on Stackoverflow