I am implementing some rotation algorithm for medical images and I was wondering if it is possible to specialize a member function of a template class depending on the rotation angle. I.e. If the angle is 180 degress then the algorithm is much simpler.
I don't think this is possible if anyone knows different please let me know!
The angle would have to be of an integral type, and the angle would have to be specified at compile time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Image {
public:
template< int AngleInDegrees > // <- Note, int
void rotate()
{
// stuff here
}
template<>
void rotate<180>()
{
// special implementation
}
};
Invocation:
1 2 3 4 5 6 7
Image i1;
i1.rotate<90>();
i1.rotate<180>();
// Note: this won't compile:
int angle = rand() % 361;
i1.rotate<i>(); // Template parameter has to be known at compile time
Template parameters must be known at compile time.
Maybe you could make rotate<180>() work, but not rotate<angle>().
Why not use an if? And why do this at all? The algorithm for rotation is neither complicated nor costly.
Yes I have already implement a rotation algorithm with billinear interpolation and of course I use an if since for 180 degrees the algorith is some loops. My class is a template class since there is no way to know my data format at compile time. It may be unsigned char, unsigned short, unsigned int. Thus the template class. However I was curious whether you can somehow specify a specialization for an integer type. Apparently I was wrong :)