Search code examples
c++templatesitk

Where to find requirement on the parameters of a general c++ template?


I try to understand how to use a template, like

itk::ImageToImageFilter< TInputImage, TOutputImage > Class Template Reference

as is documented here: https://itk.org/Doxygen/html/classitk_1_1ImageToImageFilter.html

The document does not seem to say the restriction on TInputImage or TOutputImage; neither does the code API. It seems that the two can be anything from "int", "double", to any kind of user-defined classes. This is strange. How would I know the requirement of the template parameters?


Solution

  • Actually there is information in docs, about what it can be. And it cannot be int, double, or something else.

    using   InputImageConstPointer = typename InputImageType::ConstPointer
    
    using   InputImagePixelType = typename InputImageType::PixelType
    
    using   InputImagePointer = typename InputImageType::Pointer
    
    using   InputImageRegionType = typename InputImageType::RegionType
    
    using   InputImageType = TInputImage
    

    So, TInputImage should have at least few types. Same for TOutputImage.

    Compiler will check that TInputImage have types, or typedefs that are pointed above. There is no concepts yes, so, only documentation (or compiler) can answer on question what restrictions are applied to templated type.

    And if you are just trying to understood templates, that is really not library that you should watch. From documentation I can say, that it's heavy templated.

    Simple example would be std::iterator_traits. It has

    Member types
    Member type Definition
    difference_type Iter::difference_type
    value_type  Iter::value_type
    pointer Iter::pointer
    reference   Iter::reference
    iterator_category   Iter::iterator_category
    

    You cannot construct instantiate iterator_traits with type, that has no difference_type, value_type, pointer, reference, iterator_category, it would be compilation fault. However, you can instantiate it with for example std::vector<int>::iterator, cause it has all this types.