1. Template type & template function
1.1. template type
Template type (also called class template) is a type defined with a template. The following is an example. Template type can be used to construct object. The thing inside <> is called template arguments.
template<typename T> class MyContainer { ... }; // template type for greater<T> template <typename T> struct greater { constexpr bool operator()(const T& a, const T& b) const { return a > b; } };
In most cases (C++ 11), it’s not allowed to put object into template arguments
| Template type |
|---|
| greater<T> |
| vector<T> |
| set<T> |
| all kinds of STL containers |
For template type, we offent supply the type T diretly, so that compiler knows what kind of types of container we want. There are some cases, where compiler can reference some remaining types, using some other types already provided. For example
priority_queue<int, vector<int>, greater<>> // for most templat types its template parameters are types rather than functors
1.2. tempalte function
Template function is the pretty the same, fucniton defined with template, the following is an example:
Template funciton also uses the template argument <> concepts.
template <class RandomIt, class Compare> void sort(RandomIt first, RandomIt last, Compare comp);
| Template fucntion |
|---|
| sort() |
| lowerbound() |
| upperbound() |
For template fucntion, it’s ofent the case, the compiler reference the template argument (Type T) from the arguments we provided. Note that even if it’s a template fucntion, the arguments we provde needs to an obeject rather than type.
lower_bound(nums.begin(), nums.end(), greater<>()); // here we uses greater<>() to creater a functor object, compiler helps to deduce the type for <>