Modern C++ Template Common Knowledge [2019 Class Archive]

“Modern C++ Template Common Knowledge” is a two-day training course with programming exercises taught by Steve Dewhurst. It is offered at the Gaylord Rockies from 9AM to 5PM on Saturday and Sunday, September 14th and 15th, 2019 (immediately prior to the conference). Lunch is included.

Course Description

Nearly 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 help these attendees become technically-, idiomatically-, and socially-aware modern template programmers over two intense days.

Rather than simply review traditional C++ templates followed by discussion of newer C++11/14/17/20 template features, this course presents the fundamentals as a unified whole.
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.

The course integrates higher-level design techniques like Policy-Based Design, SFINAE, CRTP, and template metaprogramming techniques.

The presentation illustrates techniques with realistic examples drawn from “Do What I Mean” interface design, embedded “bare metal” hardware programming, container and smart pointer design, and other areas.

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


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

Course Topics

  1. Part 1
    What We Think We Know About Templates…But May Not
    • TBD…by you
    • Introducing the whole family: function, class, variable, alias, and lambda
    Arguments, Parameters, and Argument Deduction
    • Template arguments and parameters
    • Multiple template parameters
    • Default template arguments
    • C++14 variable templates
    • Template argument deduction
    • Non-deduced contexts
    • auto deduction vs. template argument deduction
    • Deduction and character string literals
    • Issues with argument-dependent lookup (ADL)
    • Conversions and the Making New Friends idiom
    • The Helper Function idiom
    • C++17 class template argument deduction and deduction guides
    • Alias templates and deduction
    Two-Phase Translation
    • Two-phase processing
    • Dependent and non-dependent names
    • Dependent base classes
    • Making a name dependent
    • Two-phase translation issues
    • INCINI (it’s not called, it’s not instantiated)
    • Using two-phase translation to deal with incomplete class declarations
    • Class template explicit specialization
    • Interface “morality”
    • Partial specialization
    • Variable template partial specialization
    • static_assert
    • Function template specialization, and issues
    • The Function Template Partial Specialization idiom
    Type Traits
    • Type traits: asking questions about types at compile time
    • Constraint checking in function and class templates
    • Variable templates for syntactic simplification
    • Alias declarations and alias templates
    • Alias templates for syntactic simplification
    • Code elimination without #if
    • Compile-time algorithm selection
    • Two-phase translation and tag dispatching
    • C++17 constexpr if and two-phase translation
    Template Template Parameters
    • Mechanics of template template parameters
    • Alias templates as template template parameters
    • Compile-time type manipulation
    • Implementation of std::move
    • Uninstantiating templates
    Policy-Based Design
    • Reification of design policies for the user of a component
    • Using template template parameters for policies
    • Default policies
  2. Part 2
    • Substitution failure is not an error (SFINAE)
    • Using enable_if to preprocess overload resolution
    • Constraints vs. selection with SFINAE
    • C++11 default function template arguments and SFINAE
    • Constraints on class template specializations
    • SFINAE for variable templates
    • Ad hoc SFINAE for implementing type traits
    • Expression SFINAE
    • C++20 concepts
    Universal References and Perfect Forwarding
    • Reference collapsing
    • Rvalue references and type deduction
    • Overloading on rvalues and lvalues
    • Emplacement
    • Forwarding references and perfect forwarding
    • std::forward vs. std::move
    • Idiomatic forwarding patterns
    • Using SFINAE to tame greedy universal references in overload resolution
    • Implementing std::forward
    Variadic Templates
    • Parameter packs and variadic templates
    • Parameter pack expansion
    • Variadic function templates
    • Multiple pack expansion
    • Perfect forwarding of pack expansion; dealing with combinatorial explosions
    • Non-type and template template parameter packs
    • sizeof…
    • Variadic variable templates
    • PBD revisited: variadic policies
    Bread and Butter Metaprogramming
    • First/Rest manipulation idiom
    • Constexpr and consteval functions and lambdas
    • Using class template specialization
    • Index sequences and index parameter packs
    • C++17 fold expressions; using fold expressions in place of First/Rest
    • Variadic function template implementation with First/Rest and pattern expansion
    • Common techniques for accessing parameter packs
    • Variable templates and fold expressions: perfect together
    Literal Operator Templates
    • Type sinks and user-defined literals
    • Mechanics of literal operator templates
    • Parsing the character pack
    • Metaprogramming pack indexing and subsetting
    Member Templates
    • Template members vs. member templates
    • C++14 generic lambdas
    • Dealing with argument type deduction in generic lambdas in C++14 and C++20
    • Transparent function objects in the STL
    • Copy-like and move-like operations as member templates
    • Variadic member templates
    • Emplacement
    • Using the template keyword to disambiguate member templates
    • Using member templates to access parameter pack contents
    • The Curiously Recurring Template pattern
    • Traditional CRTP implementation
    • The Barton-Nackman Trick
    • Variadic CRTP

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.