Templates (6) Flashcards
What is a template?
A template permits the definition of a template function or template class to use multiple data types
What is a function template?
A function template defines a specific behavior that can be applied to any data type
- Method overloading is used to provide function for each data type
What does the definition of a function template (code) look like?
template void function(T param){}
What is the difference between a function template and a template function?
Function template: entire range of related functions with single code segment
Template functions: individually compiled versions of the function template
What is a class template?
Allows the program to contain a generic class definition that can be instantiated into a type specific object
What does the definition of a class template (code) look like?
.h file: template class ClassName{ public: private: }
ClassName.cpp:
template
ClassName::ClassName(){}
main.cpp ClassName objectName();
What is the difference between a function template and a template function?
Class templates: entire range of related classes with a single class definition Template classes: individual instantiated versions of the class template
What is a specialization of a template?
Providing a template with a set of template arguments
Not instantiation –> can cause instantiation
Non specialized version still needed
What does specialization of a template look like (code)?
template<>
int Stack::sum(){}
template
T Stack::sum() {}
Why can class template errors slip by the compiler?
The compiler does not evaluate class templates. The compiler needs to know the data types in order to evaluate the methods –> methods that are unused are not evaluated