C++ Study Notes (4) - template template parameter
Development March 7th, 2006
Template template parameter is a template parameter with template type. It is often used to seperate the logic and container. For example, the stack implementation:
template <class T, template <class> class CONT = std::deque >
class stack {
private:
CONT<T> elems;
public:
void push ( T const & );
void pop();
T top() const;
bool empty() const { return elems.empty(); }
};
class stack {
private:
CONT<T> elems;
public:
void push ( T const & );
void pop();
T top() const;
bool empty() const { return elems.empty(); }
};
template <class T, template <class> class CONT >
void stack<T, CONT>::push( T const& elem )
{
elems.push_back( elem );
}
… …
The dump class in Ln 1 is the placeholder for the template parameter, the real parameter is determined in Ln 4. Here is the full version of stack ( view | download ).
Leave a Comment