fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. void println(T data) {
  6. cout << data << endl;
  7. }
  8.  
  9. template<typename T>
  10. class List {
  11. public:
  12. class Item {
  13. protected:
  14. T data;
  15. Item* prev;
  16. Item* next;
  17. public:
  18. Item(T data, Item* prev, Item* next) {
  19. this->data = data;
  20. this->prev = prev;
  21. this->next = next;
  22. }
  23. T* getData() {
  24. return &data;
  25. }
  26. Item* getNext() {
  27. return next;
  28. }
  29. Item* getPrev() {
  30. return prev;
  31. }
  32.  
  33. T const* getData() const {
  34. return &data;
  35. }
  36. Item const* getNext() const {
  37. return next;
  38. }
  39. Item const* getPrev() const {
  40. return prev;
  41. }
  42. };
  43. private:
  44. class ExtendedItem : Item {
  45. public:
  46. void setPrev(Item* prev) {
  47. this->prev = prev;
  48. }
  49. void setNext(Item* next) {
  50. this->next = next;
  51. }
  52. void removeSelf() {
  53. if (next)
  54. ((ExtendedItem*)next)->setPrev(prev);
  55. if (prev)
  56. ((ExtendedItem*)prev)->setNext(next);
  57. }
  58. };
  59. Item* first;
  60. Item* last;
  61. public:
  62. List() {
  63. println("Default constructor called");
  64. first = NULL;
  65. last = NULL;
  66. }
  67. List(List<T> const& other) {
  68. println("Copy constructor called");
  69. first = NULL;
  70. last = NULL;
  71. for (Item const* cur = other.getFront(); cur != NULL; cur = cur->getNext()) {
  72. pushBack(*cur->getData());
  73. }
  74. }
  75.  
  76. List(List<T> &&) = delete;
  77.  
  78. ~List() {
  79. println("Destructor called");
  80. while (first != NULL) {
  81. Item* next = first->getNext();
  82. // ((ExtendedItem*)first)->removeSelf();
  83. delete first;
  84. first = next;
  85. }
  86. }
  87. Item* pushFront(T data) {
  88. Item* newOne = new Item(data, NULL, first);
  89. if (first != NULL)
  90. ((ExtendedItem*)first)->setPrev(newOne);
  91. first = newOne;
  92. if (last == NULL)
  93. last = newOne;
  94. return newOne;
  95. }
  96. Item* pushBack(T data) {
  97. Item* newOne = new Item(data, last, NULL);
  98. if (last != NULL)
  99. ((ExtendedItem*)last)->setNext(newOne);
  100. last = newOne;
  101. if (first == NULL)
  102. first = newOne;
  103. return newOne;
  104. }
  105.  
  106. Item* getFront() {
  107. return first;
  108. }
  109. Item* getBack() {
  110. return last;
  111. }
  112.  
  113. Item const* getFront() const {
  114. return first;
  115. }
  116. Item const* getBack() const {
  117. return last;
  118. }
  119.  
  120. void remove(Item* element) {
  121. if (element == NULL)
  122. return;
  123. ((ExtendedItem*)element)->removeSelf();
  124. if (element == first)
  125. first = first->getNext();
  126. if (element == last)
  127. last = last->getPrev();
  128. delete element;
  129. }
  130. template <typename C, bool Pred(T*, C)>
  131. Item* find(C context) {
  132. for (Item* cur = first; cur != NULL; cur = cur->getNext())
  133. if (Pred(cur->getData(), context))
  134. return cur;
  135. return NULL;
  136. }
  137. };
  138.  
  139. List<size_t> newList(size_t a) {
  140. List<size_t> result;
  141. result.pushBack(a);
  142. return result;
  143. }
  144.  
  145. int main() {
  146. List<size_t> a(newList(1));
  147. for (List<size_t>::Item const* cur = a.getFront(); cur != NULL; cur = cur->getNext())
  148. println(*cur->getData());
  149. return 0;
  150. }
Compilation error #stdin compilation error #stdout 0s 16064KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘List<long unsigned int> newList(size_t)’:
prog.cpp:142:9: error: use of deleted function ‘List<T>::List(List<T>&&) [with T = long unsigned int]’
  return result;
         ^~~~~~
prog.cpp:76:2: note: declared here
  List(List<T> &&) = delete;
  ^~~~
prog.cpp: In function ‘int main()’:
prog.cpp:146:27: error: use of deleted function ‘List<T>::List(List<T>&&) [with T = long unsigned int]’
  List<size_t> a(newList(1));
                           ^
prog.cpp:76:2: note: declared here
  List(List<T> &&) = delete;
  ^~~~
stdout
Standard output is empty