How to forward a C ++ template class declaration?

For 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 declare this class in the header file?

+44
c ++ templates forward-declaration
Dec 12 '12 at 20:55
source share
2 answers

Here's how you 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 value is indicated in the declaration ahead, and not in the actual definition.

+62
Dec 12 '12 at 20:57
source share

You can only declare default arguments for a template for the first declaration of a template. If you want to allow users to forward the declaration of a class template, you must provide a forwarding header. If you want to forward the declaration of another class template using the default values, you're out of luck!

+6
Dec 12 '12 at 21:11
source share



All Articles