Introduction to C + + templates

Source: Internet
Author: User
Tags function definition

Templates are tools that support parameterized polymorphism in C + +, using templates that enable users to declare a generic pattern for a class or function so that some data members or member functions in a class get arbitrary types of parameters and return values.

A template is a tool for parameterization of types ;

There are usually two kinds of forms: function Templates and class templates ;

function templates for functions with different parameter types only;

Class templates are for classes that differ only in data members and member function types .

  The purpose of using templates is to allow programmers to write type-independent code . For example, to write a swap function that swaps two int types, this function can only implement the int type, the type of double, the characters cannot be implemented, and to implement these types of exchanges, we need to rewrite another swap. function. The purpose of using a template is to make the implementation of the program independent of the type, such as a swap template function, that can achieve the int type, but also can achieve a double type of exchange. Templates can be applied to functions and classes. The following are described separately.

  Note: the declaration or definition of a template can only be done within a global, namespace, or class scope. That is, it cannot be done within a local scope, such as a template cannot be declared or defined in the main function.

1. Function templates:

Template <class or you can use typename t>

return type function name (formal parameter list)
{//function definition Body}

Description: Template is a keyword that declares templates, which declares that a template keyword class cannot be omitted, and if the type parameter is extra, the class < type parameter list > can contain the base data type can contain the class type, before each parameter.

Please see the following procedure:

//Test.cpp#include<iostream>usingstd::cout;usingStd::endl;//declare a function template to compare the size of the parameters of the input two of the same data type, class can be replaced by TypeName,//T can be replaced by any letter or number. Template<classT>t min (t x,t y) {return(x<y)?x:y;}voidMain () {intn1=2, n2=Ten; Doubled1=1.5, d2=5.6; cout<<"smaller integers:"<<min (N1,N2) <<Endl; cout<<"minor real numbers:"<<min (D1,D2) <<Endl; System ("PAUSE");}

2. Class Template:

Define a class Template:

Template < class or you can also use typename T >
Class Name {
Class definition ...
};

Description: Where the template is a keyword that declares templates, it declares a template, the template parameter can be one, or it can be multiple.

For example, define a class template:

#ifndef classtemplate_hh#defineClasstemplate_hhTemplate<typename T1,typename t2>classmyclass{Private: T1 I; T2 J; Public: MyClass (T1 A, T2 b);//Constructor     voidshow ();}; //This is a constructor//Note these formatsTemplate<typename T1,typename t2>MyClass<T1,T2>:: MyClass (T1 a,t2 B): I (a), J (b) {}//This is void Show ();Template<typename T1,typename t2>voidMyclass<t1,t2>:: Show () {cout<<"i="<<I<<", j="<<J<<Endl;}#endif//Test.cpp#include<iostream>#include"ClassTemplate.h"usingstd::cout;usingStd::endl;voidMain () {MyClass<int,int> Class1 (3,5);     Class1.show (); MyClass<int,Char> Class2 (3,'a');     Class2.show (); MyClass<Double,int> Class3 (2.9,Ten);     Class3.show (); System ("PAUSE");}

3. Non-type template parameters

In general, a non-type template parameter can be a constant integer (including an enumeration) or a pointer to an externally linked object.

That is, floating-point numbers do not work, and pointers to internal linked objects are not possible.

int Maxsize>class  stack{private:    T elems[maxsize];}; int main () {    Stack<int> int20stack;    Stack<int> int40stack;};

Introduction to C + + templates

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.