I'm setting up a c++(11) program in which I use a templated class that depends on 2 parameters. Much of the class can be written generically upon template parameters. Only few functions need specialized version. Here is an example pattern that reproduces my problem :
template<class T, int N>
class foo
{
// typedefs and members that depend on T and N
// but that can be written generically e.g. :
typedef std::array<T,N> myarray;
void myfunc(myarray tab);
};
// ...
template<class T, int N>
foo<T,N>::myfunc(myarray tab)
{
// generic version
}
// need specialization only of myfunc:
template<class T>
foo<T,1>::myfunc(myarray tab)
{
// specialized version for N=1
}
then compiler complains : error: invalid use of incomplete type ‘class foo<T, 1>’
about then line template<class T> foo<T,1>::myfunc(myarray tab)
The only workaround I found to work was to insert a complete duplicate of the class with its specialized version :
template<class T>
class foo<T,1>
{
// recopy all the lines of class foo<T,N>, replacing N by 1
};
// duplicate as well all generic function definition with
// specialized versions <T,1> even when not needed
what is very unsatisfactory ...
After some experiments, I found out that this problem does not seem to occur when template uses only 1 parameter (eg template <int N> class foo{...};
) but only when at least 2 parameters are involved.
Is this something well known in C++ programming ? Is there some smarter method to solve my problem ? (I thought of creating a mother class without the specialized functions and then make class foo inherit from it, keeping in it only specialized members, in order to minimize the "duplication workaround")
Thanks for advices !