1 / 10

Templates

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

tory
Télécharger la présentation

Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Templates a mechanism for generic programming

  2. Why Templates • certain code operates similarly regardless of type • example: swapping two values • templates parameterize type and allow writing generic code

  3. 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; }

  4. 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

  5. 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);

  6. 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&);

  7. 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_; }

  8. 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

  9. 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

  10. 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?

More Related