Templates in C + + (template)

Source: Internet
Author: User
Tags abstract min

My favorite technical article on the Internet is similar to a He Jun "CVS QuickStart" or "UML Reference Card" and so on, short and concise, can quickly lead you into a new world. For a longer article I usually save it to a hard disk, then prepare to "have time later" time to see, but their usual fate is "sit said Xuan Zong", until one day when the hard drive in the "unintelligible" into sin, delete.

This text is mainly aimed at the readers who have just contacted the concept of template, hoping to help readers learn the use of templates. In order to avoid this article also on the antiphon hard drive on the bad luck, I decided to write shorter. "When there is time", add some more content.

1. Introduction

The template is a new concept introduced by C + + in the 90 's, originally for the support of the container class (container classes) [1], but the effect of the template is far from what it was supposed to be.

Simply put, a template is a parameterized (parameterized) class or function, that is, the shape of the class (member, method, layout, etc.) or the form of the function (parameter, return value, etc.) can be changed by the parameter. What's even more magical is that the argument here is not just a numerical argument in our traditional function, it can also be a type (in fact, some people who know a bit more will notice the use of a type as a parameter and tend to ignore the use of numeric values as arguments).

Take a common example to explain that the template may be transformed into living code from a vague concept in your head:

In C, if we were to compare the size of two digits, we would often define two macros:

#define MIN (a,b) ((a) > (b)? ( b):(a))

#define MAX (A,b) ((a) > (b)? ( A):(B))

So you can in the code:

return min (10, 4);

Or:

return min (5.3, 18.6);

These two macros are very useful, but in C + + they are not as popular as they are in C. Macros are replaced by the inline function in C + + because they do not have type checking and are inherently unsafe (for example, if the code is written as min (a++, b--), which is obviously not what you want. But as you change the Min/max to a function, you immediately discover the limitations of the function-it cannot handle any type other than the type you specify. For example, your min () statement is:

int min (int a, int b);

It obviously can't handle the parameters of float type, but the original macro can work well! You'll probably think of a function overload later, by overloading the different types of min () functions, you can still make most of the code work correctly. In fact, C + + provides a better way to do this kind of abstract algorithm, which is the template:

template <class T> const T & min(const T & t1, const T & t2) {
  return t1>t2?t2:t1;
}

This is an example of a template function. After you have the template, you are free to use this template as you did in the C language using your min macro, for example:

return min (10,4);

You can also:

return min (5.3, 18.6)

Did you find out? You get a type-safe, and can support any type of min function, it is better than min macro it?

Of course, the above example only covers one aspect of the template, and the role of the template is far more than just an alternative to macros. In fact, templates are the basis for generalized programming (Generic programming). The so-called generalization programming, is to the abstract algorithm programming, generalization refers to can be widely applied to different data types. For example, the min algorithm we mentioned above.

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.