Templates - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Templates

Description:

Templates An introduction – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 12
Provided by: txs65
Category:

less

Transcript and Presenter's Notes

Title: Templates


1
Templates
  • An introduction

2
Simple Template Functions
  • template lttypename Tgt
  • T max(T x, T y)
  • if (x gt y) return x
  • else return y
  • int main(void)
  • int x 0
  • cout ltlt max(x, x) // prints 1
  • cout ltlt x // prints 1
  • string s hello
  • string t world
  • cout ltlt max(s, t) // prints world

3
Whats all this?
  • Templates have ugly syntax. Get used to it, and
    on an exam, be able to get in the ballpark of
    the correct syntax.
  • Conceptually, templates are a lot like macros,
    just without the unpleasant side effects.
  • When you see a template, or use a template, think
    text substitution and youll be close

4
Template Instantiation
  • A template definition is a recipe that the
    compiler can use to generate a function (or a
    class, more on that later)
  • The compiler will not use this recipe
    unless/until you instantiate the template.
  • At that point, the compiler goes and performs the
    text substitution you asked for, and then
    compiles the newly generated function as if youd
    written that function yourself.
  • What happens if I instantiate the same template
    multiple different ways?
  • Well, with function overloading, we just get two
    or more functions with the same name, but with
    different arguments!

5
Example (template definition)
  • template lttypename T, typename Ugt
  • T max(T x, T y)
  • if (x lt y) return y
  • else return x
  • T is the template parameter. Since the T is
    specified as a typename (i.e., the name of some
    type), then T can be replaced by any type
    (e.g., int or string). T can NOT be replaced
    by any arbitrary text, just by a type.
  • We have defined this template, which means the
    compiler now knows the recipe. But there is no
    machine code for the max function yet. The
    compiler wont actually compile the max function
    until we instantiate it.
  • The compiler does do some preliminary syntax
    checking, so you can get compiler errors in your
    template definitions even if you dont
    instantiate them.

6
Example (instantiations)
  • int main(void)
  • int x 3
  • x max(x, 5) // instantiation 1
  • double y max(1.0, 5.0) // instantitation
    2
  • y max(y, y 1) // not a new instantiation
  • y max(x, y) // uh oh! Ambiguous
  • y maxltdoublegt(x, y) // OK, the choice for
    T is explicitly double
  • For the first instantiation, the compiler can
    easily guess that T should be int
  • For the second instantiation, it is also obvious
    that T should be double (floating point
    constants are double)
  • The compiler instantiates a different version of
    the template for each distinct binding of T that
    you use (i.e., one time for int one time for
    double), not each time you call the function.
  • Since both arguments to max are supposed to be
    the same type (T), the compiler cant figure out
    what to do with the ambiguous line, should T be
    int (convert y) or should T be double (convert
    x).
  • We can remove the ambiguity in a few ways, the
    most certain is to simply tell the compiler what
    argument you want for the type paremeter T.

7
Template Classes
  • C also provides template classes.
  • Virtually any data structure (AKA collection,
    AKA container) will be implemented in C as a
    template
  • The type of data stored in the structure is
    really not at all relevant to the data structure
    itself.
  • Template classes get defined and instantiated
    in analogous ways to template functions with the
    following caveats
  • The compiler will never guess at the template
    argument for a template class, you must always
    explicitly tell the compiler what T is.
  • Classes cannot be overloaded, but the compiler
    will permit you to instantiate the same template
    class in multiple ways.
  • Each distinct instantiation results in a
    completely distinct class! (with its own copy of
    the static data members, for example).
  • The member functions in a template class are
    template functions (oh, how confusing!)

8
An example
  • template lttypename Tgt
  • class Foo
  • T x
  • static int count
  • public
  • Foo()
  • x 0
  • count 1
  • T getX(void) return x
  • int howMany(void) return count
  • template lttypename Tgt
  • int FooltTgtcount 0
  • int main(void)
  • Fooltintgt a
  • cout ltlt a.getX() ltlt endl // prints 0
  • Fooltintgt b
  • cout ltlt b.count() ltlt endl // prints 2

9
A Useful Example (abridged)
  • template lttypename Tgt
  • class vector
  • public
  • void push_back(T)
  • void pop_back(void)
  • T operator(int k)
  • explicit vector(int initial_capacity8)
  • private
  • T data
  • int length
  • int capacity
  • void expand(void) // increase capacity

10
Member functions are template functions, ugh.
  • template lttypename Tgt
  • vectorltTgtvector(int init_cap)
  • capacity init_cap
  • data new Tcapacity
  • length 0
  • template lttypename Tgt
  • void vectorltTgtpush_back(T x)
  • if (capacity length) expand()
  • datalength x
  • length 1
  • template lttypename Tgt
  • void vectorltTgtexpand(void)
  • capacity 2
  • T new_data new Tcapacity
  • for (int k 0 k lt length k 1)
    new_datak datak

11
Templates and .h files
  • C programs (just like C) are intended to be
    separately compiled.
  • Each module can be compiled independently and
    then linked together at the end to form the
    executable program.
  • This is nice for large development teams.
  • Type definitions and function declarations that
    are public (i.e., used by more than one module
    in the system) are usually placed into a .h file
  • Any module that needs to know about these
    functions or types simply includes the .h file.
  • When that module is compiled, the compiler checks
    the syntax by which you are calling those
    functions, but the compiler doesnt actually
    generate machine code for those functions i.e.,
    the compiler uses the .h file just to make sure
    the modules will interoperate.
  • BUT templates require that the compiler
    instantiate them (or else theres no machine code
    to interoperate with). And the compiler wont
    know which templates to instantiate (or how to
    instantiate them) until it looks at all the other
    modules in the project.
  • In practice this means that the entire template
    (not just declarations) must be placed into the
    .h file.
  • If youre writing a template class, you might
    consider just accepting this, and expanding all
    your member functions in place (inside the class
    definition).
Write a Comment
User Comments (0)
About PowerShow.com