Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Structured bindings make it more clean and readable to iterate through a map with a range based for loop like below

for (auto [key, value] : map) {
    cout << key << " : " << value << endl;
}

But can structured bindings be used in lambda expressions like the following?

std::for_each(map.begin(), map.end(), [](auto [key, value]) {
    cout << key << " : " << value << endl;
});

From what it looks like the above code does not work with the online C++ compiler I found here https://wandbox.org/permlink/sS6r7JZTB3G3hr78.

If it does not work then is there a good reason the above is not supported? Or is it just something that has not been proposed yet? The template will only be instantiated on use, so the structured bindings' "unbinding" process can occur where the instantiation is requested (i.e. when the function is called)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
306 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is not currently allowed by the syntax; structured bindings are a simple-declaration:

simple-declaration:[...]
- attribute-specifier-seqopt decl-specifier-seq ref-qualifieropt [ identifier-list ] initializer ;

while function parameters are introduced by a parameter-declaration-list, which contains declarators:

The declarators specify the names of these entities and (optionally) modify the type of the specifiers with operators such as * (pointer to) and () (function returning).

That is, a structured binding is a (block-level) statement syntax - you can see this by noting that the grammar for it ends in a semicolon ;. Allowing structured bindings in a lambda parameter list would require additional grammar to be added.

It sounds like a good idea, and I can't immediately see any ambiguity in the syntax; it would certainly be worth discussing as it solves your presented use case nicely and more concisely than the alternatives.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...