我试图解决c ++ 11 lambda-capture限制问题。
以下代码与visual studio 2013一起运行,但不与g ++(无效字段错误)一起运行。代码有什么问题?我该如何解决呢?
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
完整错误消息:
/home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/Future.h: In instantiation of ‘struct it::crc::Future<R>::then(F) [with F = it::crc::when_all(InputIterator, InputIterator) [with InputIterator = std::_List_iterator<it::crc::Future<int> >; decltype (begin->.get()) = int]::<lambda(it::crc::Future<int>&&)>; R = int; typename it::crc::callable_traits<F>::return_type = std::vector<it::crc::Future<int>, std::allocator<it::crc::Future<int> > >]::<lambda()>’:
/home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/Future.h:303:33: required from ‘it::crc::Future<typename it::crc::callable_traits<F>::return_type> it::crc::Future<R>::then(F) [with F = it::crc::when_all(InputIterator, InputIterator) [with InputIterator = std::_List_iterator<it::crc::Future<int> >; decltype (begin->.get()) = int]::<lambda(it::crc::Future<int>&&)>; R = int; typename it::crc::callable_traits<F>::return_type = std::vector<it::crc::Future<int>, std::allocator<it::crc::Future<int> > >]’
/home/christofmatthaei/Desktop/untitled folder/trunk/Concurrency/inc/Concurrency/ThreadPool/When.h:29:21: required from ‘it::crc::Future<std::vector<it::crc::Future<decltype (begin->.get())> > > it::crc::when_all(InputIterator, InputIterator) [with InputIterator = std::_List_iterator<it::crc::Future<int> >; 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<R>::then(F)::<lambda()>::<tt capture>’
auto work = [tt](){tt._val();};
修改 我在5.3.1版上使用g ++