Was the template invented just to do almost the same replacement work as the macro? You can say yes or no.
On the one hand, templates can be used to replace types, which is no different from macros. It's just that macros are replaced purely on the basis of text in the compilation stage, and the replaced text itself has no semantics. Templates will be checked when parsing and instantiating templates, and the source code can also correspond to debugging symbols one by one, so debugging at compile time and run time is relatively simple.
On the other hand, templates and macros are also very different. The biggest difference between templates is that they can be operated. Let's look at an example:
The above function realizes the addition of uint8_t and uint8 _ t. Now what should I do to realize the addition of int 16 and int 16? The simple method is as follows:
But there are two difficulties:
Some people say that overloading and virtual functions can also solve the above problems:
Even defining a new structural variant in C language or using void* can solve this problem:
That's right, but what if I have various types of addition operations such as uint9_t, uint 10_t? Anyway, it is difficult to avoid the existence of if/else in any method.
The biggest difference between template and the above method is that template is a compile-time scheduling method, regardless of its parameters or types. What can be determined at compile time can be type checked, and the compiler can also optimize and cut off any unnecessary code execution path.
Let's look at an example template:
Then when two different types of variables are passed in, or t and float variables are not passed in, the compiler will prompt an error.
From a capability perspective, everything a template can do is done at compile time. Compile-time completion means that when you compile a program, all the quantities have been determined. For example, the following example:
As we can see from the above code, aVar and bVar must be integers. So if there is a proper mechanism, the compiler can know that AddFloatOrMulInt here only needs to execute the code on the Int path, and the compiler can also generate the code on the int path separately here, thus removing the unnecessary IF. In the template code, this "appropriate mechanism" refers to "specialization" and "partial specialization", and the latter is also called "partial specialization".
1.0 version-pseudocode
Version 2.0- Function Overloading
Version 3.0-Pure Template
Well, here's the problem! How to distinguish two classes with different contents but the same template parameters? Specialization! Specialization is to give a specified content when instantiating a template according to one or more special integers or types.
Version 4.0-Template Specialization
Explanation:
At this point, the first template specialization code has been written. AddFloatOrMulInt here is like a function, but it can only be executed at compile time. If you appreciate this, congratulations, your template meta-programming has been enlightened.
The core of this paper only talks about two issues: first, why generic programming is needed, with emphasis on the relationship between macros, templates and metaprogramming; The second is how to write the special code of template class. There are many details about specialization, which we will continue to discuss in later articles, and we will also introduce some knowledge points such as specialization, so please pay attention.