C++ Logo

std-proposals

Advanced search

Re: Abbreviated Lambdas with structured bindings

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Tue, 7 Sep 2021 18:54:20 -0500
On Tue, Sep 7, 2021 at 6:46 PM Arthur O'Dwyer via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, Sep 7, 2021 at 7:10 PM Cleiton Santoia via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> I'm reading p2425r0
>> <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2425r0.html> and
>> notice that the lambda declaration
>>
>> *[* captures *]* *(* params *)* lambda-specifiers requires(optional) *{*
>> body
>>
>> *}*in "(params)" part we may include a structured binding syntax but
>> with implicit "auto"
>>
>> cv-auto ref-qualifier(optional) *[* identifier-list *]*
>>
>>
>> This could help solve the "*Arbitrary lookahead parsing" *problem ?
>>
>>
>> Sample 1 -----------------------
>> [][a,b] { return a * b; } // becomes:
>> [](auto a, auto b) { return a * b ; }
>>
>> Sample 2 -----------------------
>> [] const&& [a, b] { return a; } // becomes:
>> [](const auto&& a, const auto&& b) { return a; }
>>
>> Sample 3 -----------------------
>> []&[p] p.foo();
>> [](auto& p) noexcept(noexcept(p.foo())) -> decltype(( p.foo() )) {
>> return p.foo(); };
>>
>
> Your proposal doesn't have anything to do with structured binding, though.
> Normally (since C++17), when we see `[a, b]`, it means "we're
> structured-binding to some structure that is going to be destructured
> here." This *does* have applications to function parameters, but the
> application would be, like,
>
> auto magnitude(auto [x, y]) {
> return sqrt(x*x + y*y);
> }
> auto coord = std::pair(3, 4);
> auto five = magnitude(coord);
>
> This is not C++20, but it's a fairly obvious notion that has definitely
> been talked about informally, as well as having users ask about it:
>
> https://stackoverflow.com/questions/45541334/can-the-structured-bindings-syntax-be-used-in-polymorphic-lambdas
>
> So reusing any part of "structured binding" syntax, anywhere in this
> general vicinity, for any purpose *other than* structured binding, is a
> bad idea IMO.
>
> –Arthur
>

And formally (
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0931r0.pdf) which
was rejected in Jacksonville, 5-11.

Barry

Received on 2021-09-07 18:54:35