Work-around for missing "move-capture" syntax in C++0x
Hi!
This is not a question but more of an observation. As you might
already know, C++0x's lambda syntax only allows capturing by reference
and capturing by value (copying). And if I remember correctly, there
is no requirement for closure types to have a move constructor. I
already saw code examples involving lambdas and shared_ptr where
shared_ptr was only used because it wasn't possible to "move-capture"
a unique_ptr. But the std::bind facility is quite flexible with
respect to move-only types. With std::bind we can turn the following,
non-working code
void foo() {
unique_ptr<big> p (new big);
// ... set up *p
invoke_later([=]{ // <-- Oops! unique_ptr cannot be copied.
// work on *p
});
}
into
void foo() {
unique_ptr<big> p (new big);
// ... set up *p
invoke_later(bind([](unique_ptr<big> const& p){
// work on *p
},move(p)));
}
and have it work without the need for std::shared_ptr. This extends to
all other move-enabled types. For example, if you want to move a
string into a function object, you have to use std::bind for this
because C++0x doesn't offer a "move-capture" syntax. IMHO, it's a
shame that we have to do that. But at least this seems like a decent
work-around. (untested)
Cheers!
SG