| 83 | // Pop an object off the stack |
| 84 | template <typename T> |
| 85 | T Stack<T>::pop() |
| 86 | { |
| 87 | if (isEmpty()) // If it's empty pop() is not valid so throw exception |
| 88 | throw std::logic_error {"Stack empty"}; |
| 89 | |
| 90 | // See Chapter 18 for std::move() |
| 91 | auto next {std::move(m_head->m_next)}; // Save pointer to the next node |
| 92 | T item {m_head->m_item}; // Save the T value to return later |
| 93 | m_head.reset(); // Delete the current head |
| 94 | m_head = std::move(next); // Make head point to the next node |
| 95 | return item; // Return the top object |
| 96 | } |
| 97 | |
| 98 | template <typename T> |
| 99 | bool Stack<T>::isEmpty() const { return m_head == nullptr; } |