I have a function that constructs a lambda function with a move-capture (C++1y only) and returns it.
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
function<int ()> makeLambda(unique_ptr<int> ptr) {
return [ ptr( move(ptr) ) ] () {
return *ptr;
};
}
int main() {
// Works
{
unique_ptr<int> ptr( new int(10) );
auto function1 = [ ptr(move(ptr)) ] {
return *ptr;
};
}
// Does not work
{
unique_ptr<int> ptr( new int(10) );
auto function2 = makeLambda( std::move(ptr) );
}
return 0;
}
However, it seems as though upon returning, unique_ptr<int>
's copy constructor is called. Why is this/how can I get around this?
Link to paste: http://coliru.stacked-crooked.com/a/b21c358db10c3933