“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 23rd and 24th (immediately prior to the conference). Lunch is included.
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.
Attendees should be experienced software developers with basic knowledge of C++ templates.
- Class templates
- Parameters and arguments, specialization and instantiation (Heap container)
- Friends of class templates
- Class templates as friends
- Function templates
- Argument deduction
- Overloading and interaction with non-template functions
- Default function template arguments
- Other templates
- Alias templates
- Variable templates
- Variadic templates
- Variadic class templates and template pack expansion (tuple)
- Variadic function templates and argument pack expansion (print)
- Simultaneous and nested pack expansion (forwarding idiom)
- Member templates
- Copy-like and move-like operations (smart pointer conversions)
- Variadic member templates and constructors (PlacedPtr)
- Emplacement (Heap)
- Generic lambda functions
- Template translation
- Two-phase translation
- Dependent names and the typename keyword
- Nested template names and the template keyword
- INCINI and flexible interface design (STL container comparisons, Heap comparison)
- Some traditional template programming gotchas and hacks
- Array passed as reference does not decay during argument deduction
- Failure to track changes in std::stack, etc.
- Dependent base classes
- Making names dependent for safety
- Making names dependent to avoid premature errors (static_assert for primary template)
- Exploiting two-phase translation to delay inlining (Chicken/Egg example with set)
- Class template specialization and partial specialization
- Using specialization to customize implementations (Heap container)
- Using specialization to ask compile-time questions (is_void, is_const, can_convert)
- The type_traits header file
- Constraint checking with static_assert
- Compile-time type manipulation (std::move implementation, recovering deduced type in generic lambda)
- Function template specialization
- Problems with function template specialization
- The Function Template Partial Specialization idiom
- The First/Rest idiom
- Recursive template specialization
- First/Rest idiom for variadic class templates
- Choosing recursion paths for correctness
- Avoiding exponential specialization (std::conditional counter-example)
- Dealing with long compile-time sequences (make_index_sequence)
- First/Rest for variadic function templates
- Using nested class templates for unwrap parameter packs
- Using forwarding functions to unwrap parameter packs
*** end of first day ***
- Alias templates revisited
- Adding and removing template parameters
- First/Rest with alias template intermediaries
- The rebind mechanism
- Simplifying complex compile-time predicates (STL iterator categories)
- Non-type template parameters
- First/Rest on index sequence (max_value)
- Literal operator templates (base 3 numeric literals)
- Function pointers as template arguments (numeric integration)
- Index parameter packs
- Selection with index pack expansion
- Using initializer lists to fix evaluation order
- Replacing recursive specialization with pack expansion
- Replacing recursive specialization with constexpr functions
- Template template parameters and policy-based design
- Template template parameters (Heap container implementation policy)
- Variadic template template parameter packs
- Variadic variadic template template parameter packs
- Applications of template sequences (tuple apply)
- Mode of application of SFINAE in C++03 vs. Modern C++
- Preprocessing overload resolution with SFINAE
- Designing DWIM class interfaces (Heap range initialization)
- Dealing with overloaded universal reference member functions
- Resizable component interfaces (transparent function objects)
- Other contexts for SFINAE application (enum containers)
- Type erasure (std::function and lambdas, Observer design pattern)
- The Barton-Nackman Trick
- Making New Friends idiom (rational number binary operators)
- CRTP (count, relational operators, variadic CRTP application)
- Type structures
- Calculating new types at compile time (SI physical units)
- Expression templates (matrix arithmetic)
- Conversion of typelist meta-algorithms to variadic template meta-algorithms (typelist set intersection)
- Template ASTs (complex compile-time type predicates)
Steve 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.