Templates

可以把 customized 的类 / 一个 typename 传给 function。【函数模板】

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.

In C++ this can be achieved using template parameters. 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.

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

Last updated