fork download
  1. #include <queue>
  2. #include <utility>
  3.  
  4. template <typename T>
  5. class myqueue {
  6. public:
  7. myqueue() : q() {}
  8. myqueue(myqueue const&) = delete;
  9. virtual ~myqueue() {}
  10.  
  11. // pushes a copy
  12. void push(const T& item) {
  13. q.push(item);
  14. }
  15. // pushes the object itself (move)
  16. void push(T&& item) {
  17. q.push(std::move(item));
  18. }
  19. private:
  20. std::queue<T> q;
  21. };
  22.  
  23. class nocopy {
  24. public:
  25. // Default constructor
  26. nocopy() {}
  27.  
  28. // Move semantics
  29. nocopy(nocopy&& other) {}
  30. nocopy& operator=(nocopy&& rhs) {
  31. nocopy(std::move(rhs)).swap(*this);
  32. return *this;
  33. }
  34.  
  35. // Disable copy semantics
  36. nocopy(const nocopy& other) = delete;
  37. nocopy& operator=(const nocopy& rhs) = delete;
  38.  
  39. void swap(const nocopy& other) {}
  40. };
  41.  
  42. int main() {
  43. myqueue<int> q1; // compiles fine.
  44. q1.push(1);
  45.  
  46. std::queue<nocopy> q2; // compiles fine.
  47. nocopy nc1;
  48. q2.push(std::move(nc1));
  49.  
  50. myqueue<nocopy> q3; // ERROR :(
  51. nocopy nc2;
  52. q3.push(std::move(nc2));
  53. }
  54.  
  55.  
  56.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout

Standard output is empty