C C T B H P N

Lecture #7 - Template Policies and Traits

Pluggable behaviors

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 template policies and template traits. We will continue discussing the HashTable example.
  1. Readings for Lecture #7

    Text: Study chapter 16 carefully
    Project #1
    Templates, Templates Presentation, ppt
  2. 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.
  3. Syllabus
    Describes topics to be covered, reading material for you to digest before coming to class, and due dates for the assigned projects.
  4. We are using Piazza for Q&A help. You will find the Piazza site here piazza.com.
  5. Projects:

    Resources for Project #2: - Source code publisher for projects
    Project #2 statement, Project2HelpS2019
    OODProject2Helper adds show/hide capability to the demo output. You will find the Parser Blog helpful for Project #2. C++ Survival Guide, doc
    Visual Studio help notes, Code Analyzer
    Project Submissions

    Current Versions of Code:

    If you go to other folders in the college server you are very likely to download out-of-date code, usually correct, but won't compile with demonstration code that uses newer features.
    Policy for due date extensions - only for certified illness and work or interview trips out of town:
    one week extension, with proof. Only one extension per project.
  6. 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. 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!
  7. Hashtables

    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 Containers with Iterators, pptx
    Look at HashTable<key,value,Hash> and HashIterator<key,value,Hash> classes. Draw some fairly general conclusions.
    Template Policies and Traits, pptx, Important
    Discusses policies and traits in the context of our HashTable project.
  8. Dynamic Link Libraries

    DllDemo Protocol, Another Dll demo
  9. 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.
  10. const pointers
  11. Inheritance Demo code, Inheritance Demo Sharing, vsdx
  12. Software design presentations:

    Software Design Goals
    Design Goals and Attributes - used to measure designs.

    Design Strategies
    Discusses styles of design and binding.

    Design Guidelines
    Small principles and useful techniques

    Design Issues
    Practical issues and problems of design and implementation

  13. Namespaces:

    Namespaces
    Basic information about how namespaces work. We will discuss why you want to use them.

    Composite Namespaces
    Demonstrates nested, sequenced, and extended namespaces.

  14. These are the most important items from today's lecture:

    Template Policies and Traits - Important
    Discusses policies and traits in the context of our HashTable project.

    Template Techniques - Important
    Template members, template template parameters, partial template specialization and more...

    STL Containers
    Demonstrates almost all of the STL Containers with simple example code.

    Functor examples
    Functors are classes that define operator() - their syntax is similar to functions, but they are more powerful. The STL uses functors to support many of the algorithms defined on containers.
CST strip