Title: Template deduction failing
Tags: c++;templates
Question: Can anyone explain why this doesn't work:
#include "itkCovariantVector.h"
#include "itkImage.h"
template <typename TComponent, int NumberOfComponents&gt;
void FillImage(itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Image<itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ovariantVector<TComponent,
NumberOfComponents&gt;, 2&gt;* const image,
itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Image<itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ovariantVector<TComponent,
NumberOfComponents&gt;, 2&gt;* const output)
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Works." << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
int main(int, char* [] )
typedef itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Image<itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ovariantVector<float, 3u&gt;, 2u&gt; ImageType;
ImageTyp75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Pointer imageSmartPointer = ImageTyp75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0New();
ImageType* image = imageSmartPointer.GetPointer();
FillImage(image, image);
return 0;
no matching function for call to ‘FillImage(ImageType*&amp;, ImageType*&amp;)’
note: candidate is:
template<class TComponent, int NumberOfComponents&gt; void
FillImage(itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Image<itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ovariantVector<TComponent,
NumberOfComponents&gt;, 2u&gt;*, itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Image<itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ovariantVector<TComponent,
NumberOfComponents&gt;, 2u&gt;*)
The definitions of those class templates are: http://www.itk.org/Doxygen/html/classitk_1_1CovariantVector.html
http://www.itk.org/Doxygen/html/classitk_1_1Image.html
I created the same situation with non-ITK classes, and it works correctly:
#include <iostream&gt;
template <typename TPixel, int Dimensions&gt;
struct Image
template <typename TComponent, int NumberOfComponents&gt;
struct Vector
template <typename TComponent, int NumberOfComponents&gt;
void FillImage(Image<Vector<TComponent, NumberOfComponents&gt;, 2 &gt;* const image,
Image<Vector<TComponent, NumberOfComponents&gt;, 2 &gt;* const output)
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Works." << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
int main(int, char* [] )
typedef Image<Vector<float, 3&gt;, 2 &gt; ImageType;
ImageType* image = new ImageType;
FillImage(image, image);
delete image;
return 0;
Can anyone explain what might be the difference?
Comment: Possibly a stupid question: why is the function that does match preceded by `template  template `?

Comment: Could you also post the signature for itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Image, 2>75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0GetPointer()?

Comment: OK, from the errors that you posted, there seems to be an issue with `itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0SmartPointer75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ObjectType`. What is this `ObjectType`? Is it a typedef inside the `itk75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0SmartPointer` template?

Comment: Well, now it's clear why the first example fails: `void FillImage` is a function template and you're calling it as a simple function, so the compiler is required to deduce the template parameters `TComponent` and `NumberOfComponents`. See here for details: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/template_argument_deduction.htm Unfortunately, I can't help you figure out why this mechanism fails for these classes. Maybe someone with more experience on templates is willing to go through the documentation that you provided to figure it out...

Comment: Oh... Yeah, I was wondering about those, but I wasn't sure. It can get very hairy once you have so many overloads, especially with the superfluous C++ template syntax. I'm glad you managed to figure it out, though. Good luck with your project! :)

Comment: the template is because the function template is inside a class template. The ImageTyp75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0Pointer is a smart pointer. imageSmartPointer.GetPointer() returns a normal ImageType*.

Comment: @MihaiTodor, I have greatly simplified the question - any ideas now?

Comment: Ah, the problem was I had to change the signature from  to  so it matched the definition of the ITK classes.


Here is another answer: The problem was I had to change the signature from <int NumberOfComponents&gt; to <unsigned int NumberOfComponents&gt; so it matched the definition of the ITK classes.
#include <iostream&gt;
template <typename TPixel, int Dimensions&gt;
struct Image
// The deduction works with this
// template <typename TComponent, int NumberOfComponents&gt;
// struct Vector
// The deduction does NOT work with this (unsigned int vs int above)
template <typename TComponent, unsigned int NumberOfComponents&gt;
struct Vector
template <typename TComponent, int NumberOfComponents&gt;
void FillImage(Image<Vector<TComponent, NumberOfComponents&gt;, 2 &gt;* const image,
Image<Vector<TComponent, NumberOfComponents&gt;, 2 &gt;* const output)
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Works." << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
int main(int, char* [] )
typedef Image<Vector<float, 3&gt;, 2 &gt; ImageType;
ImageType* image = new ImageType;
FillImage(image, image);
return 0;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:07/27/12', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<c++><templates>', 'Title': 'Template deduction failing', 'ViewCount': 165, 'id': 11694461, 'user_AboutMe': '<p>I live at the intersection of computer vision research and software engineering. With my background in image processing, computer vision, machine learning, and LiDAR data analysis, I am in a position to develop algorithmic solutions to these difficult problems. But these algorithms are not particularly beneficial as only ideas or prototypes - efficient and carefully executed implementations are the key to their real world application. I am passionate about the reusability of the software I develop - I have found that continually building tools for my team rather than simply solving the particular problem at hand is the key to accelerating future work and success.</p>\n', 'user_AccountId': 106897.0, 'user_DisplayName': 'David Doria', 'user_Reputation': 9455.0, 'user_Views': 1618.0, 'user_id': 284529.0}}