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