110 likes | 343 Views
Templates. a mechanism for generic programming. Why Templates. certain code operates similarly regardless of type example: swapping two values templates parameterize type and allow writing generic code. Standalone Function Templates. function head/prototype are preceded by
E N D
Templates a mechanism for generic programming
Why Templates • certain code operates similarly regardless of type • example: swapping two values • templates parameterize type and allow writing generic code
Standalone Function Templates • function head/prototype are preceded by template <typename typeParameter> • style convention – always put on separate line • example template <typename T> void printStuff(int a, T b, T c); • type parameter may be passed by reference/returned/declared constant template <typename T> void showStuff(int, T&, const T&); • in definition template <typename T> void showStuff(int a, T& b, const T& c){ cout << a << b << c; }
Function Template Instantiation • invoked as ordinary function double one=3.5, two=5.6; string str1="one", str2="two"; showStuff(1, one, two); showStuff(3, str1, str2); • templates are not executable code • compiler generates executable code when it sees function invocation. This is called template instantiation • compiler deduces the type of template parameter by the type of arguments • parameterized function is generic function • cannot be compiled separately • should be placed in headers
Explicit Type Specification • deduction may be ambiguous double one=3.5; int two=5; showStuff(1, one, two); // is it int or double? • explicit type specification suppresses deduction and eliminates ambiguity showStuff<int>(1, one, two);
Class Templates • parameterized class is generic class • class definition is preceded by template <typename typeParameter> • example template <typename T> myclass{ private: int a; T b; T *c; }; • member functions of a generic class are generic functions • member functions can be defined inline or outside template <typename T> void myclass<T>::showStuff(int, T&, const T&);
Member Function Definition • member functions can be defined • inline template <typename T> yourclass{ public: int geta() const{return a;} T getb() const; void setc(T *const); private: int a_; T b_; T *c_; }; • or outside – use scope resolution operator and template keyword template<typename T> T yourclass<T>::getb() const { return b_; }
Object Declaration and Usage • to use template - declare an object of the class, have to explicitly specify type parameter yourclass<double> d1, *pd1; • methods are invoked as on ordinary objects pd1=&d1; cout << d1.geta(); cout << pd1->getb(); • class (or function) templates are not executable, compiler instantiates class templates when objects are declared, methods are instantiated when invoked • put class templates in header files • there are ways not to put templates in header files, but none good
Nontype Parameters • of specific types/classes may be specified template<typename T, int Size> class Stack { … private: T items_[Size]; int top_; }; • treated as named constant • initialized at instantiation: Stack<char,10> charStack; • may have default values: template<typename T, int Size=100>… • may be skipped at instantiation Stack<int> instStack; // contains 100 items
Templates as Type Parameters • are allowed, no template modification is required Stack<Stack<char,5>,50> pileOfStacks; • declares a stack of 50 stacks of characters • as of C++11, double closing brackets are allowed (compilers used to confuse it with extraction operator/right shift operator) Stack<Stack<char>> batchOfStacks; what does this declare?