|
-
May 12th, 2005, 01:04 PM
#1
Thread Starter
Not NoteMe
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).
Last edited by SLH; May 13th, 2005 at 07:23 AM.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
May 12th, 2005, 05:30 PM
#2
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
}
-
May 13th, 2005, 05:05 AM
#3
Thread Starter
Not NoteMe
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!
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
May 13th, 2005, 07:01 AM
#4
Thread Starter
Not NoteMe
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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
May 13th, 2005, 07:13 AM
#5
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.
-
May 13th, 2005, 07:22 AM
#6
Thread Starter
Not NoteMe
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?
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
May 13th, 2005, 07:30 AM
#7
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.
-
May 13th, 2005, 07:49 AM
#8
Thread Starter
Not NoteMe
Re: template class specialisation [RESOLVED]
Ok, cheers for the clarification.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|