[Insight-developers] Template function specialization
Williams, Norman K
norman-k-williams at uiowa.edu
Tue Jan 18 12:32:37 EST 2011
The problem you're running into is that C++ is very touchy about templates
and specialization. It's rather a confusing topic, about which books are
written. The solution involves some form of Template Metaprogramming.
Below is one solution to your problem, that I came up with off the top of
my head. It uses member function overloading, and doesn't require partial
template specialization, which is what I think you were trying to do.
There are other ways to do this, but this is the one that came immediately
to mind:
#include <iostream>
#include <itkImage.h>
template <class ImageType>
class TestOverLoad
{
public:
void GenerateData();
private:
// dummy type to discriminate dimension at compile time
template <unsigned dim> struct DimType {};
// the general ND case -- template member function
template <unsigned dim> void GenerateData(DimType<dim> *);
// the 2D special case -- non-template member function
void GenerateData(DimType<2> *);
};
// The 2D special case
template <class ImageType>
void
TestOverLoad<ImageType>::
GenerateData(DimType<2> *)
{
std::cout << "TwoD case" << std::endl;
}
// General Case ND
template <class ImageType>
template <unsigned dim>
void
TestOverLoad<ImageType>::
GenerateData(DimType <dim> *)
{
std::cout << "General Case, Dim = "
<< ImageType::ImageDimension
<< std::endl;
}
template <class ImageType>
void
TestOverLoad<ImageType>::
GenerateData()
{
// depending on dimension of ImageType, only one version of
// GenerateData will actually be instantiated
// The DimType<dim> * parameter never used, but it's a low-cost way
// to force compile-time type discrimination
this->GenerateData(static_cast< DimType<ImageType::ImageDimension>
*>(0));
}
int
main(int argc, char **argv)
{
TestOverLoad<itk::Image<unsigned char,2> > x2;
x2.GenerateData();
TestOverLoad<itk::Image<unsigned char,3> > x3;
x3.GenerateData();
}
More information about the Insight-developers
mailing list