Chapter 2 templates and generic programming

Source: Internet
Author: User
Tags types of functions

 

1. Basic Concepts

Generic programming: code is written independently of any specific type. When using a generic program, we need to provide the type or value operated by the specific program instance. The containers, iterators, and algorithms in the standard library are examples of generic programming. Each container, such as vector, has a single definition, but supports defining many different types of vectors. The difference between them is that they contain element types.

Templates are the basis of generic programming.

Generic programming, like object-oriented programming, depends on some form of polymorphism. The polymorphism of object-oriented programming dependency is called Runtime polymorphism, the polymorphism of generic programming dependency is known as the compile-time Polymorphism or parameter polymorphism. For Runtime polymorphism, the same code can be used for objects of the base class or derived class type as long as reference or pointer of the base class is used. In generic programming, the functions and classes we write can be polymorphism used to span unrelated types during compilation. A class and a function can be used to manipulate multiple types of objects.

In C ++, templates are the basis of generic programming, and templates are the blueprints or formulas for creating classes or functions. If the standard library defines a class template vector, it can generate any number of specific types of vector classes, such as vector <int> and vector <string>.

2. template Definition

1. Example

1) int compare (const string & v1, const string & v2)

{

If (v1 <v2) return-1;

If (v2 <v1) return 1;

Return 0;

}

2) int compare (const int & v1, const int & v2)

{

If (v1 <v2) return-1;

If (v2 <v1) return 1;

Return 0;

}

(3) int compare (const double & v1, const double & v2)

{

If (v1 <v2) return-1;

If (v2 <v1) return 1;

Return 0;

}

We can use function overloading to compare different types of functions. However, we can see that the above three functions only have different form parameters and the function bodies are the same, we can use a function template to compare any type of data. It is not just three types. If you want this function to be used for an unknown type, you can only use the function template.

2. Function template Definition

A function template is a type-independent function that can be used to generate a specific function version.

Template <typename T>

Int compare (const T & v1, const T & v2)

{

If (v1 <v2) return-1;

If (v2 <v1) return 1;

Return 0;

}

The template definition starts with the keyword template, followed by the template parameter table. The template parameter table is a list of one or more template parameters expanded with Angle brackets. The parameters are separated by commas.

The template parameter table cannot be empty.

3. template parameters

A template parameter can be a type parameter or a non-type parameter that represents a constant expression.

Type parameters are defined after the keyword class or typename. Non-type parameters are declared after the type specifiers.

Here, the keywords class and typename are no different.

4. Use function templates

When using the function template, the compiler will infer that the real parameters of the template are bound to the form parameters. Once the compiler determines the actual template parameters, it is called an instance of the function template.

The type of the type parameter is inferred from the type of the real parameter.

For example, int a = 10; int B = 11;

Compare (a, B );

Int type for variable a. T in const T & v1 is int type. Likewise, the form parameter in const T & v2 is int type. Then, the type of the template parameter is inferred based on the type of the function parameter. The T in the corresponding typename T is int, and all the T in the template function is changed to int. It is called an instance that instantiates a function template.

5. inline function Template

The inline keyword must be placed after the template parameter table and before the return type. It cannot be placed before the keyword template. For example:

Template <typename T> inline T min (const T &, const T &);

3. Define a class template

For example:

Template <typename Type> class Queue

{

Public:

Queue ();

Type & front ();

Const Type & front () const;

Void push (const Type &);

Void pop ();

Bool empty () const;

Private:

//.....

};

// Implementation of member functions

Template <typename Type> void Queue <Type>: push (const Type & v1)

{

//....

}

Summary:

Function templates are the basis for creating algorithm libraries. Class templates are the basis of the resume standard library container and iterator type.

Templates and virtual functions should not be mixed together.

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1291948

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.