ITK

From bone.wiki
Revision as of 20:26, 12 May 2010 by Enodwell (talk | contribs)
Jump to navigation Jump to search

What are .txx files?

.txx files are where ITK puts the implementation parts of templated classes (actually class templates). These can't go in a standard .cxx (or .cpp) file, because the compiler needs to have the complete implementation of a template in any compilation unit (normally a cxx file) that uses a template. This is because a template is just a pattern for making a class, and can't actually get built by the compiler until it has a specific type to make an actual version of a template (an instantiation). For most developers, this means that template implementations just get put into header files (.h or .hpp). In fact, this is effectively what happens in ITK - the .txx file gets included by the .h file. This is what you should do in any case if you want to write a new templated class: just include the .txx file at the bottom of your .h file.

Of course the actual story is more complicated. For example, it is possible for the developer to predict in advance what types will probably be used with the template, and to make explicit instantiations of these in a .cxx file that gets compiled into an object. This can save a lot of compilation time. However, if you try to use a different type, and you don't have the complete implementation available to your compilation unit, you will get linker errors.

If you want to know more about templates in general, this is a good source: http://www.parashift.com/c++-faq-lite/templates.html

ITK has in fact a much more sophisticated mechanism that provides explicit instantiations where possible and includes the template implementation only where an explicit instantiation is unavailable. If you want, you can read about it here: http://cmake.org/Wiki/Proposals:Explicit_Instantiation . Fortunately, if you are just using ITK classes in your code, it works transparently. And if you want to create a new class that inherits from an ITK class, you don't have to understand or use this mechanism: as mentioned just include your .txx at the bottom of your .h file and you're good to go.

Actually there is one more complication that you may need to know about. The VXL component (math stuff mostly) of ITK uses an entirely different mechanism for template implementations. For details see http://public.kitware.com/vxl/doc/release/books/core/book_17.html (scroll down to "C.5 On the Use of Templates") . Basically, VXL provides only explicit instantiations, so if you want to use some odd-ball type as a template parameter, you'll need to make your own explicit instantiation. VXL (and hence ITK) provide a macro for doing this in a single line. Refer to the linked page for details.