Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Template Metaprogramming with C++
Template Metaprogramming with C++

Template Metaprogramming with C++: Learn everything about C++ templates and unlock the power of template metaprogramming

Arrow left icon
Profile Icon Marius Bancila
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6 (14 Ratings)
Paperback Aug 2022 480 pages 1st Edition
eBook
NZ$14.99 NZ$54.99
Paperback
NZ$68.99
Subscription
Free Trial
Arrow left icon
Profile Icon Marius Bancila
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6 (14 Ratings)
Paperback Aug 2022 480 pages 1st Edition
eBook
NZ$14.99 NZ$54.99
Paperback
NZ$68.99
Subscription
Free Trial
eBook
NZ$14.99 NZ$54.99
Paperback
NZ$68.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Template Metaprogramming with C++

Chapter 1: An Introduction to Templates

As a C++ developer, you should be at least familiar if not well versed in template metaprogramming, in general, referred to in short as templates. Template metaprogramming is a programming technique that uses templates as blueprints for the compiler to generate code and help developers avoid writing repetitive code. Although general-purpose libraries use templates heavily, the syntax and the inner workings of templates in the C++ language can be discouraging. Even C++ Core Guidelines, which is a collection of dos and don'ts edited by Bjarne Stroustrup, the creator of the C++ language, and Herb Sutter, the chair of the C++ standardization committee, calls templates pretty horrendous.

This book is intended to shed light on this area of the C++ language and help you become prolific in template metaprogramming.

In this chapter, we will go through the following topics:

  • Understanding the need for templates
  • Writing your first templates
  • Understanding template terminology
  • A brief history of templates
  • The pros and cons of templates

The first step in learning how to use templates is to understand what problem they actually solve. Let's start with that.

Understanding the need for templates

Each language feature is designed to help with a problem or task that developers face when using that language. The purpose of templates is to help us avoid writing repetitive code that only differs slightly.

To exemplify this, let's take the classical example of a max function. Such a function takes two numerical arguments and returns the largest of the two. We can easily implement this as follows:

int max(int const a, int const b)
{
   return a > b ? a : b;
}

This works pretty well, but as you can see, it will only work for values of the type int (or those that are convertible to int). What if we need the same function but with arguments of the type double? Then, we can overload this function (create a function with the same name but a different number or type of arguments) for the double type:

double max(double const a, double const b)
{
   return a > b ? a : b;
}

However, int and double are not the only numeric types. There is char, short, long, long and their unsigned counterparts, unsigned char, unsigned short, unsigned long, and unsigned long. There are also the types float and long double. And other types, such as int8_t, int16_t, int32_t, and int64_t. And there could be other types that can be compared, such as bigint, Matrix, point2d, and any user-defined type that overloads operator>. How can a general-purpose library provide a general-purpose function such as max for all these types? It can overload the function for all the built-in types and perhaps other library types but cannot do so for any user-defined type.

An alternative to overloading functions with different parameters is to use void* to pass arguments of different types. Keep in mind this is a bad practice and the following example is shown only as a possible alternative in a world without templates. However, for the sake of discussion, we can design a sorting function that will run the quick sort algorithm on an array of elements of any possible type that provides a strict weak ordering. The details of the quicksort algorithm can be looked up online, such as on Wikipedia at https://en.wikipedia.org/wiki/Quicksort.

The quicksort algorithm needs to compare and swap any two elements. However, since we don't know their type, the implementation cannot do this directly. The solution is to rely on callbacks, which are functions passed as arguments that will be invoked when necessary. A possible implementation can be as follows:

using swap_fn = void(*)(void*, int const, int const);
using compare_fn = bool(*)(void*, int const, int const);
int partition(void* arr, int const low, int const high, 
              compare_fn fcomp, swap_fn fswap)
{
   int i = low - 1;
   for (int j = low; j <= high - 1; j++)
   {
      if (fcomp(arr, j, high))
      {
         i++;
         fswap(arr, i, j);
      }
   }
   fswap(arr, i + 1, high);
   return i + 1;
}
void quicksort(void* arr, int const low, int const high, 
               compare_fn fcomp, swap_fn fswap)
{
   if (low < high)
   {
      int const pi = partition(arr, low, high, fcomp, 
         fswap);
      quicksort(arr, low, pi - 1, fcomp, fswap);
      quicksort(arr, pi + 1, high, fcomp, fswap);
   }
}

In order to invoke the quicksort function, we need to provide implementations for these comparisons and swapping functions for each type of array that we pass to the function. The following are implementations for the int type:

void swap_int(void* arr, int const i, int const j)
{
   int* iarr = (int*)arr;
   int t = iarr[i];
   iarr[i] = iarr[j];
   iarr[j] = t;
}
bool less_int(void* arr, int const i, int const j)
{
   int* iarr = (int*)arr;
   return iarr[i] <= iarr[j];
}

With all these defined, we can write code that sorts arrays of integers as follows:

int main()
{
   int arr[] = { 13, 1, 8, 3, 5, 2, 1 };
   int n = sizeof(arr) / sizeof(arr[0]);
   quicksort(arr, 0, n - 1, less_int, swap_int);
}

These examples focused on functions but the same problem applies to classes. Consider that you want to write a class that models a collection of numerical values that has variable size and stores the elements contiguously in memory. You could provide the following implementation (only the declaration is sketched here) for storing integers:

struct int_vector
{
   int_vector();
   size_t size() const;
   size_t capacity() const;
   bool empty() const;
   void clear();
   void resize(size_t const size);
   void push_back(int value);
   void pop_back();
   int at(size_t const index) const;
   int operator[](size_t const index) const;
private:
   int* data_;
   size_t size_;
   size_t capacity_;
};

This all looks good but the moment you need to store values of the type double, or std::string, or any user-defined type you'll have to write the same code, each time only changing the type of the elements. This is something nobody wants to do because it is repetitive work and because when something needs to change (such as adding a new feature or fixing a bug) you need to apply the same change in multiple places.

Lastly, a similar problem can be encountered, although less often, when you need to define variables. Let's consider the case of a variable that holds the new line character. You can declare it as follows:

constexpr char NewLine = '\n';

What if you need the same constant but for a different encoding, such as wide string literals, UTF-8, and so on? You can have multiple variables, having different names, such as in the following example:

constexpr wchar_t NewLineW = L'\n';
constexpr char8_t NewLineU8 = u8'\n';
constexpr char16_t NewLineU16 = u'\n';
constexpr char32_t NewLineU32 = U'\n';

Templates are a technique that allows developers to write blueprints that enable the compiler to generate all this repetitive code for us. In the following section, we will see how to transform the preceding snippets into C++ templates.

Writing your first templates

It is now time to see how templates are written in the C++ language. In this section, we will start with three simple examples, one for each of the snippets presented earlier.

A template version of the max function discussed previously would look as follows:

template <typename T>
T max(T const a, T const b)
{
   return a > b ? a : b;
}

You will notice here that the type name (such as int or double) has been replaced with T (which stands for type). T is called a type template parameter and is introduced with the syntax template<typename T> or typename<class T>. Keep in mind that T is a parameter, therefore it can have any name. We will learn more about template parameters in the next chapter.

At this point, this template that you put in the source code is only a blueprint. The compiler will generate code from it based on its use. More precisely, it will instantiate a function overload for each type the template is used with. Here is an example:

struct foo{};
int main()
{   
   foo f1, f2;
   max(1, 2);     // OK, compares ints
   max(1.0, 2.0); // OK, compares doubles
   max(f1, f2);   // Error, operator> not overloaded for 
                  // foo
}

In this snippet, we are first calling max with two integers, which is OK because operator> is available for the type int. This will generate an overload int max(int const a, int const b). Second, we are calling max with two doubles, which again is all right since operator> works for doubles. Therefore, the compiler will generate another overload, double max(double const a, double const b). However, the third call to max will generate a compiler error, because the foo type does not have the operator> overloaded.

Without getting into too many details at this point, it should be mentioned that the complete syntax for calling the max function is the following:

max<int>(1, 2);
max<double>(1.0, 2.0);
max<foo>(f1, f2);

The compiler is able to deduce the type of the template parameter, making it redundant to write it. There are cases, however, when that is not possible; in those situations, you need to specify the type explicitly, using this syntax.

The second example involving functions from the previous section, Understanding the need for templates, was the quicksort() implementation that dealt with void* arguments. The implementation can be easily transformed into a template version with very few changes. This is shown in the following snippet:

template <typename T>
void swap(T* a, T* b)
{
   T t = *a;
   *a = *b;
   *b = t;
}
template <typename T>
int partition(T arr[], int const low, int const high)
{
   T pivot = arr[high];
   int i = (low - 1);
   for (int j = low; j <= high - 1; j++)
   {
      if (arr[j] < pivot)
      {
         i++;
         swap(&arr[i], &arr[j]);
      }
   }
   swap(&arr[i + 1], &arr[high]);
   return i + 1;
}
template <typename T>
void quicksort(T arr[], int const low, int const high)
{
   if (low < high)
   {
      int const pi = partition(arr, low, high);
      quicksort(arr, low, pi - 1);
      quicksort(arr, pi + 1, high);
   }
}

The use of the quicksort function template is very similar to what we have seen earlier, except there is no need to pass pointers to callback functions:

int main()
{
   int arr[] = { 13, 1, 8, 3, 5, 2, 1 };
   int n = sizeof(arr) / sizeof(arr[0]);
   quicksort(arr, 0, n - 1);
}

The third example we looked at in the previous section was the vector class. A template version of it will look as follows:

template <typename T>
struct vector
{
   vector();
   size_t size() const;
   size_t capacity() const;
   bool empty() const;
   void clear();
   void resize(size_t const size);
   void push_back(T value);
   void pop_back();
   T at(size_t const index) const;
   T operator[](size_t const index) const;
private:
   T* data_;
   size_t size_;
   size_t capacity_;
};

As in the case of the max function, the changes are minimal. There is the template declaration on the line above the class and the type int of the elements has been replaced with the type template parameter T. This implementation can be used as follows:

int main()
{   
   vector<int> v;
   v.push_back(1);
   v.push_back(2);
}

One thing to notice here is that we have to specify the type of the elements when declaring the variable v, which is int in our snippet because the compiler would not be able to infer their type otherwise. There are cases when this is possible, in C++17, and this topic, called class template argument deduction, will be discussed in Chapter 4, Advanced Template Concepts.

The fourth and last example concerned the declaration of several variables when only the type was different. We could replace all those variables with a template, as shown in the following snippet:

template<typename T>
constexpr T NewLine = T('\n');

This template can be used as follows:

int main()
{
   std::wstring test = L"demo";
   test += NewLine<wchar_t>;
   std::wcout << test;
}

The examples in this section show that the syntax for declaring and using templates is the same whether they represent functions, classes, or variables. This leads us to the next section where we will discuss the types of templates and template terminology.

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.

A brief history of templates

Template metaprogramming is the C++ implementation of generic programming. This paradigm was first explored in the 1970s and the first major languages to support it were Ada and Eiffel in the first half of the 1980s. David Musser and Alexander Stepanov defined generic programming, in a paper called Generic Programming, in 1989, as follows:

Generic programming centers around the idea of abstracting from concrete, efficient algorithms to obtain generic algorithms that can be combined with different data representations to produce a wide variety of useful software.

This defines a paradigm of programming where algorithms are defined in terms of types that are specified later and instantiated based on their use.

Templates were not part of the initial C with Classes language developed by Bjarne Stroustrup. Stroustrup's first papers describing templates in C++ appeared in 1986, one year after the publication of his book, The C++ Programming Language, First Edition. Templates became available in the C++ language in 1990, before the ANSI and ISO C++ committees for standardization were founded.

In the early 1990s, Alexander Stepanov, David Musser, and Meng Lee experimented with the implementation in C++ of various generic concepts. This led to the first implementation of the Standard Template Library (STL). When the ANSI/ISO committee became aware of the library in 1994, it quickly added it to the drafted specifications. STL was standardized along with the C++ language in 1998 in what became known as C++98.

Newer versions of the C++ standard, collectively referred to as modern C++, have introduced various improvements to template metaprogramming. The following table lists them briefly:

Table 1.1

Table 1.1

All these features, along with other aspects of template metaprogramming, will make the sole subject of this book and will be presented in detail in the following chapters. For now, let's see what the advantages and disadvantages are of using templates.

The pros and cons of templates

Before you start using templates, it's important to understand the benefits of using them as well as the disadvantages they may incur.

Let's start by pointing out the advantages:

  • Templates help us avoid writing repetitive code.
  • Templates foster the creation of generic libraries providing algorithms and types, such as the standard C++ library (sometimes incorrectly referred to as the STL), which can be used in many applications, regardless of their type.
  • The use of templates can result in less and better code. For instance, using algorithms from the standard library can help write less code that is likely easier to understand and maintain and also probably more robust because of the effort put into the development and testing of these algorithms.

When it comes to disadvantages, the following are worth mentioning:

  • The syntax is considered complex and cumbersome, although with a little practice this should not really pose a real hurdle in the development and use of templates.
  • Compiler errors related to template code can often be long and cryptic, making it very hard to identify their cause. Newer versions of the C++ compilers have made progress in simplifying these kinds of errors, although they generally remain an important issue. The inclusion of concepts in the C++20 standard has been seen as an attempt, among others, to help provide better diagnostics for compiling errors.
  • They increase the compilation times because they are implemented entirely in headers. Whenever a change to a template is made, all the translation units in which that header is included must be recompiled.
  • Template libraries are provided as a collection of one or more headers that must be compiled together with the code that uses them.
  • Another disadvantage that results from the implementation of templates in headers is that there is no information hiding. The entire template code is available in headers for anyone to read. Library developers often resort to the use of namespaces with names such as detail or details to contain code that is supposed to be internal for a library and should not be called directly by those using the library.
  • They could be harder to validate since code that is not used is not instantiated by the compiler. It is, therefore, important that when writing unit tests, good code coverage must be ensured. This is especially the case for libraries.

Although the list of disadvantages may seem longer, the use of templates is not a bad thing or something to be avoided. On the contrary, templates are a powerful feature of the C++ language. Templates are not always properly understood and sometimes are misused or overused. However, the judicious use of templates has unquestionable advantages. This book will try to provide a better understanding of templates and their use.

Summary

This chapter introduced the concept of templates in the C++ programming language.

We started by learning about the problems for which the solution is the use of templates. We then saw how templates look with simple examples of function templates, class templates, and variable templates. We introduced the basic terminology for templates, which we will discuss more in the forthcoming chapters. Toward the end of the chapter, we saw a brief history of templates in the C++ programming language. We ended the chapter with a discussion on the advantages and disadvantages of using templates. All these topics will lead us to understand the next chapters better.

In the next chapter, we will explore the fundamentals of templates in C++.

Questions

  1. Why do we need templates? What advantages do they provide?
  2. How do you call a function that is a template? What about a class that is a template?
  3. How many kinds of template parameters exist and what are they?
  4. What is partial specialization? What about full specialization?
  5. What are the main disadvantages of using templates?

Further reading

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Grasp the fundamentals of and learn to write effective C++ templates
  • Get up to speed with the latest C++20 template features such as constraints and concepts
  • Explore different patterns and idioms to integrate templates in your program design

Description

Learn how the metaprogramming technique enables you to create data structures and functions that allow computation to happen at compile time. With this book, you'll realize how templates help you avoid writing duplicate code and are key to creating generic libraries, such as the standard library or Boost, that can be used in a multitude of programs. The introductory chapters of this book will give you insights into the fundamentals of templates and metaprogramming. You'll then move on to practice writing complex templates and exploring advanced concepts such as template recursion, template argument deduction, forwarding references, type traits, and conditional compilation. Along the way, you'll learn how to write variadic templates and how to provide requirements to the template arguments with C++20 constraints and concepts. Finally, you'll apply your knowledge of C++ metaprogramming templates to implement various metaprogramming patterns and techniques. By the end of this book, you'll have learned how to write effective templates and implement metaprogramming in your everyday programming journey.

Who is this book for?

This book is for beginner-to-intermediate C++ developers who want to learn about template metaprogramming as well as advanced C++ developers looking to get up to speed with the new C++20 features related to templates and the the various idioms and patterns. Basic C++ coding experience is necessary to get started with this book.

What you will learn

  • Understand the syntax for all types of templates
  • Discover how specialization and instantiation works
  • Get to grips with template argument deduction and forwarding references
  • Write variadic templates with ease
  • Become familiar with type traits and conditional compilation
  • Restrict template arguments in C++20 with constraints and concepts
  • Implement patterns such as CRTP, mixins, and tag dispatching

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 19, 2022
Length: 480 pages
Edition : 1st
Language : English
ISBN-13 : 9781803243450
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 19, 2022
Length: 480 pages
Edition : 1st
Language : English
ISBN-13 : 9781803243450
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 206.97
C++20 STL Cookbook
NZ$68.99
Template Metaprogramming with C++
NZ$68.99
Modern CMake for C++
NZ$68.99
Total NZ$ 206.97 Stars icon
Banner background image

Table of Contents

14 Chapters
Part 1: Core Template Concepts Chevron down icon Chevron up icon
Chapter 1: An Introduction to Templates Chevron down icon Chevron up icon
Chapter 2: Template Fundamentals Chevron down icon Chevron up icon
Chapter 3: Variadic Templates Chevron down icon Chevron up icon
Part 2: Advanced Template Features Chevron down icon Chevron up icon
Chapter 4: Advanced Template Concepts Chevron down icon Chevron up icon
Chapter 5: Type Traits and Conditional Compilation Chevron down icon Chevron up icon
Chapter 6: Concepts and Constraints Chevron down icon Chevron up icon
Part 3: Applied Templates Chevron down icon Chevron up icon
Chapter 7: Patterns and Idioms Chevron down icon Chevron up icon
Chapter 8: Ranges and Algorithms Chevron down icon Chevron up icon
Chapter 9: The Ranges Library Chevron down icon Chevron up icon
Assignment Answers Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6
(14 Ratings)
5 star 78.6%
4 star 7.1%
3 star 14.3%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Christian Albaret Feb 16, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This the first book I am reading of the 5 I bought at once. I have been developping a lot in C++, much on and with templates, and some metaprogramming. The book goes through the features, tricks, and pitfalls of C++ templates, with many explanations., and then demonstrates some programming patterns. Much can readily be found in reference documentation, but they are hard to read, and one easky gets lost. The discussions in the book are what makes the difference.
Feefo Verified review Feefo
Vine Reviewer Nov 15, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Since I've taught C++ courses, and have used templates extensively, I figured this book would be an easy read.Well parts of it were a fairly easy read -- but the author dived a little deeper than I expected into some of the details. For instance, I knew about type traits, but I didn't know how they were implemented. The author showed the background theory, and did a good job of explaining how/why they are used.I learned quite a bit about C++20 features, too. The author did an in-depth coverage of constraints and concepts, new features I had heard about, but never used. The main take-away I got from those topics was that those new features yielded *much* clearer error messages. If you have ever struggled with VS2017 compiler error messages, you will *love* constraints and concepts.The patterns & idioms section was a slow read, because it had a lot of new (to me) information.The last parts on ranges and algorithms, was a bit easier reading, since I've made extensive use of most of the material there.I was very impressed with Marius Bancila's writing style, although there were a few word choices that hinted that English was not his native language. But his English is excellent, as is his ability to teach fairly complex topics well. About the only thing I would have suggested (if I had been an editor) is the simple way to approach templates as a novice. Namely, write and test your class (or function) as a non-template, using a specific type, then convert it to a templated version. The reverse is also true; because template syntax is ugly (IMNSHO), the easiest way to understand a template written by somebody else is to copy it, and substitute some simple type for the template parameter(s) to get rid of the ugly template syntax.But that's not enough of a shortcoming to knock any points off. In fact, I think this book is more than just "good" -- it will be an important part of my reference library.Disclaimer: I was provided a copy of this book by Packt for review. This review is my honest opinion of the book.
Amazon Verified review Amazon
Y. Arazi Sep 13, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Another great book from Marius. Great information about the modern and advanced templates and c++. Highly recommended.
Amazon Verified review Amazon
Krishnan Raghavan Nov 30, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have always wanted to understand how templates are written in C++ for a very long time and was very excited when I saw this book. Confession: I am yet to read the whole book but I thought I would write a small review of the few initial chapters that I have read in case it helps anyone.Metaprogramming is the programming technique of using templates to generate code at compile-time that is then merged with the rest of the source code for compiling a final program. This book is divided into 3 parts. The first two parts will give the user a good understanding of all the different kinds of templates available in C++. The third part will help the reader put their knowledge into practice to perform metaprogramming with templates.The author has ensured that this book caters to all levels of C++ programmers (Beginners, Intermediate and Advanced). At the end of each chapter, there are questions that will ensure that the concepts learned in the chapter are tested. Please do not skip these. Further reading will give the reader about what to read next to ensure that the chapter lessons as well understood.I will post a detailed chapter-wise review in the next few days.A big shout out to Marius Bancila for writing a book a such a difficult topic.
Amazon Verified review Amazon
Adam Rackis Jan 02, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I’ve read a lot of C++ books, but this was one of my favorites. It’s a solid deep dive into C++ templates. It starts slowly enough for non-experts to keep up, and then covers some solid material, including new C++20 content.Highly recommend.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.