Special C ++ templates

Source: Internet
Author: User

Understanding of template specialization: there is no doubt about the whole process of specialization divided into full and partial features, but it is difficult to define the specific statuses of each of them, in addition, many authoritative books are inconsistent. If you care about it, we can use all kinds of special features. Next, let's talk about my personal division and definition of theme: the so-called theme is to make the generic stuff more concrete and explain it literally, it is to make some special settings for the existing template parameters, so that the previous template parameters are not subject to any constraints, or are subject to specific modifications (such as const or changing to something like a pointer, or even the template type after being packaged by another template class) or completely specified. This is some of my opinion on the Internet: There are two types of special templates: Fully-specific and partial-specific (partial-specific). The template functions can only be fully-specific, without being overly-specific (which may be available in the future ). The template class can be fully specialized and partial. All features, that is, all template parameters in the template are specified as determined types. The full feature defines a brand new type. The functions in the full feature class can be different from the template class. This is because the template parameters in the template are not fully determined and need to be determined by the compiler during compilation. Adding const, &, * (cosnt int, int &, int *, and so on) to the type does not produce a new type. Only the type is modified. The template can get the modified information during compilation. I personally agree with the division of this man-in-law. The mark of "all-in-one" is to generate something completely definite, rather than searching for appropriate special implementations during compilation, it seems that in my understanding, all things with special features, including classes and functions, have such characteristics, template <> then there is a class implementation or function definition that is completely irrelevant to the template type. If you want to say it is completely fixed, what template should you do, just define it? However, in many cases, we need a template to deal with various situations, and a specific type (such as bool) to have special processing, which is needed in this case. Now that we have mentioned the all-specific mark, let's talk about other common things: the mark of a special template class: added when defining the class implementation <>, for example, class A <int, T>. when defining A Template class, class A is not followed by A fully-specific sign of <>: template <> then the class implementation or function definition that is completely unrelated to the template type is biased: template <typename T ......>, that is to say, there is still something left, unlike the whole special <> the whole so thoroughly first recommended two good web site: http://www.cnblogs.com/cutepig/archive/2009/02/12/1389479.htmlhttp://read.newbooks.com.cn/info/175115.html first class template of the special it: Who did not say all the special: // general versiontemplate <class T> class Compare {public: st Atic bool IsEqual (const T & lh, const T & rh) {return lh = rh ;};// specialize for floattemplate <> class Compare <float> {public: static bool IsEqual (const float & lh, const float & rh) {return abs (lh-rh) <10e-3 ;}}; no one has said it is special: template <class T1, class T2> class A {} template <class T1> class A <T1, int >{} the next special type should be classified into full or partial, you can do it on your own, but we can't escape from the following: Special as reference, pointer type: // specialize for T * template <class T> class Compare <T *> {public: static bool IsEqual (const T * lh, const T * rh) {return Compare <T >:: IsEqual (* lh, * rh) ;}}; specially converted to another class template: // specialize for vector <T> template <class T> class Compare <vector <T >{public: static bool IsEqual (const vector <T> & lh, const vector <T> & rh) {if (lh. size ()! = Rh. size () return false; else {for (int I = 0; I <lh. size (); ++ I) {if (lh [I]! = Rh [I]) return false ;}} return true ;}}; mixed type: template <typename T1, typename T2> class X {...}; template <typename T> class X <vector <T>, int &> {...}; // As for how T2. T2. it's just dependent on a template parameter T. Don't worry. Let me tell you the essence, let me do this: 1. the number of template parameters is the same; 2. as long as the template <...> something in it is not <>, such as typename T, so T is used for special purposes; 3. without any modification to template parameters, such as template <typename T> class <T> {...}, at least you have to do something like const T, huh, huh. The following are some special situations I have developed. They are all correct: template <typename T1, typename T2> class X {}; template <typename T> class X <vector <T>, T & >{}; template <typename T> class X <vector <T>, int &> {}; template <> class X <vector <double>, int & >{}; template <typename T1, typename T2, typename T3> class X <map <T1, T2>, t3 & >{}; Finally, there is a super ox X which is used to implement the function in tr1. I have never seen it before and can play it like this: template <typename T> class Y; // This is to declare a class template. Since it is declared, we have to follow this rule in the future. In our previous programming experience, we can emphasize It's okay to declare a thing again, but it's no longer possible to declare a different thing for the same thing. Therefore, you cannot declare a template such as <typename T1, typename T2> class Y; in fact, nothing can be declared. Since we can declare variables, declare functions, and declare classes, we can also declare function templates or class templates. Template <typename R, typename P1, typename P2> class Y <R (P1, P2)> {...}; // The function type with two parameters and return values is special. Here, R (P1, P2) defines a type, which is an implicit function pointer and returns R, the parameters are P1 and P2. This function pointer definition is exactly the same as R (*) (P1, P2), but the previous definition is very uncommon, generally, you will not notice this place. Well, I have talked a lot about the specialization of class templates. Next I will briefly talk about the specialization of function templates: the specialization of function templates can only be full specialization, rather than partial specialization, therefore, it is relatively simple to specialize in functions, that is, to repeat them. For example: template <class T> T mymax (const T t1, const T t2) {return t1 <t2? T2: t1;} template <> const char * mymax (const char * t1, const char * t2) {return (strcmp (t1, t2) <0 )? T2: t1;} But you cannot do this: template <> bool mymax (const char * t1, const char * t2) {return (strcmp (t1, t2) <0) ;}in fact, for the definition of the template function mymax, a template parameter is used to control three places, so when you are making a special request, you also need to modify the three corresponding places with a specific type. If you have to return bool, you can define only one function template: template <class T> bool mymax (const T t1, const T t2) {return t1 <t2? The t2: t1;} problem comes again. We all know that the function overload does not care about the return value, but only about the number of parameters and whether the types are inconsistent. The inconsistency is the overload, but for the template function, this rule is no longer true, because everything related to the template is simply put there, as long as it complies with the syntax rules, these shelves work only when someone wants to call them. That is to say, they will search for appropriate template functions or classes during compilation, as long as they can be found, in addition, you can only find one. If you find more than one, you cannot. As a matter of fact, although functions cannot be differentiated, they cannot be followed by a <typename T> like a template class, however, you can use the function overload (note that the overload here refers to the template overload, rather than the function overload in the general sense) to implement a special implementation: template <typename T1, typename T2> bool mymax (T1 t1, T2 t2) {return t1 <t2? T2: t1;} template <typename T1> bool mymax (T1 t1, int t2) {return t1 <t2? T2: t1;} Let's Talk About the derivation of function template parameters. There are roughly the following methods, but no matter how we derive them, you must ensure that the types of template parameters of the template function can be determined before calling the function. Template <typename T1, typename T2> T2 fun (T1 arg1, int arg2) {T2 t2; return t2;} For the above special template functions, you cannot automatically obtain the types of all template parameters by passing parameters. Therefore, you must specify the T1 and T2 types. There are two ways to achieve this: int (* pfun) (double, int) = fun; // borrow a function pointer to define cout <pfun (12.2, 11) <endl; cout <fun <int, double> (11, 3.2) <endl; // directly specify the type. If the preceding template function is changed to template <typename T1, typename T2> T2 fun (T1 arg1, T2 arg2) {return arg2 ;} in addition to the two methods that specify the template parameter types, since the template function parameter types can be obtained by their parameters, we do not need to specify The template parameter step directly calls the template function fun (23, 2.3). Finally, let's talk about the non-type template parameter, in chapter 4 of C ++ Template. Template <typename T, int LEN> struct stack {...}; template <int margin> int add (int x) {return x + margin;} the preceding two examples correspond to the class and function situations respectively, some people say that non-type template parameters have no value, but they do not, because we can use a definite value to generate a new type or function. For the above two examples, I think it makes sense to use non-type template parameters to specify the stack size and the marginal value to be added. For more applications, you can gradually explore in the future development process. In addition, it is necessary to emphasize the restrictions on parameters of non-type templates, you cannot use floating-point, class-type, or internal linked objects (for example, String constant "hello world! ") As real parameters; they can be constant integers (including enumeration values) or pointers to external linked objects. For example, template <char const * name> class MyClass {...}; extern char const s [] = "hello"; MyClass <s> x; // OK, so I will put so much content in the template. I also posted some good stuff on the internet here: class template: * If the class template contains static members, these static members will be instantiated for each type of Instantiation. * A space must be left between two template angle brackets (>). Otherwise, the compiler considers it to be using operator>, resulting in a syntax error. * The implementation of special features can be completely different from the implementation of basic class templates. * Class templates can define default values for template parameters, which are called default template parameters. They can also reference previous template parameters. * Member function templates cannot be declared as virtual functions. * A class template cannot share a name with another object. Eg: 1 int C; 2 3 class C; // OK, 4 5 int X; 6 7 template <typename T> 8 9 class X; // error. conflict with variable X non-type template parameters: a constant value that can be determined during the compilation or linking period. The type of this parameter must be the following: a> integer or enumeration B> pointer type (Common Object Pointer, function pointer, member pointer) c> the reference type (pointing to an object or pointing to a function reference) Other types are currently not allowed to be used as non-type template parameters. Today, we suddenly mine some good stuff and paste it here: template <typename t> void f (t) {}// f1 template <> void f (int t) {}// f2 void f (int t) {} // f3 void f (char t) {} // f4 f (3); // invoke f3f ('3'); // invoke f4/** caveat: f3 must be put after f2, or an error occurs: specialization of void f (T) [with T = int] after instantiation; notes: the compiler will use f3 as the instantiation for f1, and use f2 as the specialization for f1; rule: specialization must be before instantiation (*); Above we have discuss the template function, and then we'll focus on member template function. acronym: MTF (member template function); Firstly, you shoshould pay attention to the rule: the specialization of MTF must be the outside of the class, I. e ., inline shoshould not be allowed. secondly, specialization and instantiation still follow the rule (*). but you 'd better put the instantiation outside of the class since specialization must be the outside of the class (the root cause is: if you wanna specialize a MTF, you shoshould give compiler the defenition of the templcate firstly. but if you use the specialization as the inline method, the specialization will be anxous since you can put the defination of MTF outside of the class. as you know, the inline member function precedes the non-inline member function. so the compiler will chose the safest way to solve it, I. e ., the specialization must be put outside of class declaration ). */

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.