2

I like new C++14 addition of giving lambdas ability to capture move only arguments, but I am not a fan of the syntax:
Move capture in lambda

What is a a reason that simpler

auto f = [&&x]{return x->twenty_perc_cooler();};

was not used?

Community
  • 1
  • 1
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • I think that would be surprising and contradict the design of rvalue references. The point of an rvalue reference is to document that the value is not aliased, and transfer of the the unique reference holder is explicit via `std::move`. Your proposal would make the no-aliasing guarantee hard to follow and brittle. – Kerrek SB Apr 17 '15 at 14:04
  • Moreover, what's the point? What would the difference be between `[&x](){}` and `[&&x](){}`? In both cases `x` must refer to an existing, named value. – Kerrek SB Apr 17 '15 at 14:05
  • after lambda in first case if x is vector x is not empty, in second it is. – NoSenseEtAl Apr 17 '15 at 15:19
  • Why? You're just binding a reference, not mutating any state? – Kerrek SB Apr 17 '15 at 15:54
  • to make it clear I want this: std::vector vi{1,2,3,47}; auto f = [&&vi](){}; assert(0==v.size()); same for unique_ptr, other user defined types... – NoSenseEtAl Apr 17 '15 at 16:10
  • That seems like a terrible idea. It would create a completely unique and idiosyncratic meaning of `&&`. – Kerrek SB Apr 17 '15 at 16:36
  • sure, like &x everywher else except in [] means take address of, not a reference – NoSenseEtAl Apr 17 '15 at 17:16
  • Taking the address of something also doesn't mutate it, but in any case the capture syntax is of course reminiscent of the declaration syntax `[T] & x`, `[T] && x`, `[T] x`. – Kerrek SB Apr 17 '15 at 17:41
  • @NoSenseEtAl: Are you sure that's guaranteed in other cases? I don't recall any such guarantee, and entertain some doubt that it exists. – Jerry Coffin Apr 17 '15 at 18:30

1 Answers1

3

This issue was actually addressed in N3610, a proposal regarding capture-by-move:

Why not capture with &&?

It's often asked why we wouldn't just support something like

[&&x] { ... }

The issue here is that we're not capturing by an rvalue reference, we are attempting to move. If we would capture by rvalue reference, the move would occur too late, since it's intended to happen at capture time, not at call time. And as long as we're capturing something that has a name, we shouldn't do a hidden move from it.

Philipp Lenk
  • 921
  • 6
  • 14