template class specialisation [RESOLVED]
Is it possible, like you would specialise a template function??
I've got my template matrix class, with variable dimensions and i want to define multiple 'Inverse' member functions (as well as others) that will be different depending on the number of dimensions of the matrix (a template parameter).
Re: template class specialisation
Yes, you can specialize template classes:
Code:
template <unsigned dimensions>
class Matrix {
// ...
};
template <>
class Matrix<2> {
// ...
};
Alternativly you could specialize a single function:
Code:
template <unsigned dimensions>
class Matrix {
void function();
};
template <unsigned dimensions>
void Matrix<dimensions>::function() {
// normal implementation
}
template <>
void Matrix<2>::function() {
// special implementation
}
Re: template class specialisation
Ah, thanks. I was trying to specialise the member function within the class declaration, and it didn't like it. Your 2nd example looks like just what i need, thanks!
Re: template class specialisation
Ok, i've done that, as below and put it in a header.
Code:
template <int m_iDimensions>
class CMatrix
{
public:
//Class functions
CMatrix Inverse(void);
}
template<>
CMatrix<2> CMatrix<2>::Inverse(void)
{
//Nice code
}
template<>
CMatrix<3> CMatrix<3>::Inverse(void)
{
//More, different nice code
}
Trouble is now i get linker errors saying those functions (with parameters filled in) have already been defined. I'm guessing it's because i've put them in a header, but i didn't think you could seperate class declaration and implementation for template classes.
Any help would be greatly appreciated. :)
Re: template class specialisation
You should put the implementation of the functions in a source file. I am not sure how that would work with the normal template function though. Alternativly you could make the functios inline if they are short.
Re: template class specialisation
Thanks for your help. Am i right in thinking that the reason you shouldn't make longer functions inline is because of the impact it'll have on excecutable size?
Re: template class specialisation [RESOLVED]
Yes, if you make long functions inline your executable could become a lot larger. In turns this might also make it slower, because more memory is needed and code is further apart. The compiler is free to ignore the inline keyword when it thinks that it will do more harm then good, but not all compilers are smart enough to actually do this.
Re: template class specialisation [RESOLVED]
Ok, cheers for the clarification.