07 - Templates
Memorize the exact syntax - including defaulting and instantiating
Templates are quite similar to macros – macros are code fragments that are parameterized, which then get
expanded – so are templates. “templates are parameterized declarations”
Templates are very useful in settings like building large websites where there is a need to repeat a lot of code
because some detail varies here and there and templates allow to parameterize this detail and reduce the repetition
of code.
,Functions can also be templated
if the types provided in <> (when using the template) cannot/do not have the operation being used in the template
code there will be compile time error.
,Templates provide parametric polymorphism – important to note that for this type of polymorphism (parametric)
almost no assumptions are made about the parameter i.e. some special implementations or methods
BUT because we know in some cases it will be a class ( <class T> ) some assumptions do get made at that time - such
as copy constructor, destructor, equality operator overloading. Just like in Java when you know it is an object you
can use some calls on the object e.g. .equals
This polymorphism is very similar to the parametric polymorphism in functional languages like Haskell and ML
(where very few assumptions are made about the parameter)
Difference from Haskell – there is no type checking done in C++ template parameters
, Important to note the 3 types of parameters above
When a base type (primitive type) e.g. int is sent into a class type there is auto boxing
You cannot parameterize over local classes
Memorize the exact syntax - including defaulting and instantiating
Templates are quite similar to macros – macros are code fragments that are parameterized, which then get
expanded – so are templates. “templates are parameterized declarations”
Templates are very useful in settings like building large websites where there is a need to repeat a lot of code
because some detail varies here and there and templates allow to parameterize this detail and reduce the repetition
of code.
,Functions can also be templated
if the types provided in <> (when using the template) cannot/do not have the operation being used in the template
code there will be compile time error.
,Templates provide parametric polymorphism – important to note that for this type of polymorphism (parametric)
almost no assumptions are made about the parameter i.e. some special implementations or methods
BUT because we know in some cases it will be a class ( <class T> ) some assumptions do get made at that time - such
as copy constructor, destructor, equality operator overloading. Just like in Java when you know it is an object you
can use some calls on the object e.g. .equals
This polymorphism is very similar to the parametric polymorphism in functional languages like Haskell and ML
(where very few assumptions are made about the parameter)
Difference from Haskell – there is no type checking done in C++ template parameters
, Important to note the 3 types of parameters above
When a base type (primitive type) e.g. int is sent into a class type there is auto boxing
You cannot parameterize over local classes