Analysis on the use of C++ template
This article mainly introduces the relevant knowledge of C++ template case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that you will gain something after reading this C++ template case analysis article. Let's take a look.
Untyped template parameter
Template parameter classification type shape participates in non-type parameter.
Type parameter: appears in the template parameter list, followed by a parameter type name such as class or typename.
A non-type parameter is to use a constant as a parameter of the class (function) template, which can be used as a constant in the class (function) template.
Note:
Floating-point numbers, class objects, and strings are not allowed as untyped templates.
Untyped template parameters must be able to confirm the results at compile time.
Template specialization
Sometimes, compiling the default function template or class template does not handle the logic correctly, and you need to specialize for some situations, so you need to do template specialization.
In general, templates can be used to implement some type-independent code, but for some special types, you may get some wrong results, such as:
Templatebool IsEqual (const TempleftGravity Const T&right) {return left==right;} / / specialization for string types-write a specialization of the function template templatebool IsEqual (const char* & left,const char* & right) {return strcmp (left,right) = 0;} int main () {cout