First look at an example:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/E9/wKiom1bya0-QrTSYAAAjTtqh81A714.png "title=" 7.PNG " alt= "Wkiom1bya0-qrtsyaaajttqh81a714.png"/>
We have found that many functions must be written out when comparing the equality of A and B, because of their type uncertainties. These functions have the same type of arguments and different return value types, and others are the same. This makes the program look cumbersome, whether there is a way to complete the program can be both concise and can be more than the different types of comparison function? Of course there is. That's the template.
The so-called template, is equivalent to the same formula. Template programming is also called generic programming. You can handle situations where the type is not known when you write the program, and you can get the type at compile time.
So:
The above program can be replaced by the following function template:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/E9/wKiom1bybD_hSKPiAAAVi7cFf4w962.png "title=" 44.PNG "alt=" Wkiom1bybd_hskpiaaavi7cff4w962.png "/>
When called:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/E6/wKioL1bybTDjXZVoAAAaUj47GEA426.png "title=" 55.PNG "alt=" Wkiol1bybtdjxzvoaaaauj47gea426.png "/>
2. Templates are divided into function templates and template classes.
(1) Function template
Format: template <typename t1,typename T2,... >
return type function name (parameter list)
{function Body}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/E9/wKiom1byYp7B_YXFAAAOlxL0RTw600.png "title=" 1.PNG " alt= "Wkiom1byyp7b_yxfaaaolxl0rtw600.png"/>
When called:
(equal);
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/E6/wKioL1byZN7DZ8woAAAQCs3k_2A311.png "title=" 3.PNG " alt= "Wkiol1byzn7dz8woaaaqcs3k_2a311.png"/>
T1,T2 type is not the same,
When called:
Equal (1, (int) 2.3);
Equal<int> (2,2.5);//Display instantiation
(2) Template class
Format: template <typename t1,typename T2,... >
Class Name {};
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/E9/wKiom1byY96TVs6DAAAOgfG3gKc708.png "title=" 2.PNG " alt= "Wkiom1byy96tvs6daaaogfg3gkc708.png"/>
3. Template compilation
When the compiler encounters the definition of a template, it does not generate code for it, but the compiler generates code when we instantiate a special case of the template.
Usually, when we call a normal function, we know the declaration of the function is mastered. When you define an object of a class type, we can place the class definition and the normal function declaration in the header file, but the definition of the function and the definition of the member function of the class are placed outside the class. However, in order to generate an instantiated object, the compiler needs to know the definition of the function template and the member function of the class template, so the definition of the class, the definition of the class member function, the declaration of the ordinary function, and the definition should be placed in the header file.
This article from "Together to see the Stars" blog, reproduced please contact the author!
Templates for C + +