C++ – How to forward declare a C++ template class

c++forward-declarationtemplates

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?

Best Solution

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.