C C T B H P N

Lecture #26 - Template Metaprogramming

Compile-time computation

"There cannot be a stressful crisis next week. My schedule is already full."
Henry Kissenger, former diplomat

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:

In this lecture we begin a discussion of template metaprogramming.
  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. Projects:

    Project #3 - Code Publisher Client Project3HelpS2019 - includes:
    Client-Channel-Server Diagram
    Diagram showing use ofC++\CLI translater between WPF (C#) GUI and C++ Comm Channel. Prototype GUI for Project #3
    Demonstrating Requirements with a GUI
    The projects, below, are intended to help with Project #4. They are included so we can peek ahead to see where we need to go with Project #4. Project4Help WPF
    Illustrates techniques used for WPF to use Comm via C++\CLI. Prototype GUI for Project #3
    Uses C++\CLI translator to interoperate with C++ Comm to navigate a remote directory tree.
    Project #4 - Remote Code Page Management Project4HelpS2019 - includes: Client-Channel-Server Diagram
    Diagram showing use ofC++\CLI translater between WPF (C#) GUI and C++ Comm Channel. Interop Help Demo class diagram, Interop Help Demo code
    Shows how to implement interoperation between C# and C++ as simply as practical. Creating C++\CLI Projects, pptx
    Mock Channel block diagram, Mock Channel
    Illustrates how to use .Net threads in a managed layer and C++ threads in a native layer. Communication Channel Structure
    Diagrams that show how asynchronous message passing communication works. Communication Channel Class Diagram
    Class diagram of our protype communication channel. Communication Channel Activities
    Activity diagram showing message flow through the communcation channel. CppCommWithFileXfer - Communication Channel Code
    Here's code you can use to implement message-passing communication. Project4HelpWPF code demo
    A getting-started GUI prototype that shows one way to build a client for Project #4. C++ Repository with WPF GUI - code demo
    This demo illustrates use of User Controls as well as remote directory navigation.
  3. Unit Test Framework
  4. Preliminaries:

    Program Scope and global data
    Scope inclusion rules
    Programming Languages, vsdx
    History of imperative, functional, and scripting langugages.
  5. Template Metaprogramming

    Template metaprogramming is the process of writing code for its compile-time effects in addition to its run-time behavior. Template metaprograms run at compile-time to produce often interesting template functions and classes. We will look at some examples today. Template metaprograms are often written for one of these reasons:
    • perform computation at compile-time instead of during execution.
    • introduce functionality that could not be achieved by run-time programs alone.
    • provide useful constructs for other (meta)programs, e.g., building metaprogramming tools.
    Note:
    I've built all these examples with Visual Studio 2019, a few with the Properties > C/C++ > Language > C++ Language Standard > ISO C++ 2017 Standard.
    Template Metaprogramming
    Template Preliminaries:
    Template Members
    Typename qualifier
    Non-Template Template Parameter
    Template Template Parameter
    Partial Template Specialization
    Motivation for Partial Template Specialization
    The beginnings:
    Curiously Recurring Template Pattern (CRTP)
    Invoker - almost a delegate
    Test Types
    Coming soon: optional<T>
    What Template Metaprogramming can do:
    Binary Conversion at compile time
    compile time functions
    compile time loops
    compile time selection
    compile time switch case
    compile time type lists
    compile time lists improved
    Variadic Templates Variatic templates produce functions that can take an unspecified finite number of arguments and template classes that accept an unspecified finite number of template arguments. These demo programs try to show why that could be useful.
  6. The D Programming Language:

  7. Open Source Libraries:

  8. C++11 - new features

    Herb Sutter's excellent and brief overview from his Blog
    C++11 concurrency
    Threads, locks, condition variables, async, futures, promises, packaged_tasks, and atomics
    C++11 BlockingQueue
    Uses condition variable and mutex to build a queue that blocks deQers when empty.
    Str strings
    Demonstrates move constructor and move assignment.
    Component Reference Counting
    Demonstrates variadic templates and functions.
    Symbol Table
    Illustrates how using aliases are a great design tool. Also demonstrates range-based for loops.
    Chrono Timer
    Uses Chrono library to build a high resolution timer precise to about 100 nanoseconds.
    Callable Objects
    Demonstrates lambdas and compares them to function pointers and functors.
    Singleton
    Uses atomic.
    Uniform Initialization
    Illustrates use of uniform initialization for user defined types and STL containers.
  9. C++11 - The new standard:

    C++ Reference - includes C++11
    What is C++11?
    A guide to Lambdas
    Local, anonymous function definition
    Move semantics and rvalue references
    Pass by value and return by value doesn't need to make copies
    make_shared
    Don't need delete if you use make_shared smart pointer.
    Type Inference
    Type declaration using auto that adopts the type of an expression result.
    Range-based for loops
    C++11 Thread support
    std::Thread documentation.
    C++ 11 std::async
    Clean semantics for calling a function asynchronously.
  10. Other C++11 References:

  11. C++11 Videos and Blogs:

    Herb Sutter on C++11
    Outstanding presentation on the things you can do better with C++11.
    C++ and Beyond
    Herb Sutter's talk on lambdas
    Herb Sutter's C++ and Beyond 2011 talk
    Herb Sutter's Effective Concurrency series
    Herb Sutter on Concurrency
    Herb Sutter's talk at PDC05 - skip the animations install
    This is an outstanding discussion of new features planned for C++ wrapped in an amazing discussion of concurrency.
    Slides from Herb Sutter's talk at PDC05
  12. Advanced C++ Texts

  13. Thinking about Software Design:

    Software Design Blog
    Summary of fundamental ideas. Object Oriented Design Blog
    Essential features of Object Oriented Design. Software Design Principles
    SRP, LSP, OCP, DIP, and ISP Software Design, ppt
    Broad survey of ideas with examples Design Strategies, pptx
    Summarizes design issues for modules and classes. Design Guidelines, pptx
    Design rules and idioms that transcend Object Oriented Design.
  14. Common C++ Problems, pptx
  15. Fragile Designs, pptx
CST strip