CS106L: Lec08 Templates
Function Template: A function template defines a family of functions. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types.
- specify the type T
cout << myMax<int>(2, 3) << endl;
- leave the type for the compiler to deduce
cout << myMax(2, 3) << endl
template functions are not compiled until used!
For each instantiation with different parameters, the compiler generates a new specific version of your template at compile time.
Template code is instantiated at compile time.
Class Template: A class that is parametrized over some number of types. A class that is comprised of member variables of a general type/types. Use generic typenames as placeholders.
Templated code implementation should never be in a .cpp file: your compiler has to see them at the same time as it sees the code that calls them.
Generic Programming is a programming paradigm for developing efficient, reusable software libraries.
Generics is the idea to allow type (Integer, String, … etc) to be a parameter to methods, classes and interfaces.
Generics can be implemented in C++ using Templates.
The advantages of Generic Programming are
- Code Reusability
- Avoid Function Overloading
- Once written it can be used for multiple times and cases.
Normally, code runs during runtime. But with
template metaprogramming(TMP), code runs once during compile time. Something runs once during compiling and can be used as many times as you like during runtime.
struct is similar to
class in that it can contain both member variables and member functions.
the difference of struct and class
- When using class, the members of a class are all private by default, while when using struct, the members of a struct are all public by default.
- class can be used as a template keyword, while struct cannot.
- class inheritance is private inheritance by default, while struct inheritance is public inheritance by default