19

Current C++11 standard does not support move capture of variable in lambda expression like

unique_ptr<int[]> msg(new int[1000000]);
async_op([&&msg] { // compile error : move capture is not supported
   /* do something */
});

Since message passing and unique ownership has some critical role in some asynchronous system design, I think move semantic should be treated as first class language semantic. But lambda doesn't support move capture.

Of course I know that there is some workaround using move capture proxy - but I wonder the reason of decision that this functionality not to be included in the C++11 standard, despite of its importance.

10
  • 8
    The reason is most likely that they simply didn't think of it when lambdas were introduced. FWIW, the committee is reworking the capturing part and thinking about generalizing it by allowing initializers to captures (aka [m = std::move(msg)]{ ... }). Commented Nov 23, 2012 at 2:10
  • Thank you for answer. Allowing initializer expression in capture list looks promising. Commented Nov 23, 2012 at 2:37
  • 1
    this can also be interesting for you stackoverflow.com/questions/8640393/move-capture-in-lambda Commented Nov 23, 2012 at 12:21
  • 1
    @rubenvb: Is a capture copied every time a lambda is called? :P And for copying, I'd assume the capture to be copied too. If it's move-only, well, the lambda won't be copyable. Commented Nov 25, 2012 at 11:58
  • 1
    @rubenvb: I don't see where there are hidden moves. If you move the lambda, you move the captures. If you copy the lambda, you (try to) copy the captures. I can't see how that might be confusing, but maybe that's just me. Commented Nov 25, 2012 at 18:28

1 Answer 1

5

The C++ spec tries to be pretty conservative. It's really bad for the next language spec to break programs that were compliant under the previous spec.

Move semantics took a while to mature. There were changes as late as 2009, if I recall. Meanwhile lambdas weren't implemented in many compilers until a similar timeframe. There was no time to fill in the gaps and still release a standard in 2011, which was already very late. (Prototype the spec with the compilers, test the compilers, go back and debate the spec, draft, prototype, test, repeat. Takes a while.)

Lambdas will be extended greatly in the next standard, gaining type deduction (auto polymorphism). Xeo mentions one potential solution to move initialization.

Note that lambdas are only syntactic sugar. They are specified in terms of an automatically-defined class, but contain nothing you can't do yourself. As for the present language standard, you are expected to manually flesh out that implementation when the sugar runs out.

By the way, you can possibly work around the missing feature by capturing an old fashioned auto_ptr, which is C++03's now-deprecated attempt at a smart pointer. It is invalidated by its own copy constructor, essentially implementing move-on-copy. But it is deprecated by C++11 and may cause warnings. You could try implementing something similar, though.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.