References
- https://en.cppreference.com/w/cpp/language/templates
- We will be studing about only function templates and abbreviated function template using keyword 'auto' which is also explained in above link(auto is just c++20 feature).
Main points
- Templates have been introduced in 1991.
-
we use templates like:
template <typename/class T> //we can use the keywords typename or class T function_name(T parameter_names) // scope of T is within the function only. { }
-
we can use template using keyword
auto
which is just c++20 feature only.(compile with -std = c++20 )auto function_name(auto parameter_names) { // function body; note that 'auto' will deduce the type for us. }
#include<iostream> auto multiply(auto a, auto b) { return a*b; } int main() { int a=2,b=4; float c=2.5,d=2; std::cout<<multiply(a,b)<<"\t"<<multiply(c,d)<<std::endl; return 0; }
Output
8 5
-
Instantiation
It is process of making codes by compiler, In previous lecture 1 we saw that the compiler was make codes for us, and that is known as Instantiation.