Last active
April 10, 2025 02:25
-
-
Save unixnut/96d08eac432ea9c34196d5b1c932aa62 to your computer and use it in GitHub Desktop.
C++ Stack container template with constexpr support, by Jason Turner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename T, std::size_t Size> | |
| struct Stack | |
| { | |
| T m_data[Size](); | |
| std::size_t pos = 0; | |
| template<typename ... Arg> | |
| void constexpr emplace_back(Arg &&... arg) | |
| { | |
| m_data[pos] = T{std::forward<Arg>(arg)...}; | |
| ++pos; | |
| } | |
| constexpr T pop_back() | |
| { | |
| --pos; | |
| return m_data[pos]; | |
| } | |
| constexpr bool empty() const | |
| { | |
| return pos == 0; | |
| } | |
| constexpr std::size_t size() const | |
| { | |
| return pos; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment