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

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

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

Review

In 2020 we had again the “Back to basics” track on CppCon. Jody Hagins took the topic of template metaprogramming, in particular type traits. This subject is quite important to understand because templates are more and more used to create application logic rather than to reuse code. Each new standard introduces a significant amount of new STL modules related to the type system. Almost any modern library relies heavily on type traits and exposes some of them to the user. In order to use them currently, we first need to understand them and know what they are used for.

Jody gave a wonderful talk, which was a gentle yet very concrete introduction to the type traits. From the first minutes, you can feel the positive energy around the speaker. It gives you confidence that the author is well prepared and in fact, this whole topic can be great fun. The first part of the 2 episode series is about explaining what is the difference between normal functions and metafunctions, what is a type trait and finally how does type traits look in the STL. There are plenty of micro examples, which Jody slowly examines and comment with personal advices. Both code and formal definitions are easy to follow.

This is the kind of talk I wish I had seen many years before. Jody introduces the audience to the world of the metafunctions, which can be found in almost any source code nowadays. This talk is strictly for beginners, but even experienced developers can benefit and treat this as a structured refresher. For example, I didn’t know that the standard specifies concrete requirements for the STL type traits. To sum, I recommend watching this talk as soon as you get comfortable with the template and typename keywords. This knowledge will benefit you a lot.

In this talk you will learn

  • What is metaprogramming in general?
  • What are the metafunctions and how they differ from normal functions?
  • What is a type trait?
  • What are some of the more popular use cases for type traits in STL?

Selected keynote slides

  • 0:25 – Intended audience:
    • beginner-oriented,
    • you have to know some C++,
    • rather a beginner to templates.
  • 2:38 – Mandatory talk to watch about metaprogramming: CppCon 2014: Walter E. Brown “Modern Template Metaprogramming: A Compendium” – Part I & Part II.
  • 3:15 – What to expect from the talk:
    • gentle introduction,
    • how to implement and use type traits,
    • exploration of the standard set of type traits,
    • common techniques used with type traits,
    • practical advices.
  • 4:12 – What is metaprogramming:
    • in general: when programs treat programs as data,
    • could be at compile time and at run time.
  • 6:55 – Why should we care about metaprogramming and type traits:
    • these techniques are quite old (type traits appeared in Boost in 2000),
    • each new standard adds more metaprogramming stuff.
  • 9:17 – Metafunctions:
    • how traditional functions differ from metafunctions in terms of arguments and return value specification,
    • metafunctions are not part of the language,
    • idiomatic use of existing language features,
    • their use is not dictated by the language by rather by convention.
  • 10:55 – Metafunction is a class:
    • zero or more template parameters,
    • zero or more return types and values,
    • by convention, it should only one thing.
  • 11:42 – How to return from a metafunction:
    • expose a public value named value,
    • expose a public type named type.
  • 12:35 – Value metafunctions:
    • example of a generic “identity” function vs metafunction,
    • simplified C++17 version of the example,
    • example of the “sum” function vs metafunction,
    • generic “sum” function.
  • 18:26 – Type metafunctions:
    • “returns” a type.
  • 19:12 – Calling metafunctions:
    • identifying parameters and return values at the call site,
    • typename dance: the reason why we sometimes have to use typename in our code outside template parameter list.
  • 21:18 – Convenience calling conventions:
    • value metafunctions use variable templates ending with _v,
    • type metafunctions use aliases ending with _t,
    • each variable/alias has to be explicitly written.
  • 25:16std::integral_constant:
    • possible implementation example.
  • 27:33std::bool_constant:
    • convenient helper,
    • std::true_type, std::false_type.
  • 28:32 – Standard type traits requirements:
    • Cpp17UnaryTypeTrait,
    • Cpp17BinaryTypeTrait,
    • Cpp17TransformationTypeTrait.
  • 31:15 – Undefined behavior:
    • do not specialize standard type traits,
    • be very careful when using incomplete types.
  • 33:44 – Specialization.
  • 33:58std::is_void (unary type trait):
    • example of specialization.
  • 42:31std::is_void: cv qualifiers:
    • cv qualifiers do not change the type trait values for Primary Types.
  • 45:03std::remove_const (transformation trait):
    • formal definition,
    • possible implementation,
    • example with explanation.
  • 53:37std::conditional:
    • like an if statement for types.
  • 56:27 – Questions.

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!