Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Template Metaprogramming with C++

You're reading from   Template Metaprogramming with C++ Learn everything about C++ templates and unlock the power of template metaprogramming

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781803243450
Length 480 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Marius Bancila Marius Bancila
Author Profile Icon Marius Bancila
Marius Bancila
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Part 1: Core Template Concepts
2. Chapter 1: An Introduction to Templates FREE CHAPTER 3. Chapter 2: Template Fundamentals 4. Chapter 3: Variadic Templates 5. Part 2: Advanced Template Features
6. Chapter 4: Advanced Template Concepts 7. Chapter 5: Type Traits and Conditional Compilation 8. Chapter 6: Concepts and Constraints 9. Part 3: Applied Templates
10. Chapter 7: Patterns and Idioms 11. Chapter 8: Ranges and Algorithms 12. Chapter 9: The Ranges Library 13. Assignment Answers 14. Other Books You May Enjoy Appendix: Closing Notes

Understanding template terminology

So far in this chapter, we have used the general term templates. However, there are four different terms describing the kind of templates we have written:

  • Function template is the term used for a templated function. An example is the max template seen previously.
  • Class template is the term used for a templated class (which can be defined either with the class, struct, or union keyword). An example is the vector class we wrote in the previous section.
  • Variable template is the term used for templated variables, such as the NewLine template from the previous section.
  • Alias template is the term used for templated type aliases. We will see examples for alias templates in the next chapter.

Templates are parameterized with one or more parameters (in the examples we have seen so far, there was a single parameter). These are called template parameters and can be of three categories:

  • Type template parameters, such as in template<typename T>, where the parameter represents a type specified when the template is used.
  • Non-type template parameters, such as in template<size_t N> or template<auto n>, where each parameter must have a structural type, which includes integral types, floating-point types (as for C++20), pointer types, enumeration types, lvalue reference types, and others.
  • Template template parameters, such as in template<typename K, typename V, template<typename> typename C>, where the type of a parameter is another template.

Templates can be specialized by providing alternative implementations. These implementations can depend on the characteristics of the template parameters. The purpose of specialization is to enable optimizations or reduce code bloat. There are two forms of specialization:

  • Partial specialization: This is an alternative implementation provided for only some of the template parameters.
  • (Explicit) full specialization: This is a specialization of a template when all the template arguments are provided.

The process of generating code from a template by the compiler is called template instantiation. This happens by substituting the template arguments for the template parameters used in the definition of the template. For instance, in the example where we used vector<int>, the compiler substituted the int type in every place where T appeared.

Template instantiation can have two forms:

  • Implicit instantiation: This occurs when the compiler instantiates a template due to its use in code. This happens only for those combinations or arguments that are in use. For instance, if the compiler encounters the use of vector<int> and vector<double>, it will instantiate the vector class template for the types int and double and nothing more.
  • Explicit instantiation: This is a way to explicitly tell the compiler what instantiations of a template to create, even if those instantiations are not explicitly used in your code. This is useful, for instance, when creating library files, because uninstantiated templates are not put into object files. They also help reduce compile times and object sizes, in ways that we will see at a later time.

All the terms and topics mentioned in this section will be detailed in other chapters of the book. This section is intended as a short reference guide to template terminology. Keep in mind though that there are many other terms related to templates that will be introduced at the appropriate time.

You have been reading a chapter from
Template Metaprogramming with C++
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781803243450
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime