C + + notes (7): Generic Programming and templates (function templates and class templates)

Source: Internet
Author: User
Tags class definition types of functions

Generic Programming and templates

0. Generic Programming

1. Function templates

2. Class templates

-----------------------------------------------------------------------------------------------------------

0. Generic Programming

Generics are code that is written in a way that is independent of any particular type . The containers, iterators, and algorithms of the standard libraries described earlier are the specific applications of generic programming.
Templates are the basis for generic programming. You don't need to know how a template is defined when using a template, but today we'll show you how to define your own template classes and template functions.

-----------------------------------------------------------------------------------------------------------

1. Template Basics

function templates
As the following comparison function:
  template <typename t> //template and TypeName are keywords that remain constant and change only the type T
  int cmp (t v1, T v2)
{
if (V1 > V2)
Return 1
else if (V1 < v2)
return-1;
return 0;
}

CMP (10, 20);
CMP (STR1, STR2);


The compiler instantiates two different versions of CMP, using int and string instead of T to generate the above two types of functions.
int cmp (int v1, int v2)
{
if (V1 > V2)
Return 1
else if (V1 < v2)
return-1;
return 0;
}


int cmp (String v1, String v2)
{
if (V1 > V2)
Return 1
else if (V1 < v2)
return-1;
return 0;
}

You can declare a template function as an inline function.
Such as
Template<typename t>
inline int cmp (T v1, T v2);

Notice where the inline is placed.

-----------------------------------------------------------------------------------------------------------

2. Class templates
A class template is also a template that needs to be preceded by a keyword template followed by a templated parameter list.
The definition of a class template is similar to a generic class definition and can have its own data members and methods as well as type members. You can also define constructors and destructors.
Just when defining member functions for classes and classes, use template parameters as placeholders, exactly as they are used for built-in types or class types.
template<typename t>
class MyClass
{
Public
Class_name ();
~class_name ();
t func (t& t);
Private
T m_t;
};


class template member functions
The definition of a template member function needs to start with the keyword template keyword,followed by the template parameter list
Template<typename t>
T Class_name<t>::func (t& t)
{
//.....
}

Instantiation of
A template is a blueprint that the compiler uses to produce a specific type of version of a specified class or function. This process is called instantiation.
The function template is instantiated at the time of invocation. The class template is instantiated when the actual template type is used.
Such as
Myclass<int> obj;
When compiling, the compiler replaces the int type with the parameter T in the MyClass template class.

Generic programming and class templates are advanced topics, where we do not detail some of the bits and pieces of knowledge about it, the introduction of too much will make you feel foggy.
When you understand the C + + understanding of a certain program to learn the class template will have a greater receipt.
Let's introduce an example. Implement your own vector class.

C + + notes (7): Generic Programming and templates (function templates and class templates)

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.