Class templates in C + + tell the details

Source: Internet
Author: User
Tags class definition int size

I. Definition and instantiation of class template 1 define a class template:
1 template<class template Parameter table >

3 class Name {

5//class definition ...

7};

Where template is a keyword declaring a class template, which means declaring a template, which can be one or more, can be a type parameter , or it can be a non-type parameter. type parameters are composed of the keyword class or typename and the identifiers that follow it. A non-type parameter consists of a common parameter that represents a constant in the template definition.

Cases:

1 Template<class type,int width>

3//type is a type parameter, width is a non-type parameter

5 class Graphics;

Attention:

(1) if a variable with the same name as the template parameter is declared in the global domain, the variable is hidden.

(2) a template parameter name cannot be used as the name of a class member in a class template definition.

(3) the same template parameter name can appear only once in the template Parameters table.

(4) in different class templates or declarations, template parameter names can be reused.

1 typedef string Type;

3 Template<class type,int width>

5 class Graphics

7 {

9 type Node;//node is not string

typedef double TYPE;//Error: member name cannot have the same name as template parameter type

13};

Template<class type,class type>//Error: Reusing a parameter named type

-Class Rect;

Template<class type>//Parameter name "type" can be reused between different templates

Class Round;


(5)
In the forward declaration and definition of a class template, the name of the template parameter can be different.

1//All three Image declarations refer to a declaration of the same class template

3 Template <class t> class Image;

5 Template <class u> class Image;

7//Real definition of the template

9 Template <class type>

Each class Image {//template definition can only refer to the first name "Type", cannot reference the name "T" and "U"};


(6)
Class template parameters can have default arguments, and the order in which the parameters are provided with default arguments is first right and left.

1 template <class type, int size = 1024>

3 class Image;

5 template <class type=double, int size >

7 class Image;


(7)
The class template name can be used as a type indicator. When a class template name is used as a type indicator in another template definition, the full argument table must be specified

1 Template<class type>

3 Class Graphics

5 {

7 Graphics *next;//No need to specify a full template parameter table in the class template's own definition

9};

Template <calss type>

void Show (Graphics<type> &g)

15 {

Graphics<type> *pg=&g;//must specify the complete template parameter table

19}

2.
instantiation of class templates

Definition: The process of generating a class from a generic class template definition is called template instantiation.

Example:graphics<int> gi;

When will the class template be instantiated?

When the name of the class template instance is used, and the context requires the definition of the class to exist.

The object type is a class template instance when the object is defined. This point is referred to as the instantiation Point of the class.

A pointer or reference to an instance of a class template when the pointer is checked or the object referred to is referenced.

Cases:

1 Template<class type>

3 class graphics{};

5 void F1 (graphics<char>);//is only a function declaration and does not need to be instantiated



9 {

graphics<double>& rsd;//Declaration of a class template reference, no instantiation required

Graphics<int> si;//si is a Graphics type object that requires the instantiation of class templates

15}

+ int main () {

Graphcis<char>* sc;//declares only one class template pointer and does not need to be instantiated

F1 (*SC);//needs to be instantiated because a Graphics<int> object is passed to the function F1.

iobj=sizeof Int (graphics<string>);//need to instantiate, because sizeof calculates the size of the Graphics<string> object, in order to calculate the size, The compiler must produce the type according to the class template definition.

25}

3.
template arguments for non-type parameters

Key points :

An expression bound to a non-type parameter must be a constant expression.

allows some conversions between types from template arguments to non-type template parameters. Includes lvalue conversions, qualifying modifiers, elevation, and integer value conversions.

There are some limitations to the kind of template arguments that can be used for non-type template parameters.

Cases:

View Code

II. member functions of class templates

Points:

The member functions of the class template can be defined in the definition of the class template (inline function) or outside the class template definition (at which point the member function definition must be preceded by a template argument and a stencil parameter).

The class template member function itself is also a template that is not automatically instantiated when a class template is instantiated, and is instantiated only if it is called or taken.

View Code

Friend Declaration of class template

There can be three friend declarations in a class template:

1 . non-template friend class or friend functionView Code

2 , a bound friend class template, or a function template. 3 , a non-binding friend class template, or a function template.

The second declaration indicates that an instance of a class template and its friends are a one-to-two mapping relationship.

The third declaration represents a one-to-many mapping between an instance of a class template and its friends.

Example: Binding friend template

View Code


Example: a non-binding friend template

View Code


Note
: When non-template classes or functions are declared as class template friends, they do not have to be declared or defined in the global domain, but a member of a class is declared as a friend of a class template that must already be defined, and the template must be declared when it declares a friend class template or function template that is bound.

Cases:

View Code

Iv. static data members for class templates, nested types 1 . static data members for class templates

Points:

The template definition of a static data member must appear outside the class template definition.

class template A static data member is itself a template, and its definition does not cause memory to be allocated and memory is allocated only if it is instantiated.

When a program uses a static data member, it is instantiated, and each static member instance corresponds to a class template instance, and the instance reference of a static member is passed through a class template instance.

Cases:

View Code

2. nested types of class templates

Key points :

allows you to embed templates in a class template, so the class template's nested class is also a template that can use template parameters for the perimeter class template.

When a perimeter class template is instantiated, it is not automatically instantiated and is instantiated only if the context requires its full class type.

A public nested type can be used outside the class definition, and the name of the class template instance must precede it.

Cases:

View Code
V. Template of Members

definition: Before a member definition is added Template and Template Parameters tables.

Points:

defines a member template in a class template, meaning that an instance of the class template contains an infinite number of nested classes and an infinite number of member functions.

is instantiated only if the member template is used.

The member template can be defined outside its perimeter class or class template definition.

Cases:

View Code


Note: class template parameters are not necessarily the same as those specified in the class template definition.

Vi. compilation mode for class templates 1. include compilation mode

In this compilation mode, the definition of member functions and static members of a class template must be included in all files to be instantiated, and if a member function is defined outside the class template definition, then those definitions should be placed in the header file that contains the class template definition.

2. Detach Compilation Mode

In this mode, the class template definition and its inline member function definitions are placed in the header file, and not the inline member functions and static data members are placed in the program text file.

Cases:

View Code


The member definition of Setup is not visible in user.c, but the template instance graphics<int>::setup (const int &) can still be called in this file. To achieve this, the class module must be declared exportable: when its member function instance or static data member instance is used, the compiler requires only the template's justification, which is declared in the keyword Template Pre-add Keyword export

3 . an explicit instance declaration

When using the Include compilation mode, the definition of a class template member is included in all program text files that use its instance, when and where the compiler instantiates the definition of a class template member, we do not know precisely, in order to solve this problem, standard C + + Provides an explicit instance declaration: The keyword template is followed by the keyword class and the name of the class template instance.

Cases:

View Code


When a class template is explicitly instantiated, all its members are also explicitly instantiated.

The special and partial special of class template 1. the specificity of class templates

Let's look at the following example:

View Code


If the template argument is a Rect type, we do not want to instantiate the member function out () by using the generic member function definition of the class template graphics, and we want to specifically define the Graphics<rect>::out () instance so that it uses the member function inside the Rect.

To do this, we can provide a special definition for a member of a class template instance through a display-specific definition.

format: Template <> member function Special definition

The following is an explicit specificity for the member functions of the class template instance graphics<rect> ():

template<> void Graphics<rect>::out (Rect figure) {...}

Attention:

only if the generic class template is declared, its explicit specificity can be defined.

If you define a class template specificity, you must define all member functions or static data members that are related to this specificity, where the class template-specific member definition cannot begin with a symbolic template<>. (Template<> is omitted)

The class template cannot be instantiated in some files based on a generic template definition, but in other files it is specific to the same set of template arguments.

2. partial specificity of class templates

If the template has more than one template parameter, some people might want to have a particular template argument or a set of template-specific class templates, rather than the class template for all template parameters. That is, you want to provide a template that is still a generic template, except that some template parameters have been replaced by actual types or values. This can be achieved by using a partial specificity of the class template.

Cases:

View Code


Format:
template< template parameter table >

Attention:

The template parameter table for partial specificity lists only those parameters that are still unknown to the template arguments.

-class template partial specificity is implicitly instantiated. The compiler chooses to instantiate the template definition that is most specific to this instance and uses the generic template definition when no specificity is available.

Example:graphics<24,90> figure;

It can be instantiated from a generic class template definition, or it can be instantiated from the definition of a partial customization, but the compiler chooses to instantiate the template with partial specificity.

The class template partial specificity must have its own definition of member functions, static data members, and nested classes.

Viii. Namespaces and class templates

Class template definitions can also be placed in namespaces. For example:

View Code


When a class template name graphics is used outside of a namespace, it must be fixed by the namespace name Cplusplus_primer, or introduced through a using declaration or indicator. For example:

View Code

Note: declaring a class template in the namespace also affects how the class template and its members are both specific and partial, and the custom declaration of a class template or class template member must be declared in the namespace that defines the generic template (you can define template specificity outside the namespace).

An example of a queue, the following code is organized as follows:

View Code


Run results

Class templates in C + + tell the details

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.