I'm creating a small program for my data structures class that simulates a critical system. Thus, using heap memory is not an option, as speed is important. It's a basic stack implementation that allows the user to decide the type and size of the stack. So, for example, the user could tell it to use integers with a total allocation of 500 units.
I need to encapsulate an array inside a Stack class that uses a template, such as this:
So I can use it like so:Code:template <typename T, std::size_t size> class Stack { public: Stack(void); void push(T value); private: T m_stack[size]; int top; };
However since templates are compile-time only, I can't allow the user to define the size in the second template argument. I can't use any STL containers such as a vector. I'm not allowed to use new, malloc(), or anything that allocates memory on the heap, yet the array must be dynamically allocated. I don't think using a maximum size for the array inside the class is allowed, or a VLA.Code:Stack<int, 10> stack;
Is there any such method to accomplish this? I've researched this extensively, and asked my professor but he is very good at avoiding answering questions directly. In fact, we are encouraged to use Ada, since this is much easier in Ada, but I refuse, I must use C++.