Generic Programming: from Strange to Normal

Instructor

Patrice Roy

Patrice Roy has been playing with C++ either professionally, for pleasure or (most of the time) both for almost 30 years. After a few years doing R&D and working on military flight simulators, he has moved on to academics and has been teaching computer science since 1998. Since 2005, he has been involved more specifically in helping graduate students and professionals from the fields of real-time systems and game programming develop the skills they need to face today’s challenges.

Patrice has been a participating member in the ISO C++ Standards Committee since late 2014 and has been involved with the ISO Programming Language Vulnerabilities working group since late 2015. He has five kids, and his wife ensures their house is home to a continuously changing number of cats, dogs and other animals.

Workshop

Generic programming has been both a scary aspect and a strength of C++ for decades now. Seen as something that is often complex to express yet easy to use, this programming paradigm pervades contemporary C++ programs, and many are those who use the standard library without noticing how much of it is made of generic types and algorithms.

This course proposes an exploration of generic programming through many lenses. From relatively simple use cases, we will move to expressing one’s own generic types, writing efficient and safe generic containers and algorithms, writing and using type traits, solving real problems through template metaprogramming (the reputedly scary part; we’ll make it fun), variadic templates, programming with constexpr, fold expressions, and getting acquainted with the beauty of concepts. In the end, we will write less code to get better results.

Target Audience

The target audience is intermediate-level developers who:

  • Are familiar with modern C++, meaning C++11 and more in this case, without necessarily being experts
  • Seek to develop an understanding, or a deeper understanding, of generic programming mechanisms made available to them in C++
  • Want the programs they write to be faster, safer and more correct

Junior developers can benefit from this course if they are willing to explore this part of the C++ language. Developers coming to C++ from another language might find the course surprisingly instructive, as they might gain a better understanding of the workings of other languages too – generic programming in C++ is quite different from generic programming in Java or C# programmers. More advanced developers might also enjoy this course if looking for a greater familiarity with modern C++ and its usefulness in their daily tasks. Please examine the Selected Approach and Expected contents sections below to ensure you make the right choice for your needs.

This course will concentrate on standard C++ and avoid most compiler-specific and operating system-specific techniques and tools.

Selected Approach

Since the class will involve problem-solving exercises, participants must bring their laptops (or equivalent development tool) and at least one recent C++ compiler of their choice; compilers that support at least C++ 17 (ideally C++ 20) are obviously preferable given the subject matter. As mentioned previously, we will strive to constrain ourselves to standard C++ only, and will be avoiding non-portable or platform-specific code as much as possible.

The format for this course will be a combination of lecture-style presentation of features and tricks and exercise solving. We will typically start with a concrete problem to solve, and a minimal objective to achieve, and work towards reaching that objective for some short time. A presentation of potential solutions will follow, accompanied by a discussion where participants’ input is encouraged.

Expected contents

Topics covered in this course include:

  • Humble beginnings
    • Simple examples
    • Templates are not macros
    • Anatomy of a template
    • Templates and implicit contracts
    • Traditional template parameters: types and integral constants
  • Type deduction
    • Words of power: auto, decltype, decltype(auto)
    • Types of references
    • Forwarding
    • Value categories
  • Generic classes
    • Writing a non-generic class (simple example)
    • Transforming a non-generic class into a generic class
    • Making a generic class safe: rethinking its public interface
    • Template specialization
  • Generic algorithms
    • From loops to algorithms, with care
    • Iterators
    • Function objects
    • From generic type to generic member function
    • Lambdas
    • The Law of Useful Returns
    • Numeric algorithms
  • Type traits
    • Evolution of type traits, from inception to standardization
    • Writing traits yourself
    • Standard traits
    • Documenting containers
    • Documenting iterators
    • When to use typename
    • When to use template
    • Traits aliases for types
  • Template metaprogramming
    • From constants to types
    • Non-type template parameters
    • A non-existing yet useful function named declval
    • Generic variables
    • Traits aliases for values
  • Using traits in practice
    • Implementing an average function
    • The close_enough case
    • Tag dispatching
    • enable_if and SFINAE
    • if constexpr
    • Introduction to concepts
    • The difference case
    • The distance case
    • Traits for optimization and safety: the copy case
    • Traits for optimization and convenience: the lexical_cast case
  • Concepts
    • Improving generic code through concepts
    • Turning generic programming into normal programming
    • Benefiting from common_type
    • Writing concepts
    • Using requires clauses
    • Why requires requires?
  • Variadic templates
    • Why and how they are useful
    • The reason for perfect forwarding
    • Classes with variadic bases
    • Composing lambdas
  • Fold expressions
    • What they are, how they work
    • Interesting applications
  • The C++ detection idiom
    • Detecting the validity of a specific type
    • Detecting the validity of a specific function
    • Detecting the validity of arbitrary expressions
    • Comparing with enable_if
    • Comparing with concepts
  • More on non-type template parameters
    • Interesting use cases
    • Floating point parameters
    • Literal type parameters
    • Compile-time strings
  • Metaprogramming for fun and profit
    • Type lists
    • Computing on constants
    • Benefiting from template template parameters
  • Introduction to ranges
    • A small overview of ranges for people familiar with algorithms

Dates

The workshop takes place on the weekend prior to the start of the conference (July 16 & July 17). Find out more details about fees and about the schedule here.

Where

Toronto

When

July 17-19, 2022

LinkedIn

CppNorth Group