简体   繁体   中英

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?

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.

itk::ImageToImageFilter is an abstract base class for image filters (computations which operate on images and produce images as outputs). It is meant to be used with itk::Image as both input and output. Most of the concrete filters give some limitations on the possible variations of input and output template types (dimension, pixel type float or integral etc).

Generally, if you want to learn how to use a library, reading its documentation is a good starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM