C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Easier syntax for move-capture in lambdas

From: Bo Persson <bo_at_[hidden]>
Date: Tue, 17 May 2022 16:52:26 +0200
On 2022-05-17 at 13:05, Fabio Alemagna via Std-Proposals wrote:
> I have searched wide and large to see if anything like, or better
> than, this has been proposed, but found nothing. Apologies if this has
> already been discussed, if so I'd appreciate any pointers to past
> discussions/proposals.
>
> In my current project I suddenly found myself having to write lots of
> lambdas like this:
>
> [a = std::move(), b = std::move(b), /*...,*/ n = std::move(n)](/*...*/) {
> /* body */
> }
>
> Those moves are tedious to write and can easily extend too far on the
> line, making the code more difficult to read.
>
> I thought it'd be nice if we could just do this, instead:
>
> [&&a, &&b, /*..., */ &&n](/*...*/) {
> /* body */
> }

Possibly useful, but I haven't had the need so far.


>
> And, just like one can use the single ampersand to capture everything
> by reference, one could be able to use the double ampersand to capture
> everything by moved-to-value:
>
> [&&](/*...*/) {
> /* body */
> }
>

But this is a real foot-gun.

Post condition: "Some variables might have been moved from".

Received on 2022-05-17 14:52:55