Partial specialization for pointers, c++

Woody picture Woody · Jan 21, 2011 · Viewed 8.2k times · Source

How to make partial specialization of the class GList so that it is possible to store pointers of I (i.e I*) ?

template <class I>
struct TIList
{
    typedef std::vector <I> Type;
};


template <class I>
class GList
{
      private:
            typename TIList <I>::Type objects;
};

Answer

Peter Alexander picture Peter Alexander · Jan 21, 2011

You don't need to specialise to allow that. It can store pointers already.

GList<int*> ints;

Anyway, if you wanted to specialise GList for pointers, use the following syntax.

template <class I>
class GList<I*>
{
    ...
};

Then just use I as you would in any normal template. In the example above with GList<int*>, the pointer specialisation would be used, and I would be int.