template definition

function templates are special functions that can operate with generic types. this allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

the format for declaring function templates with type parameter is :

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

template <class myType>
myType GetMax (myType a, myType b) {
   return (a>b?a:b);
}


Iterators are the mechanism that makes it possible to decouple algorithms from containers: algorithms are templates, and are parameterized by the type of iterator, so they are not restricted to a single type of container.

from the point of view of the compiler,templates are not normal functions or classes,    they are compiled on demand, meaning that the code of a template function is not compiled until an instaniation with specific template arguments is required. at that moment ,when an instaniation is required, the compiler generates a function specifically for those arguments from the template.

http://www.cplusplus.com/doc/tutorial/templates/

猜你喜欢

转载自wwwjjq.iteye.com/blog/1647100