#pragma once #include #include #include template class stack { public: ~stack() { while (head) head = std::move(head->next); } void push(const T &t) { push_impl(t); } void push(T &&t) { push_impl(std::move(t)); } std::optional pop() { std::optional ret = std::nullopt; atomic_noexcept { if (head) { ret = std::move(head->data); head = std::move(head->next); } } return ret; } private: template void push_impl(U &&u) { auto p = std::make_unique(nullptr, std::forward(u)); atomic_noexcept { p->next = std::move(head); head = std::move(p); } } struct node { std::unique_ptr next; T data; }; std::unique_ptr head = nullptr; };