Modern C++ Template Programming [2018 class archive]

“Modern C++ Template Programming” is a two-day training course with programming exercises taught by Steve Dewhurst. It is offered at the Meydenbauer Conference Center from 9AM to 5PM on Saturday and Sunday, September 22nd and 23rd, 2018 (immediately prior to the conference). Lunch is included.

Course Description

All C++ programmers are template users, but many practicing professional C++ programmers have only a passing knowledge of the details of generic programming and meta-programming with C++ templates.  This course will bring attendees from template novices to template experts over two intense days.

Rather than simply review traditional C++ templates followed by discussion of newer C++11/14/17 template features, this course presents the fundamentals—including variadic, variable, and alias templates—as a unified whole before moving on to more advanced topics.

In addition to template mechanics, attendees will learn about traditional template programming idioms upgraded for use with modern C++, as well as emerging idioms and design techniques that make use of modern template language features.

We’ll also examine higher-level design techniques like Policy-Based Design, Expression Templates, Typelists, and template metaprogramming techniques involving recursive template specialization and constexpr function type calculation.

The presentation will illustrate techniques with realistic examples drawn from “Do What I Mean” interface design, embedded “bare metal” hardware programming, numeric programming, securities trading, and other areas.

This session includes optional programming exercises and solutions.  Any compliant C++11/14/17 environment will be sufficient, but some exercises may require a C++14 or C++17 environment.

Prerequisites

Attendees should be experienced software developers with basic knowledge of C++ templates.

Course Topics

  1. Class templates
    1. Parameters and arguments, specialization and instantiation (Heap container)
    2. Friends of class templates
    3. Class templates as friends
  2. Function templates
    1. Argument deduction
    2. Overloading and interaction with non-template functions
    3. SFINAE
    4. Default function template arguments
  3. Other templates
    1. Alias templates
    2. Variable templates
  4. Variadic templates
    1. Variadic class templates and template pack expansion (tuple)
    2. Variadic function templates and argument pack expansion (print)
    3. Simultaneous and nested pack expansion (forwarding idiom)
  5. Member templates
    1. Copy-like and move-like operations (smart pointer conversions)
    2. Variadic member templates and constructors (PlacedPtr)
    3. Emplacement (Heap)
    4. Generic lambda functions
  6. Template translation
    1. Two-phase translation
    2. Dependent names and the typename keyword
    3. Nested template names and the template keyword
    4. INCINI and flexible interface design (STL container comparisons, Heap comparison)
  7. Some traditional template programming gotchas and hacks
    1. Array passed as reference does not decay during argument deduction
    2. Failure to track changes in std::stack, etc.
    3. Dependent base classes
    4. Making names dependent for safety
    5. Making names dependent to avoid premature errors (static_assert for primary template)
    6. Exploiting two-phase translation to delay inlining (Chicken/Egg example with set)
  8. Class template specialization and partial specialization
    1. Using specialization to customize implementations (Heap container)
    2. Using specialization to ask compile-time questions (is_void, is_const, can_convert)
    3. The type_traits header file
    4. Constraint checking with static_assert
    5. Compile-time type manipulation (std::move implementation, recovering deduced type in generic lambda)
  9. Function template specialization
    1. Problems with function template specialization
    2. The Function Template Partial Specialization idiom
  10. The First/Rest idiom
    1. Recursive template specialization
    2. First/Rest idiom for variadic class templates
    3. Choosing recursion paths for correctness
    4. Avoiding exponential specialization (std::conditional counter-example)
    5. Dealing with long compile-time sequences (make_index_sequence)
    6. First/Rest for variadic function templates
    7. Using nested class templates for unwrap parameter packs
    8. Using forwarding functions to unwrap parameter packs

*** end of first day ***

  1. Alias templates revisited
    1. Adding and removing template parameters
    2. First/Rest with alias template intermediaries
    3. The rebind mechanism
    4. Simplifying complex compile-time predicates (STL iterator categories)
  2. Non-type template parameters
    1. First/Rest on index sequence (max_value)
    2. Literal operator templates (base 3 numeric literals)
    3. Function pointers as template arguments (numeric integration)
  3. Index parameter packs
    1. Selection with index pack expansion
    2. Using initializer lists to fix evaluation order
    3. Replacing recursive specialization with pack expansion
    4. Replacing recursive specialization with constexpr functions
  4. Template template parameters and policy-based design
    1. Template template parameters (Heap container implementation policy)
    2. Variadic template template parameter packs
    3. Variadic variadic template template parameter packs
    4. Applications of template sequences (tuple apply)
  5. SFINAE
    1. Mode of application of SFINAE in C++03 vs. Modern C++
    2. Enable_if
    3. Preprocessing overload resolution with SFINAE
    4. Designing DWIM class interfaces (Heap range initialization)
    5. Dealing with overloaded universal reference member functions
    6. Resizable component interfaces (transparent function objects)
    7. Other contexts for SFINAE application (enum containers)
  6. Type erasure (std::function and lambdas, Observer design pattern)
  7. The Barton-Nackman Trick
    1. Making New Friends idiom (rational number binary operators)
    2. CRTP (count, relational operators, variadic CRTP application)
  8. Type structures
    1. Calculating new types at compile time (SI physical units)
    2. Expression templates (matrix arithmetic)
    3. Typelists
    4. Conversion of typelist meta-algorithms to variadic template meta-algorithms (typelist set intersection)
    5. Template ASTs (complex compile-time type predicates)

Course Instructor

Steve DewhurstSteve Dewhurst is the co-founder and president of Semantics Consulting, Inc. Steve is the author of numerous technical articles on C++ programming techniques and compiler design, is the author of the critically acclaimed books C++ Common Knowledge and C++ Gotchas, and is the co-author of Programming in C++. He is a frequent speaker at industry conferences, a member of the advisory board for The C++ Source, was programming track chair for the Embedded Systems Conference (ESC), and was a visiting scientist at the Software Engineering Institute (SEI) of Carnegie Mellon University.  Steve has mentored and consulted with C++ projects ranging in size from 1 to over 100 developers, in areas ranging from compiler design to embedded telecommunications to ecommerce to derivative securities trading.

As a Member of Technical Staff in the UNIX Development Laboratory at AT&T Bell Laboratories, Steve worked with Bjarne Stroustrup, the designer and first implementer of C++, on the first public release of the language and cfront C++ compiler, then served as the lead designer and implementer of the first non-cfront C++ compiler.  As a compiler architect at Glockenspiel, Ltd., he designed and implemented a second C++ compiler.

Steve was a contributing editor for The C/C++ User’s Journal, a principal lecturer at The C++ Seminar, has served as a principal on the ANSI/ISO C++ standardization committee, was the C++ training series adviser for Technology Exchange Company (Addison-Wesley), was a member of the editorial board of and columnist for C++ Report, co-founder and member of the editorial board of The C++ Journal, and a Visiting Professor of Computer Science at Jackson State University.  He has also written C, COBOL, and Pascal compilers, was a principal on the ANSI/IEEE Pascal Standardization Committee, and a reviewer for ACM Computing Reviews.

Semantics Consulting, Inc. is located in the small New England town of Carver, Massachusetts. Like all small New England towns, Carver has a tradition of citizen involvement, and over the years Steve has been astonished to find himself harvesting cranberries, coaching the high school wrestling team, serving on the town water commission, finance committee, and website committee, and serving as campaign manager for a candidate for the Massachusetts state legislature.