Both object oritend programming(OOP) and generic programming deal with types that are not known at the time the program is written. The distinction between the two is that OOP deals with types that are not known until run time, whereas in generic programming the types become known during compilation.
When we use a generic type, such as vector, or a generic function, such as find, we supply the information needed to transform that blueprint into a specific class or function. That transformation happens during compilation.
Function Templates
A function template is a formula from which we can generate type-specific version of that function
template <typename T> int compare(const T &v1, const T &v2) { if(v1 < v2) return -1; if(v2 < v1) return 1; return 0; }
A template definition starts with the keyword template followed by a template parameter list, which is a comma-separated list of one or more template parameters.
The compiler use the deduced template parameter to instantiate a specific verison of the function for us. When the compiler instantiate a template, it create a new "instance" of the template using the actual template arguments in place of the corresponding template parameters.
cout << compare(1, 0) << endl; // T is int vector<int> vec1{1, 2, 3}, vec2{4, 5, 6}; cout << compare(vec1, vec2) << endl; // T is vector<int>
In above example, the compiler will instantiate two different version of compare. One for int, another for vector<int>.
Each type parameter must be preceded by the keyword class or typename.
// error: must precede U with either typename or class template <typename T, U> calc(const T&, const &U); // ok: no distinction between typename and class in a template parameter template <typename T, class U> cal(const T&, const &U);
Template compliation
When the complier see the definition of template, it does not generate code. It generate code only when we instantiate a specific instance of the template.
- When we call a function, the compiler need to see only a declaration for the function.
- When we use objects of class type, the compiler need to see the class definition(class declaration), while the definition of the member functions need not to present.
So, we put class definition and function declaration in header file, and definiton of ordinary and class-member functions in the source files.
Template are different: To generate an instance, the compiler need to know the code that define a function template or class template member function.
So, we put both definition and declaration of templates in the header file.
Reference:
C++ Primer, Fifth Edition, chapter 16 Templates and Generic Programming