C C T B H P N

Lecture #6 - Templates

Building compile-time flexibility

Web Resources:

CppCon - 2014, Going Native 2013
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
Excellent C++ Q&A, Excellent reference - CppReference.com, CPlusPlus.com, C++ TechLinks

Content:

This lecture covers template functions and template classes. We will discuss a HashTable example.
  1. Syllabus
    Describes topics to be covered, reading material for you to digest before coming to class, and due dates for the assigned projects.
  2. Readings for Lecture #6

    Text: Study chapter 16 carefully
    Project #1
    Templates, Templates Presentation, pptx
  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. Projects:

  5. Template presentation and demo code:

    Quick overview Templates Presentation, pptx
    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. Template Techniques
    Illustrates syntax and also provides examples of template specialization. Variadic Templates
    Illustrates use of variadic function templates, variadic class templates, and shows one way to implement tuple. Properties
    Templates help us provide properties for C++, not supported by the native language. Utilities - several template classes
    Templates help us build flexible utilities. DirExplorerT
    Templates help us build reusable parts. Simple Demo of Callable Objects
    Look at this demo first. Illustrates first look at function pointers, functors, and lambdas. Functor examples
    Shows how templates and functors can help make code flexible. Demonstrate several useful variations of Callable Objects
    Illustrates callable objects: pointers to global functions, pointers to member functions, functors, and lambdas. Template Functors and Invokers, Function Objects CodeSnap, LambdaCapture CodeSnap
    These demos illustrate function objects - functions, function pointers, functors, and lambdas. Simple demos for all of the STL Containers
    Code for Array, Deque, List, Map, Native array, Queue, Set, SingleList, Stack, String, UnorderedMap, UnorderedSet, and Vector. STLContainers examples
    This code, linked on earlier slides, contains a simple example of the new Variadic Template functionality in the "inserter" project. Also includes functors, algorithms, and several other things you need to know. Smart Pointers with Reference Counting
    Smart pointers are useful tools, especially for holding onto heap resources indexted through an STL container. They are also a great illustration of many of the Template Techniques we've discussed in this course. Excellent C++ Q&A - Marshall Cline and Bjarne Stroustrup, Excellent Templates Q&A
    If you are only going to go one place to find information to supplement class texts and notes,then go here!
  6. 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
  7. 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.
      Please don't do that unless you are sure the the non-const function won't change the 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.
  8. Namespaces, pptx
    Basic information about how namespaces work. We will discuss why you want to use them.
  9. 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 Techniques
    Illustrates syntax and also provides examples of template specialization.
CST strip