Function template
We can function template as a special function inside the parameter type can be any type , so that we can reduce duplication defined, so that the function template automatically adapt to different types of parameters, that can accommodate a variety of functions types of parameters, for example double
, int
or what class.
C ++ in order to achieve the above functions, the introduction of template
this concept. We can as a template is a special type of parameters, and can also be passed as a parameter in a function, heart inside it as int
what's on the line.
Type parameters declared function template format is as follows :
template <class identifier> function_declaration;
template <typename identifier> function_declaration; // 这里使用了typename 当做声明类型参数
The above statement format template function is only a difference, that is class
, and typename
, in fact, here makes no difference what kind of use, they like to see on the line, C ++ did not do this strict distinction;
In order to better illustrate how to use the function template, give it in a very vivid example of that is a function that returns a larger value, the specific wording as follows:
template <typename T>
T GetMax(T a, T b)
return a > b ? a : b;
In the above few lines of code we created a template function is used to get the bigger number between the two numbers, easy to see T here does not specify a particular type, in which we can still be in my heart think of it as an ordinary variable processing, the return value is T.
To use our template function definitions, reference may be unified template function call format:
function_name<type> (parameters); // type就是具体的类型,例如int、double这些,parameters就是函数里的参数,也是具体类型
So, if we want to achieve access to two large integer value in that, we can pass this type int after calling GetMax
function, specific examples are as follows:
int x = 4;
int y = 2;
int max = GetMax<int> (x, y); // max = 4
Class Template
Because of the type of template declaration may be of any type , that is, when using specific types of incoming line. So you can refer to the above function template configuration examples of class template is the same, passing specific variables when using this type of class on the line. For example, the following class template definition:
template <typename T>
class MyPair{
T value[2];
public:
MyPair(T first, T second)
{
value[0] = first;
value[1] = second;
}
};
Sample code class definition of a template MyPair
class, for storing any type of two elements , for example double
, char
, int
like, two examples are given below I, are stored double
, and int
a variable of type
MyPair<int> myInt(2, 3); // 存储两个整型值
MyPair<double> myDouble(2.3, 3.43); // 存储两个double值
Template class member functions may be defined outside the class writing, and writing function template is the same, the specific example is shown below:
template <class T>
class MyPair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
T Getmax ();
};
template <typename T>
T MyPair<T>::Getmax () // 在类外定义函数, 注意前面的写法和函数模板写法一致
{
T retval;
retval = a>b? a : b;
return retval;
}
Template specialization
If we want to class template is defined as a different implementation of the interface, and requires specific parameters need to be passed as a parameter, then we can be the template specialization ;
To better illustrate the template specialization, or give a simple example. Suppose we define a class mycontainer
, the class may be stored in any type of a variable, and this class there is a function called increasement
to add a type of storage. And if this class stores is a char type, we will find this class to implement to convert one of the member variables to uppercase function will be more convenient, it might be this function is defined ToUpper
, so we can be of type char achieve specialization class template template, specific examples code is as follows:
// 模板特化
#include <iostream>
using namespace std;
// 声明类模板:
template <typename T>
class mycontainer {
T element;
public:
mycontainer (T arg) {element=arg;}
T increase () {return ++element;}
};
// char类型的模板特化:
template <>
class mycontainer<char> {
char element;
public:
mycontainer(char arg) {element=arg;}
char ToUpper()
{
if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;
}
};
For special template above, we need points, pay attention to class templates and template specialization of the differences and connections :
- In front of the class template plus
template <>
, indicating a template specialization, that is to say template specialization need to add this sentence; - Another point is that behind the use of class template
<char>
parameters, this particular parameter represents the template class specialization that we would drive<char>
type;
Other uses of template
In addition to template
or in class
addition to the template parameter indicates the type of the beginning of the declaration of keywords, template can also have other types of parameters, for example int
, double
these parameters, similar to a function with multiple parameters, each parameter type is not the same. In order to better illustrate template
other uses, it may refer to the following sample code,
#include <iostream>
using namespace std;
template<class T, int N> // 这里除了含有class声明的类型之外,还拥有一个int类型
class mysequence {
T memblock [N];
public:
void setmember(int x, T value);
T getmember(int x);
};
template<class T, int N>
void mysequence<T,N>::setmember(int x, T value) {
memblock[x]=value;
}
template<class T, int N>
T mysequence<T,N>::getmember(int x) {
return memblock[x];
}
int main(){
mysequence<int,5> myints;
mysequence<double,5> myfloats;
myints.setmember(0,100);
myfloats.setmember(3,3.1416);
cout << myints.getmember(0) << '\n';
cout << myfloats.getmember(3) << '\n';
return 0;
}