C++ Study Notes (4) - template template parameter


Template template parameter is a template parameter with template type. It is often used to separate the logic and container. For example, the stack implementation:

template <class T, template <class> class CONT = std::deque >
class stack {
    CONT<T> elems;
    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 dumb class in line #1 is the placeholder for the template parameter, the real parameter is determined in line #4. Here is the full version of stack.