Can the structured bindings syntax be used in polymorphic lambdas
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.
I think this would be a great proposal (if isn't there one already) that would simplify code for algorithms that pass zip iterators/tuples of references. (for example https://github.com/correaa/iterator-zipper)
At the same time is seems that it is not something that you couldn't achieve using a bit more verbose code by extracting the structure in the first line of the function:
#include <algorithm>
#include <iostream>
#include <map>
using std::cout;
using std::endl;
int main(){
auto map = std::map<int, int>{{1, 2}};
std::for_each(map.begin(), map.end(), [](auto const& key_value){
auto const& [key, value] = key_value;
cout<< key <<" "<< value <<endl;
});
}
https://wandbox.org/permlink/sS6r7JZTB3G3hr78
(which strengthens the point that this is implementable and simple to add to the language).
A more streamlined version could be indented as:
[](auto const& _){auto&& [key, value] = _; // [](auto const& [key, value]){
cout<< key <<" "<< value <<endl;
}