[Insight-users] About C++ template problem In ITK.

Luis Ibanez luis.ibanez at kitware.com
Wed Dec 10 13:54:44 EST 2008


Hi Weidong,


1) You can't implement templated classes in shared libraries nor static
    libraries.

    This is because the templated classes are not fully-defined-types
    until you instantiate them with specific template arguments.

    That's the reason why the libraries in ITK are so small: they only
    contain the non-templated classes of ITK.

2) When writing templated classes, you can separate (just for style)
    the declaration from the implementation of the class. The declaration
    goes into a .h file, while the implementation goes into a .txx file.

    HOWEVER: The .h file MUST include the .txx file.

    Please take a look at the bottom lines of any of the ITK templated
    classes, and you will see that we include the corresponding .txx
    file. (except when manual or explicit instantiation is used).


3) Your DON'T need the export clause in the implementation of templated
    classes, because you can't make templated code to be part of a
    library.

    If you want a library, you will have to choose some explicit
    instantiations of your templated class.



    Regards,


       Luis



---------------------
Wei-Dong Lian wrote:
> I have written a c++template class, I want to separate declaration and 
> implementation into two file(".h, .cc"). As that, when referencing to 
> this class that I exported into shared linked library(so) in Linux, the 
> compiler indicated "the undefined reference to these member function in 
> that class". If I put both the declaration and implementation into one 
> file, it works well.
> I have searched this problem in Google and there is two solutions, one 
> is to write declaration and implementation in one header file; the other 
> is to use the C++ keyword "export", but it seems that GNU does not 
> support this keyword. Only few C++ compilers support this.
> I found in the source code of ITK, it separate the declaration and 
> implementation of the template class into separated file(.h file and .cc 
> file). So can anyone give me some suggestions for this problem. Thanks a 
> lot for your help.
> Weidong
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users


More information about the Insight-users mailing list