fork download
  1. #include <queue>
  2. #include <utility>
  3. #include <future>
  4.  
  5. template <typename T>
  6. class myqueue {
  7. public:
  8. myqueue() : q() {}
  9. virtual ~myqueue() {}
  10.  
  11. // pushes a copy
  12. virtual void push(const T& item) {
  13. q.push(item);
  14. }
  15. // pushes the object itself (move)
  16. virtual void push(T&& item) {
  17. q.push(std::move(item));
  18. }
  19. private:
  20. std::queue<T> q;
  21. };
  22.  
  23. int main() {
  24. // Thanks to Yakk for pointing out that I can reduce clutter by using one of std's non-copyable classes!
  25. myqueue<std::packaged_task<int()>> q;
  26. std::packaged_task<int()> t([]{return 42;});
  27. q.push(std::move(t));
  28. }
  29.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.7/i486-linux-gnu/bits/c++allocator.h:34:0,
                 from /usr/include/c++/4.7/bits/allocator.h:48,
                 from /usr/include/c++/4.7/deque:62,
                 from /usr/include/c++/4.7/queue:61,
                 from prog.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::packaged_task<int()>; _Args = {const std::packaged_task<int()>&}; _Tp = std::packaged_task<int()>]’:
/usr/include/c++/4.7/bits/stl_deque.h:1376:6:   required from ‘void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::packaged_task<int()>; _Alloc = std::allocator<std::packaged_task<int()> >; std::deque<_Tp, _Alloc>::value_type = std::packaged_task<int()>]’
/usr/include/c++/4.7/bits/stl_queue.h:212:9:   required from ‘void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::packaged_task<int()>; _Sequence = std::deque<std::packaged_task<int()>, std::allocator<std::packaged_task<int()> > >; std::queue<_Tp, _Sequence>::value_type = std::packaged_task<int()>]’
prog.cpp:13:9:   required from ‘void myqueue<T>::push(const T&) [with T = std::packaged_task<int()>]’
prog.cpp:28:1:   required from here
/usr/include/c++/4.7/ext/new_allocator.h:110:4: error: use of deleted function ‘std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(const std::packaged_task<_Res(_ArgTypes ...)>&) [with _Res = int; _ArgTypes = {}; std::packaged_task<_Res(_ArgTypes ...)> = std::packaged_task<int()>]’
In file included from prog.cpp:3:0:
/usr/include/c++/4.7/future:1337:7: error: declared here
stdout

Standard output is empty