2014/12/30

Lamba captures with move semantics in C++ 11

It is possible to move values into lambda's in C++ 11 but it requires a wrapper. Here is what I am using.  It should only be used in this context as when it is copied it acts like a move and sucks the guts out of the source. I needed this where I had to supply a shared_ptr to a class to an async event callback and needed to keep it in scope past the lifetime of the parent. I did not want to store the "global" and copying would keep the lifetime alive well past the end of the callback

template<typename T>
class MoveCapture {
mutable T m_value;
public:
MoveCapture( ) = delete;
MoveCapture( T && val ) : m_value( std::move( val ) ) { }
MoveCapture( MoveCapture const & other ) : m_value( std::move( other.m_value ) ) { }
MoveCapture& operator=(MoveCapture const & rhs) {
if( this != &rhs ) {
m_value = std::move( rhs.m_value );
}
return *this;
}
T& value( ) {
return m_value;
}
T const & value( ) const {
return m_value;
}
T& operator*() {
return m_value.operator*();
}
T const & operator*() const {
return m_value.operator*();
}
T const * operator->() const {
return m_value.operator->();
}
~MoveCapture( ) = default;
T move_out( ) {
auto result = std::move( m_value );
return result;
}
}; // class MoveCapture
template<typename T>
MoveCapture<T> as_move_only( T&& val ) {
return MoveCapture<T>( std::move( val ) );
}
////////////////////////////////
/// Example usage
int main( int, char** ) {
auto lots_of_data = std::vector<LargeObj>( 100000000 );
auto mlots_of_data = as_move_only( std::move( lots_of_data ) );
auto processed_data = [mlots_of_data](...) {
// do something
return mlots_of_data;
}( );
return 0;
}
Link

No comments:

Post a Comment