If I use a template function with T where T = itk::Image<unsigned char>::Pointer, everything is fine:

#include <itkImage.h>

class Test
{
  public:
  template <class T>
  void Add(T patch);
};

template <class T>
void Test::Add(T patch)
{

}

int main(int, char*[])
{
  Test a;
  itk::Image<unsigned char>::Pointer image;
  a.Add(image);
  return EXIT_SUCCESS;
}

However, if I use a template function with a parameter T::Pointer where T = itk::Image<unsigned char>, I get errors:

#include <itkImage.h>

class Test
{
  public:
  template <class T>
  void Add(typename T::Pointer patch);
};

template <class T>
void Test::Add(typename T::Pointer patch)
{

}

int main(int, char*[])
{
  Test a;
  itk::Image<unsigned char>::Pointer image;
  a.Add(image);
  return EXIT_SUCCESS;
}
error: no matching function for call to ‘Test::Add(itk::SmartPointer<itk::Image<unsigned char, 2u> >&)’

Can someone explain why these are different, and how to fix the second one?

Thanks,

David

Since the compiler uses the type of the parameter to figure out which template to instantiate, the only information it has is the type of the parameter, it cannot "back-trace" the type from which this parameter type comes from. This is because it cannot assume that the only type T that has a Pointer typedef of the type that you gave as parameter is the type itk::Image<unsigned char, 2u>.. what if another type has the same Pointer typedef, how would the compiler choose?

To solve the problem, you could of course call Add< itk::Image<unsigned char, 2u> >(image) instead. If you want to avoid the template argument... well, I cannot think of a solution right now.

Mike,

Thanks, that does the job. I don't mind telling it the template argument, I'll always know what it is. I didn't realize you could just tell it in <> before the argument list.

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.