C ++ 11 parameters default template function template

Template categories: generic class description (used to define the generic class), for instance of where a particular type of generic then replaced.

Function template: a general purpose function description (used to define the generic functions), instantiated, then the generic type in which replacement of concrete.

Difference between the standard [1] in both the C ++ 98

Function templates and class templates are introduced together with the standard C ++ 98, both the main difference is:

When the class template declaration, allowing it to have a standard default template arguments. The function template is not supported.

Parameters default template acts as a default function parameter. But 11 in C ++, this restriction has been lifted, in the following example:

. 1  void DefParm ( int m = . 3 ) {} // C ++ compiler 98, c ++ 11 compiler 
2  
. 3 Template <typename T = int >
 . 4  class defclass {};         // C ++ compiler 98, c ++ 11 compiler 
. 5  
. 6 Template <typename T = int >
 . 7  void DefTempParm () {};     // C ++ compiler 98 fails, c ++ 11 compiler

You can see, DefTempParm function template has a default template parameters (type int).

Use only supports C ++ compiler 98, the compiler DefTempParm will fail, and compiler support for C ++ 11 is no problem.

The difference between the two [2] Standard C ++ 11

Although C ++ 11 support the default template template parameter function, but syntactically, there is still difference between the two:

Class template when specify default values ​​for several default template parameter declaration, must be specified in accordance with "right to left" rule.

And this rule to function template is not necessary. Examples are as follows:

. 1 Template <typename Tl, T2 = typename int >
 2  class DefClass1 {};
 . 3  
. 4 Template <typename Tl = int , typename T2>
 . 5  class DefClass2 {};   // ERROR: not compile : template parameters because the default value is not follow "from right to left" rule 
. 6  
. 7 Template <typename T, int I = 0 >
 . 8  class DefClass3 {};
 . 9  
10 Template < int I = 0 , typename T>
 . 11  class DefClass4 {};   // ERROR: Unable compiled by: Because the default value of the parameter does not follow the template "from right to left" rule 
12 is  
13 is Template <typename Tl = int , typename T2>
 14  void DefFunc1 (Tl A, T2 B) {}; // the OK function template do not follow the " from right to left "rule 
15  
16 template < int I = 0 , typename T>
 . 17  void DefFunc2 (T a) {};   // the OK function template do not follow the" from right to left "rule

We can see, not from right to left and DefClass4 template class DefClass2 define the default class template parameters are not compile.

As for the function template, the default template parameter position is more casual.

DefFunc1 and DefFunc2 defines default parameters for the first template parameter, and the default value of the second template parameters are not defined, C ++ 11 compiler thinks there is no problem.

Function template argument deduction rules are not complicated. Simply put: If you can deduce from the type of function arguments in the case, then the default template parameters can not be used, otherwise, the default template arguments might be used.

The following example:

. 1 Template < class T, class the U-= Double >
 2  void F (T = T 0 , the U-U = 0 ) {};
 . 3  void G () 
 . 4  {
 . 5      F ( . 1 , ' C ' ); // F <int , char> (. 1, 'C') 
. 6      F ( . 1 );       // F <int, Double> (. 1, 0), using default parameters template Double 
. 7      F ();        // error: T can not be derived 
. 8      F < int > ();   //f <int, double> (0 , 0), using default parameters template Double 
. 9      F < int , char > (); // F <int, char> (0, 0) 
10 }

It defines a function template f, f while using the default template arguments and default function parameter.

It can be seen that, since the function of the template parameters may be derived from the argument of function out:

In f (1) function call, a call is instantiated template function be f <int, double> (1, 0), wherein the second type parameter U uses the default parameter template type double, and function arguments or the default value of zero.

Similarly, f <int> () to instantiate a template function of a second parameter type double, the value 0.

The expression f () as the first type parameter T can not be deduced, resulting in the failure of compilation.

And you can also see by this example, the default template parameters are usually required function with default parameters used together.

Another point should be noted: the default form template function parameter value is not based on a template argument deduction. Template parameter selection function, after all, is derived from the argument of function comes.

 

good good study, day day up.

Select the cycle order summary

Guess you like

Origin www.cnblogs.com/Braveliu/p/12231451.html