Why are C + + generics present?

Source: Internet
Author: User

C + + as an evolution from C and adopt the object-oriented concept of semi-oriented semi-object-oriented programming language, on the basis of the explicit conversion and implicit conversion of C, but also has its own type conversion mode. Before we discuss the type conversion of C + +, let's first look at the important technical ideas that are used-generics.

Let's start with an example of a cliché, write a function that compares the size of two data objects, and, if not surprisingly, may have at least the following:

1 int cmp (int A,int b);

   2 double cmp (double A,double b);

   3 double int cmp (int A,double b);

   4 int cmp (char A,char b);

   5 int cmp (const A & A,const B & B); (A , B is A class)

We know that in C + + or Java (C does not support function overloading) there is an overloaded mechanism that allows the function names to be the same, the number of arguments and the parameter types are different.

C is how to solve the "function overloading" problem? Macro mechanism. For example, we can write the following macros:

   6 #define Min (x) < (y)? ( x):(y))

But what's wrong with the macro mechanism?

The first problem with macros is to not check the type and not to check for program errors during compilation, such as a custom object and an integer for Min operation;

The second problem is that macros are simply replaced with text, because the macro is prone to error results because of priority issues.

But even with function overloading, its drawbacks are obvious:

Code redundancy. The logic of these four functions is consistent, but four versions.

Another problem, the definition of a similar class.

C + + built-in data types (int double), if we want to create an array, use the standard mode: class A [Max] or class A * =new A  [Max]; If it is a custom class, and such a class is ever-changing, we need a mechanism to solve the duplicate definition problem of similar class, and reduce the programming effort.

There is one more problem, type-compatibility issue.

One. From the point of view of type conversion

Type conversion in C: (T) x

1. Built-in type (int double) object conversion Security Basic guarantee

2. Conversions between class objects can lead to uncontrollable results

Conversion of C + + type: T (x)

1. Single-parameter constructors and overloaded type conversion operators may be required

2. If the conversion is not implemented, it does not exist.

Two. Look at the Class library architecture

Depending on the class's inheritance hierarchy, it is necessary to frequently type-convert objects, such as pointers to base classes or references to subclasses, or subclasses to parent classes

However, the existing C + + type conversions are static transformations and cannot be adapted to the polymorphism of pointers or references

So we need a mechanism to ensure the type compatibility, this mechanism must have the following characteristics:

1. Ensure that the type conversion is valid and notify the user when it fails

2. Run-time type information for objects that need to be maintained (run-time type Information,rtti)

Above, is the value of the existence of generics: function overload problem Similar class definition problem type compatibility problem.

The specific implementation of generics-template and type parameterization is described in the next article.

Why are C + + generics present?

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.