Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Template Metaprogramming with C++
Template Metaprogramming with C++

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

eBook
R$80 R$209.99
Paperback
R$261.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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.

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 : 9781803230535
Category :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 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
R$500 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 R$25 each
Feature tick icon Exclusive print discounts
R$800 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 R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 785.97
Modern CMake for C++
R$261.99
Template Metaprogramming with C++
R$261.99
C++20 STL Cookbook
R$261.99
Total R$ 785.97 Stars icon

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.