C++ Logo

std-proposals

Advanced search

Re: Abbreviated Lambdas with structured bindings

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 7 Sep 2021 19:44:38 -0400
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

Received on 2021-09-07 18:44:51