Generic programming is to write code in a way that is independent of any particular type, whereas a template is the basis for C + + generic programming.
The so-called template is a function or class written for "one or more types that are not yet clear."
When using a template, the type can be displayed or implicitly passed as a parameter.
Here is a typical example that returns the larger of the two numbers:
Template<class T>
inline Const T& MAX(Const T& a,Const T&b
{
return a>B?A:b
}
Here, the first row of T is defined as any data type, which is specified by the caller when the function is called.
This type has the keyword class boot, can also be used typename boot, TypeName is actually more intuitive than class.
(It is important to note that if you use nested dependency types, you must use TypeName).
Understanding :
The first line of Template<class T> tells the compiler that I have defined a mutable type T here, and what type of caller you are using to compile it!
Default Template Parameters
The template class can have default parameters, such as declarations, that allow you to declare MyClass objects using one or more template:
Template<class T,class Container=Vector<T> >
class MyClass
{
Public:
MyClass(){}
~MyClass(){}
protected:
Private:
};
If only one parameter is passed, the default parameter can be used as a second parameter:
MyClass<int> X1;//equivalent to:
MyClass<int,Vector<int> > X2;
Note: The template default parameter is defined based on the previous (or previous) parameter. This means that if a parameter in the argument pass list is a default parameter, all subsequent arguments should be default parameters.
Keyword TypeName
The keyword typename is used as the identification symbol before the type. Consider the following example:
Template<class subtype>
struct BaseType
{
subtype a;
};
Template <class T>
class MyClass1
{
TypeName T::subtype *ptr;
// ...
};
Here, TypeName points out that subtype is a type defined in class T, so PTR is a pointer to T::subtype.
If no typename,subtype will be treated as a static member, then:
T::subtype * PTR;
is interpreted as the product of the two child members subtype and PTR in type T.
Member templates
The class member function can be a template, but such member template types cannot be virtual or have default parameters, such as:
class MyClass
{
//...
Template<class T>
void F(T);
};
Here, Myclass::f declares a member function that applies to any type parameter.
This feature is often used to provide automatic type conversions for members in the template class, such as the parameter x of assign () in the following example, whose type must exactly match the type of object provided by the caller:
Template<class T>
class MyClass
{
Public:
MyClass();
~MyClass();
void Assign(Const MyClass<T>& x)//x must have same type as *this
{
value=x.value;
}
// ...
protected:
Private:
T value;
};
If two types are used, an error occurs even if the two types can be automatically converted:
void Fun()
{
MyClass<Double> D;
MyClass<int> I;
D.Assign(D);//OK
D.Assign(I);//Error:i is myclass<int> and myclass<double> is required
}
Understanding :
For a template member in a template class, follow the "preconceptions", and if the type is specified for the first time, then it will be consistent with the first time.
But what if you want to specify two different types of class member variables?
The method is very simple, we will use the member variable in a different class template type is the line:
Template<class T>
class MyClass
{
Public:
MyClass();
~MyClass();
Template<class X> //Member template
void Assign(Const MyClass<X>& x)//Allow different template types
{
value = x.Getvalu();
}
T GetValue()Const
{
return value;
}
// ...
protected:
Private:
T value;
};
void Fun()
{
MyClass<Double> D;
MyClass<int> I;
D.Assign(D);//OK
I.Assign(I);//OK (int is assigned to double)
}
Reprint please indicate source:http://www.cnblogs.com/crazyacking/p/4997425.html , infringement must investigate.
C + + Generic programming-template templates