Lecture #6 - Templates

Web Resources:

CppReference.com, CPlusPlus.com, C++ TechLinks
Going Native 2013 - many experts
Herb Sutter's Blog, Herb Sutter on C++11, Herb Sutter keynote - Going Native 2012
Scott Meyer's Blog, S.M. on thread suspension, S.M.: C++11 Sampler
Stroustrup's Home Page, Code Project, Boost Library

Content:

This lecture covers template functions and template classes. We will discuss a HashTable example.
  1. Notice:

    Two former students, now working at FaceBook, will be interviewing during the Career Fair, Feb 2nd.
    They will have an information meeting, directed especially at students from my classes:
    FaceBook Information Meeting
    11:00 am in LSB 001, Wednesday, February 1st
    If you are interested in either an internship or permanent employment, you will be welcome.
  2. Readings for Lecture #6

    Text: Study chapter 16 carefully
    Project #1
    Templates, Templates Presentation, ppt
  3. Glossary of Terms

    • Template:
      A function or class whose declaration includes one or more unspecified type parameters.
      Templates allow us to write one function or class that use more than one type. The specific type isn't specified until an application instantiates it with a type suited for the application.
  4. Syllabus
    Describes topics to be covered, reading material for you to digest before coming to class, and due dates for the assigned projects.
  5. Project #1, Project #2
  6. Template presentation and demo code:

    Quick overview

    Templates Presentation
    Templates are one of the terrific features of the C++ language. They are the basis of the Standard Template Library (STL) and are useful in day-to-day programming.

    fully functional C++ Properties, courtesy of templates

    First template demo

    Template Code Examples
    Basic and some more advanced template examples.

    STLContainers examples
    This code, linked on earlier slides, contains a simple example of the new Variadic Template functionality in the "inserter" project.

    STL Template-based Container Examples
    Another set of template code examples, including functors, algorithms, and several other things you need to know.

    Functor examples

    Excellent template Q&A - Marshall Cline, Excellent C++ Q&A - Marshall Cline
    If you are only going to go one place to find information to supplement class texts and notes,then go here!

  7. More advanced template presentations and demo code:

    HashTable Project
    HashTables are fundamental and very useful data structures. At this time there are no hash-based containers in the STL. So here is my implementation - it's a good example of the use of templates.

    Design of Hash Table and Iterators

    Template Policies and Traits

  8. New style casts:

    new-style casts: demo code

    There are four distinct things the new-style casts provide:

    • static_cast:
      1. converts a foreign type to a type defined by a class with a promotion constructor
      2. converts an instance of a type that provides a cast operator to a foreign type
    • const_cast:
      Provides a non-const reference to a const object. That allows you to apply a non-const function to a const object.
    • reinterpret_cast:
      Treat an instance of one type by the rules of another type.
    • dynamic_cast:
      Support safe downcasting from a base pointer or reference to a derived pointer or reference.
  9. Namespaces
    Basic information about how namespaces work. We will discuss why you want to use them.
  10. These are the most important items from today's lecture:

    Templates Presentation
    Templates are one of the terrific features of the C++ language. They are the basis of the Standard Template Library (STL) and are useful in day-to-day programming.

    Template Code Examples
    Basic and some more advanced template examples.

CST strip