Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the structured bindings syntax be used in polymorphic lambdas

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)

like image 455
Curious Avatar asked Aug 07 '17 07:08

Curious


Video Answer


1 Answers

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

simple-declaration:[...]
- attribute-specifier-seqoptdecl-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.

like image 100
ecatmur Avatar answered Sep 24 '22 19:09

ecatmur