[Insight-users] Link problem with a template class.

Martin Urschler martin at urschler.info
Thu Apr 14 09:55:16 EDT 2005


hi george,

I hope I understood your problem right.

The template implementation of VC++6 (and of most other compilers too, i 
guess) does not support the definition of a template class in a cpp 
file. You have to place the template function definitions into the 
header. ITK solves this by using .txx files for template 
implementations. These txx files are included at the end of the header 
files for a template class, I'm sure you already came across these 
includes when looking through itk headers.

In your case the template definition is never parsed, since the compiler 
needs an actual template instantiation with a specific typename for your 
class, to parse the function definitions of this class. That should be 
the reason why you don't get an error when placing an arbitrary string 
into your cpp file.

So I guess the first method is to stick to the way itk handles this 
problem, put yout template implementation into a txx file and include it 
at the end of the header, such that with future compilers which may 
handle this issue better (ok, i guess microsoft compilers won't ever do 
;-), but that's already enough with flaming... ) porting will be an easy 
task.

The other choice (which I prefer actually) is to put your template 
declaration into the header file, the template definition into a txx 
file and provide a dummy cxx file where you put your template class with 
one or all possible template arguments, so they have to be instantiated 
from the compiler (and they are linked into a lib which saves 
compilation time...). Of course the drawback is that you somehow 
restrict the concept of generic programming, but IMHO its a good workaround.

hth,
Martin Urschler


More information about the Insight-users mailing list