How to separate the definition and declaration of a class of child templates

If I have a template class defined as:

#ifndef A_HPP #define A_HPP template<class T> class A { public: int doSomething(int in, bool useFirst); private: template<int CNT> class B { public: int doSomething(int in); }; B<2> first; B<3> second; }; #include "a_imp.hpp" #endif 

Now I can find the declaration for A::doSomething in the implementation header file like this

 #ifndef A_IMP_HPP #define A_IMP_HPP template<class T> int A<T>::doSomething(int in, bool useFirst) { if (useFirst) return first.doSomething(in); return second.doSomething(in); } #endif 

However, I do not know how I am declaring for a child class method. Is this possible, or do I need to do one of the other two ways that I can do this, namely defining methods in the main header or declaring a class outside A.

Please note that I use C ++ 11, so if it is possible only in that it will work for me anyway, although a C ++ 98 solution would be good for other people.

+6
source share
2 answers

Not sure if this is what you are asking for, but I think you need something like this:

  template<class T> template<int CNT> int A<T>::B<CNT>::doSomething(int in) { return /*...*/; } 

Note that the template keyword is displayed twice, first for template A parameters, then for nested class B parameters.

+4
source

You can do it as follows:

 template <class T> template <int CNT> int A<T>::B<CNT>::doSomething(int in) { // do something } 
+6
source

All Articles