vendredi 24 juin 2016

Move into lamda expression in c++11

I'm trying to work around the c++11 lambda-capture restriction with moving in question.

The following code runs with visual studio 2013, but not with g++ (invalid field error). What is wrong with the code? How can i fix it.

template <typename T>
struct move_on_copy
{
    using T_t = typename std::remove_reference<T>::type;
    mutable T_t _val;

    move_on_copy(){}

    move_on_copy(T_t&& v):_val(std::move(v)){}
    move_on_copy(move_on_copy const& v):_val(std::move(v._val)){}
    move_on_copy& operator=(move_on_copy const& v)
    { _val =std::move(v._val); return *this; }
};

//...
auto tt = move_on_copy<Packaged_Task<R2()>>(std::move(task));
auto work = [tt](){tt._val();}; //compiler error here

Full Error Message:

/home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/Future.h: In instantiation of ‘struct it::crc::Future::then(F) [with F = it::crc::when_all(InputIterator, InputIterator) [with InputIterator = std::_List_iterator >; decltype (begin->.get()) = int]::&&)>; R = int; typename it::crc::callable_traits::return_type = std::vector, std::allocator > >]::’: /home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/Future.h:303:33: required from ‘it::crc::Future::return_type> it::crc::Future::then(F) [with F = it::crc::when_all(InputIterator, InputIterator) [with InputIterator = std::_List_iterator >; decltype (begin->.get()) = int]::&&)>; R = int; typename it::crc::callable_traits::return_type = std::vector, std::allocator > >]’ /home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/When.h:29:21: required from ‘it::crc::Future.get())> > > it::crc::when_all(InputIterator, InputIterator) [with InputIterator = std::_List_iterator >; decltype (begin->.get()) = int]’ /home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/test/Future.cpp:253:62: required from here /home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/Future.h:303:17: error: using invalid field ‘it::crc::Future::then(F)::::’ auto work = tt{tt._val();};

Aucun commentaire:

Enregistrer un commentaire