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

Li, George (NIH/NCI) ligeorge at mail.nih.gov
Thu Apr 14 10:04:48 EDT 2005


Thanks,

That explains everything. I did not pay close attention to this
Issue before, but thought that template class is considered a
Standard C++ syntax. Any way, currently, my cxx file is actually
A dummy, and I will keep it as is.

George


-----Original Message-----
From: Martin Urschler [mailto:martin at urschler.info] 
Sent: Thursday, April 14, 2005 9:55 AM
To: Li, George (NIH/NCI)
Cc: insight-users at itk.org
Subject: Re: [Insight-users] Link problem with a template class.


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