0

当我尝试将包含 packaged_task 对象的队列作为参考传递给线程时出现编译错误。我修改了示例代码以将其传递给函数并观察到相同的编译错误

void runtasks(std::queue< packaged_task<int()> >& que)
{
    while(!que.empty())
    {
        auto task = std::move(que.front());
        que.pop();
        task();
    }
}

int main()
{
    std::queue< packaged_task<int()> > que;
    packaged_task<int()> t1(std::bind(factorial,1));
    packaged_task<int()> t2(std::bind(factorial,4));
    que.push(std::move(t1));
    que.push(std::move(t2));
    runtasks(que);
    return 1;
}
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_uninitialized.h:75:18:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::packaged_task<int()>, const std::packaged_task<int()>&, const std::packaged_task<int()>*>; _ForwardIterator = std::_Deque_iterator<std::packaged_task<int()>, std::packaged_task<int()>&, std::packaged_task<int()>*>; bool _TrivialValueTypes = false]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_uninitialized.h:126:15:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::packaged_task<int()>, const std::packaged_task<int()>&, const std::packaged_task<int()>*>; _ForwardIterator = std::_Deque_iterator<std::packaged_task<int()>, std::packaged_task<int()>&, std::packaged_task<int()>*>]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_uninitialized.h:281:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<std::packaged_task<int()>, const std::packaged_task<int()>&, const std::packaged_task<int()>*>; _ForwardIterator = std::_Deque_iterator<std::packaged_task<int()>, std::packaged_task<int()>&, std::packaged_task<int()>*>; _Tp = std::packaged_task<int()>]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_deque.h:944:36:   required from 'std::deque<_Tp, _Alloc>::deque(const std::deque<_Tp, _Alloc>&) [with _Tp = std::packaged_task<int()>; _Alloc = std::allocator<std::packaged_task<int()> >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_queue.h:96:11:   required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_construct.h:75:7: error: use of deleted function 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(const std::packaged_task<_Res(_ArgTypes ...)>&) [with _Res = int; _ArgTypes = {}]'
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
In file included from ../src/SmartPointers.cpp:16:0:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/future:1507:7: note: declared here
       packaged_task(const packaged_task&) = delete;
       ^
make: *** [src/subdir.mk:23: src/SmartPointers.o] Error 1
4

0 回答 0