Hya,
Can anyone please tell me how this thing is working?
template <typename T,
template <typename ELEM> class CONT = std::deque >
class Stack {
private:
CONT<T> elems; // elements
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
};
What i don’t understand is this :
template class V or say this “template class CONT = std::deque”
i visualize this as
template <class>
class CONT = std::deque // here CONT is templatized class declaration.
but what pesters me is , how can we assign something to class name CONT , rather than writing its definition (which i’ve done till this time):
template <class>
class CONT{
//def
}
one more thing :
template <class> // why its only class written in angle bracket there should be also be name
like : template<class ty>
Thanks a lot , any help is very appreciated)
There is no such line in your question, so I can’t help with that.
This is a declaration of a template template parameter. You pass a template into the
Stacktemplate, and thenStackcan use it internally.The
= std::dequepart is a default value, in case you leave the CONT parameter unspecified. (std::dequeis a predefined template.)However, this will not work, because
std::dequetakes two arguments. This will work:However
ELEMandALLOCdo not actually name anything; they exist merely to clarify what the parameter list of the required template is. So, you can omit them: