C + + template parameter type (reprint)

Source: Internet
Author: User

There are actually three types of template parameters: type template parameter, untyped template parameter, and template template parameter (as a template parameter). 1, type template parameter type template parameters are the primary purpose of our use of templates. We can define multiple types of template parameters: Templates<typename T,typename container>classGrid {...} Similarly, you can specify a default value for the type template parameter: #include<iostream>usingstd::vector; Template<typename T,typename contianer=vector<t> >//Note Space    classGrid {...} 2Template template parameter is the parameter of a template as another template. As an example above: Grid<int,vector<int> >Myintgrid;      Note that int appears two times, and you must specify that the grid and vector element types are int. If written as: Grid<int,vector>Myintgrid; Because the vector itself is a template, not a type, so this is a template template parameter.      Specifying a template template parameter is a bit like specifying a function pointer parameter in a regular function. function pointer types include the return type and the parameter type of the function. Also include the full template declaration when declaring template template parameters: First of all, you need to know the prototype of the template as a parameter, such as vector templates<typename E,typename allocator=allocator<e> >classvector {...}; Then you can define: template<typename t,template<typename e,typename allocator=allocator<e> >classContainer=vector>classGrid { Public:       //omitted for brevitycontainer<t>*Mcells;      }; General syntax for template template parameters: Templates<otherparams,..., template<templatetypeparams>classParametername,otherparams,... >For example, an application, a constructor for a grid: template<typename t,template<typename e,typename allocator=allocator<e> >classContainer>Grid<t,container>::grid (intInwidth,intinheight): Mwidth (Inwidth), Mheight (inheight) {mcells=Newcontainer<t> [Mwidth];//Note Here container<t> instructions, in fact, grid<int,vector<int> >     for(intI=0; i<mwidth;++i) mcells[i].resize (mheight); When used, it's no different from the general: Grid<int,vector>Mygrid; Mygrid.getelement (2,3);      Note: Don't be stuck with its syntax implementation, just remember that you can use a template as a parameter to a template. 3, untyped template parameter No type template parameter cannot be an object, not even a double or float. Untyped parameters are limited to int, ENMU, pointers, and references. Sometimes you might want to allow a user to specify an element of a specific value to initialize an empty object, you can use the following method: Template<typename T,ConstT empty>classGrid { Public:        //omitted for brevityGrid (Constgrid<t,empty>&src); Grid<T,EMPTY>&operator=(Constgrid<t,empty>&RHS); //...      }; We can use this: Grid<int,Ten>Myintgrid; Grid<int, ->MyIntGrid2;      The initial value can be any number of int, which must be an int, a ENMU, a pointer, and a reference. 4, pointers, and reference template parameter pointers and reference template parameters must point to global variables that are available in all translation units.      For these types of variables, the corresponding technical term is data with external connections.      You can use the extern declaration. such as: Template<typename T,Constt& empty>classGrid {...}; extern Const intEmptyint=0; Grid<int,emptyint>Myintgrid;  For initialization we can also use "0 initialization" that is T (). 

C + + template parameter type (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.