Use of template templates in C + +

Source: Internet
Author: User

1, in C++template in a lot of places have used 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, and when a template is introduced to C + +, the template is initially defined as: Templates, where the class keyword indicates that T is a type. Later, in order 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. This allows you to define templates in the following ways: template. The keyword class in the template definition syntax has the same effect as 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) agrees that the user defines a pattern for the class. Make certain data members in the class, the parameters of the function of the dictation member, the return value of some member functions, can take the arbitrary type (including the system defined in advance and the user's own definition).
Suppose that the data type of a data member in a class cannot be determined. Either the number of parameters for a member function or the type of the return value cannot be determined. This class must be declared as a template, and its presence does not represent a detailed, actual class, but rather a class.


(2) class template definition
Define a class template that typically has two-part content:
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. The generic type T can be used as the type of a normal member variable and as a const and static member variable, as well as a member function's parameter and return type.

Like what:

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 a member function is defined outside the class definition body, if there is a template parameter in the member function, the template declaration must be made outside the body of the function, in addition to the definition of the member function in the body of the general class, such as

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 ;} 

Assume that a function is a return type with a common type, then "" On the class name suffix in front of the function name.

Like what:

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, such as

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 essentially an instantiation of a class template into a detailed class. It is 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 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. To support many different forms of participation. The function body that avoids overloading functions is repeatedly designed.

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><返回类型><函数名>(參数表){    函数体}

The template is a keyword that defines a templated function, and the angle brackets that follow it cannot be omitted; TypeName (or Class) is the keyword that declares the data type parameter identifier. The identifier used to indicate that it follows is a data type identifier. Thus, in a later definition of this function, any variable that wishes to determine the data type based on the actual reference data type can be described by the data type parameter identifier, so that the variable can adapt to different data types.

Like what:

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

A function template is simply a descriptive narrative of a function that declares a template. is not a function that can run directly, only after the actual data type to replace the type parameter identifier, the ability to produce a real function.


(2) Template function:
The generation of template functions is the process of instantiating the type of a function template.


Like what:

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

Then the system will replace the T-generation function in the function template with the data type double of the real participant D:

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

Use of template templates in C + +

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.