Use of template templates in C + +

Source: Internet
Author: User

1, in c++template many places all use TypeName and class these two keywords, and seems to be able to replace, is not these two keywords exactly the same?
A: Class is used to define classes, after the introduction of C + +, the template is initially defined by the method: template, where the class keyword indicates that T is a type, and later to avoid the use of class in these two places may be confusing, so the introduction of typename this keyword, Its function, like class, indicates that the following symbol is a type, so that when defining the template, it can be used as follows: templates. The keyword class in the template definition syntax works exactly like TypeName.

2. The concept of class templates and template classes

(1) What is a class template
A class template (also known as a generic class or Class generation Class) allows the user to define a pattern for a class that enables certain data members in the class, parameters of the function of the dictation member, and the return value of some member functions to take any type (including system predefined and user-defined).
If the data type of a data member in a class cannot be determined, or if the type of the parameter or return value of a member function cannot be determined, the class must be declared as a template, and its existence does not represent a specific, actual class, but rather a class.
(2) class template definition
Defining a class template typically has two things in common:
A. First define the class, in the form of:

template <class T>class foo{……}

Foo is the class name, in the class definition body, such as a member of a common data type, the function parameter is preceded by a T, where the generic type T can be used as the type of the normal member variable, and can also be used as a const and static member variable and as a parameter and return type of the member function. For example:

template<class T>class Test{private:    T n;    const T i;    static T cnt;public:    Test():i(0){}    Test(T k);    ~Test(){}    void print();    operator+(T x);};

B. When defining member functions in the class definition body, if a template parameter exists in this member function, the template declaration must be made outside the body of the function, in addition to the definition of the member function defined in the body of the generic class.
For example

template<class  t ,  void test  < t  >:  :p rint  () {STD:  :cout<<  "n="  <<n<<std  :  :endl ; STD:  :cout<< "i="  <<i<<std  :  :endl ; STD:  :cout<< "cnt="  <<cnt<<std  Span class= "Hljs-symbol" >: :endl ;} 

If the function is to return a type with a common type, then "" On the class name suffix before the function name. For example:

template<class T>Test<T>::Test(T k):i(k){n=k;cnt++;}template<class T>TTest<T>::operator+(T x){               return n + x;               }

C. The practice of initializing Const members and static member variables outside the class definition body and Initializing const members and static member variables outside of the normal class are essentially the same, with the only difference being that the template needs to be declared again, for example

template<class T>Test<T>::cnt=0;template<class T>Test<T>::Test(T k):i(k){n=k;cnt++;}

(3) The use of class templates is actually used to instantiate a class template into a specific class, in the format: class name < actual type >.
A template class is a product of the instantiation of a class template. Let's say an example of the image point. I compare the class template to a cookie-making mold, and the template class is the cookie made with this mold, and the cookie is what it tastes like to see yourself in the instantiation of what is the material, you can make chocolate biscuits, can also make bean paste biscuits, these biscuits in addition to the material is not the same, Everything else is the same.
3. function templates and template functions

(1) Function template
Function templates can be used to create a common function that supports a number of different formal parameters and avoids repetitive design of function bodies that overload functions. Its greatest feature is the data type used by the function as a parameter.
The function template is declared in the following form:

template<typename(或class) T><返回类型><函数名>(参数表){    函数体}

Where template is the keyword that defines a templated function; the angle brackets behind the templates cannot be omitted; TypeName (or Class) is the keyword that declares the parameter identifier of the data type, indicating that the identifier following it is the data type identifier. Thus, in a later definition of this function, any variable that wishes to determine the data type based on the actual argument type can be described with the data type parameter identifier, so that the variable can be adapted to different data types. For example:

T>T fuc(TT y){    T x;    //……}

A function template simply declares a description of a function as a template, not a function that can be executed directly, but only if the data type of the argument is used instead of the type parameter identifier in order to produce a real function.
(2) Template function:
The generation of template functions is the process of instantiating the type parameters of a function template.
For example:

double d;     int a;     fuc(d,a);

The system will use the data type of argument D as a double to replace the T generation function in the function template:

double fuc(double x,int y){    double x;    //……}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of template templates in C + +

Related Article

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.