1 / 19

Templates

Templates. Generic Programming. The Issue. Want function to find the larger of two values. Up Until Now. Need multiple versions: int largest(int num1, int num2) { //return largest } double largest(double num1, double num2) { //return largest }

bennettk
Download Presentation

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 Generic Programming

  2. The Issue • Want function to find the larger of two values

  3. Up Until Now • Need multiple versions: int largest(int num1, int num2) { //return largest}double largest(double num1, double num2) { //return largest} Person largest(const Person& p1, const Person& p2) { //return largest}

  4. Templates • Templated Functions • Specify way to build a function • Applies to any data type

  5. Template Declaration • template<class/typenameT> • class/typename interchangeable • T is a type parameter – can be anything • Can have multiple type parameters • https://www.tutorialspoint.com/compile_cpp_online.php

  6. Exe 1 • Go to https://www.tutorialspoint.com/compile_cpp_online.php • Implement the Min method following the idea of Slide #4 • Show me the output

  7. Type Parameter • Type parameter used to code template: • Whatever T is… • We return one • We take two as parameters

  8. Instantiation • Template instantiated when needed at compile time • New version of code createdfor each type templateapplied to

  9. Template Including • Template functionsmust have full code in.h file • Needed at compile timefor instantiation • Don’t have to worry aboutmultiple versions

  10. Templates • All T's must be same type • No conversions done

  11. Templates • All T's must be same type • No conversions done • Can force type

  12. Exe 2 • Try to call Max of Exe 1 with f1 and j • What is the key step to make it to work?

  13. Operators & Members • Template can assume any operators/members it wants • Compile error if type doesn't support

  14. Non Templated Params • Can have normal parameters in template function

  15. Templated Variables • Can use template type as local variable type

  16. Templated Class • Class can be template • Type of variable includes instantiation type:

  17. Templated Class Functions • Function definitions must be template • Scope resolution must include template

  18. Templates of Templates • Template can instantiate on templated type:

  19. Multiple Types • Template can have multiple types:

More Related