template <class T> class shared_ptr;
shared_ptr 定义如上,是一个template,具体应该怎么理解呢?
From wikipedia, Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
There are three kinds of templates: function templates, class templates, variable templates(since C++14).
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
The format for declaring function templates with type parameters is :
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
To create a template function that returns the greater one of two objects we could use:
template <class myType>
myType GetMax (myType a, myType b){
return (a>b?a:n);
}
Here we have created a template function with myType
as its template parameters
. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were regualr type.
To use this function template we use the following format for the function call:
function_name <type> (parameters);
For example:
int x, y;
GetMax <int> (x,y);
We also have the possibility to write class templates, so that a class can have members that use template parameters as types.
template <class T>
class mypair {
T value [2];
public:
mypair (T first, T second)
{
values[0]=first; value[1]=second;
}
};
The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:
mypair <int> myobject(115, 36);
This same class would also be used to create an object to store any other type:
mypair <double> myfloats (3.0, 2.18);
Template specialization allows us to have different code for a particualr data type.
It is possible in C++ to get a special behavior for a particular data type. This is called template specialization.
When we write any template based function or class, compiler creates a copy of that function/class whenever compiler sees that being used for a new data type or new set of data types(in case of multiple template arguments).
If a specialized version is present, compiler first checks with the specialized version and then the main template. Compiler first checks the most specialized version by matching the passed parameter with the data type(s) specified in a specialized version.
Templates are expanded at compiler time. This is like macros.
The difference is , compiler does type checking before template expansion.
The idea is simple, source code contains only function/class, but comiled code may contain multiple copies of same function/class.
Both are examples of polymorphism feature of OOP.
Function overloading is used when multiple functions do similar operations;
Templates are used when mutiple functions do identical operations.
Each instance of a tempalte contains its own static vairable.