Template Metaprogramming with C++
Learn everything about C++ templates and unlock the power of template metaprogramming
Marius Bancila
BIRMINGHAMMUMBAI
Template Metaprogramming with C++
Copyright 2022 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Associate Group Product Manager: Gebin George
Content Development Editor: Rosal Colaco
Technical Editor: Pradeep Sahu
Copy Editor: Safis Editing
Project Coordinator: Manisha Singh
Proofreader: Safis Editing
Indexer: Sejal Dsilva
Production Designer: Vijay Kamble
Business Development Executive: Kriti Sharma
Marketing Coordinator: Sonakshi Bubbar
First published: July 2022
Production reference: 1220722
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80324-345-0
www.packt.com
To the curious minds that always want to learn more.
Marius Bancila
Contributors
About the author
Marius Bancila is a software engineer with two decades of experience in developing solutions for line of business applications and more. He is the author of Modern C++ Programming Cookbook and The Modern C++ Challenge. He works as a software architect and is focused on Microsoft technologies, mainly developing desktop applications with C++ and C#. He is passionate about sharing his technical expertise with others and, for that reason, he has been recognized as a Microsoft MVP for C++ and later developer technologies since 2006. Marius lives in Romania and is active in various online communities.
About the reviewer
Aleksei Goriachikh has 8+ experience in C++ programming. After graduating from Novosibirsk State University in 2012 with a masters degree in mathematics, Aleksei has worked on research projects in the areas of computational mathematics and optimization, a geometric kernel for CAD systems, and a multi-threaded library for autonomous driving. Alekseis last professional interest is pre-silicon modelling.
Preface
The C++ programming language is one of the most widely used in the world and it has been so for decades. Its success isnt due just to the performance it provides or maybe to its ease of use, which many would argue against, but probably to its versatility. C++ is a general-purpose, multi-paradigm programming language that blends together procedural, functional, and generic programming.
Generic programming is a paradigm of writing code such as that entities such as functions and classes are written in terms of types that are specified later. These generic entities are instantiated only when needed for specific types that are specified as arguments. These generic entities are known as templates in C++.
Metaprogramming is the programming technique of using templates (and constexpr functions in C++) to generate code at compile-time that is then merged with the rest of the source code for compiling a final program. Metaprogramming implies that at least an input or an output is a type.