Talk review: Template Metaprogramming: Type Traits (part 2 of 2)

Template Metaprogramming: Type Traits (part 2 of 2) – Jody Hagins – CppCon 2020

Template Metaprogramming: Type Traits (part 2 of 2) – Jody Hagins – CppCon 2020

Review

This talk is a second part of the type traits introduction by Jody Hagins during the CppCon 2020 “Back to basics” track. This time, we go deeper into the world of template metaprogramming and examine how are some of the standard type traits implemented. Among many shown cases, we can find type traits that are used to identify to which group does given type belongs to (e.g. integer, class, floating-point number) and also type traits that are used to convert one type into another (e.g. remove CV qualifiers). These features are usually used by many popular libraries, so it is good to understand them.

During the talk, Jody is showing how each presented type trait could be implemented. However, this requires knowledge about common metaprogramming techniques like template specialization and template partial specialization. Both of them are slowly described with the help of proper examples. Even if listeners ask to explain something again, Jody goes back and with his typical enthusiasm repeats given material with different words.

This part of the introduction is important because we can see concrete uses cases for the type traits along with their possible implementation. This gives us a solid foundation to compose them into more complex expressions and also to create custom ones. This talk and the first one are the must-watch videos, in order to understand type traits and template metaprogramming in general and allow further investigation with good results.

In this talk you will learn

  • What are the most useful standard type traits?
  • How several type traits are implemented?
  • What is template (partial) specialization?

Selected keynote slides

  • 0:37 – List of primary type categories (as type traits):
    • for any type T only one of them should return true.
  • 2:45std::is_void.
  • 3:22std::is_null_pointer.
  • 4:06std::is_floating_point:
    • yields true for float, double, long double,
    • requires 12 specializations.
  • 5:33std::is_integral:
    • 5 standard signed integer types,
    • implementation-defined extended signed integer types,
    • unsigned integer types,
    • char types,
    • bool,
    • requires 64 specializations.
  • 6:34 – Metafunctions abstractions:
    • example with regular function parameters,
    • removing CV-qualifiers before inspecting the type.
  • 10:14std::remove_cv:
    • std::remove_volatile,
    • formal definition,
    • example implementation.
  • 14:01 – Example with std::remove_cv<int const volatile>.
  • 16:06 – Comparing types:
    • std::is_same,
    • partial template specializations explained.
  • 21:41is_same_raw (not standard).
  • 22:18std::is_floating_point (redux).
  • 23:20std::is_integral (redux).
  • 24:47 – Q: How is “best match” formally defined in template specializations?
  • 26:51std::is_array:
    • contains both type and non-type template parameters (in specialization).
  • 31:56std::is_pointer.
  • 33:44std::is_union:
    • impossible to implement without compiler support.
  • 34:41std::is_class:
    • almost always implemented as compiler intrinsic,
    • without compiler assistance, impossible to distinguish between union and non-union class type.
  • 37:00is_class_or_union:
    • it has a valid syntax for representing a pointer to a member (even if it is an empty class/struct).
  • 39:47 – Function overload resolution:
    • decltype + declval,
    • tag dispatching,
    • SFINAE.
  • 50:00std::is_class.
  • 55:36 – Q: Explain how decltype/declval works?

Subscribe to get notified about new content

Thanks for taking your time to read it. If you like it or have other opinions, please share it in the comments. You can also subscribe to my newsletter to get latest news about my content. Happy coding and let’s stay connected!