C++ Logo

std-proposals

Advanced search

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

From: Abdullah Qasim <iamminecraftredstoner_at_[hidden]>
Date: Wed, 1 Jun 2022 07:52:06 +0000
However, remember that “&&” primarily means “R-value” reference!

[&&] {
          // this should mean “R-value” reference (sim. to const ref)
}

Also, you can use:

using std::move;

[a = move(a) , b = move(b)] {

}

You may also like to define a macro:

#define _M(expr) ##expr = std::move(##expr)

Now, if this macro gets named as _MOVE_CAPTURE (or something similar...) in <stdlib.h>, your suggestion becomes redundant.

From: Abdullah Qasim<mailto:iamminecraftredstoner_at_[hidden]>
Sent: 26 May 2022 12:23
To: std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>
Subject: RE: [std-proposals] Easier syntax for move-capture in lambdas

Agreed.
+1

Move semantics should be easier, as they also give C++ the performance edge over Java etc.

From: Fabio Alemagna via Std-Proposals<mailto:std-proposals_at_[hidden]>
Sent: 17 May 2022 16:05
To: std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>
Cc: Fabio Alemagna<mailto:falemagn_at_[hidden]>
Subject: [std-proposals] Easier syntax for move-capture in lambdas

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 */
    }

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 */
    }

Thoughts?

Regards,
Fabio
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-06-01 07:52:10