How to forward declare a C++ template class?

Tron Thomas picture Tron Thomas · Dec 12, 2012 · Viewed 78k times · Source

Given a template class like the following:

template<typename Type, typename IDType=typename Type::IDType>
class Mappings
{
public:
    ...
    Type valueFor(const IDType& id) { // return value }
    ...
};

How can someone forward declare this class in a header file?

Answer

Pubby picture Pubby · Dec 12, 2012

This is how you would do it:

template<typename Type, typename IDType=typename Type::IDType>
class Mappings;

template<typename Type, typename IDType>
class Mappings
{
public:
    ...
    Type valueFor(const IDType& id) { // return value }
    ...
};

Note that the default is in the forward declaration and not in the actual definition.