Template Metaprogramming with C++

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Understand how to use modern C++ templates for writing maintainable, robust, and fast software Key Features 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 Book 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. 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 Who this book is 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.

Author(s): Marius Bancila
Edition: 1
Publisher: Packt Publishing
Year: 2022

Language: English
Pages: 480
Tags: c++20 c++23 template

Template Metaprogramming with C++
Contributors
About the author
About the reviewer
Preface
Who this book is for?
What this book covers
To get the most out of this book
Mentions and further readings
Download the example code files
Download the color images
Conventions used
Get in touch
Share Your Thoughts
Part 1: Core Template Concepts
Chapter 1: An Introduction to Templates
Understanding the need for templates
Writing your first templates
Understanding template terminology
A brief history of templates
The pros and cons of templates
Summary
Questions
Further reading
Chapter 2: Template Fundamentals
Defining function templates
Defining class templates
Defining member function templates
Understanding template parameters
Type template parameters
Non-type template parameters
Template template parameters
Default template arguments
Understanding template instantiation
Implicit instantiation
Explicit instantiation
Understanding template specialization
Explicit specialization
Partial specialization
Defining variable templates
Defining alias templates
Exploring generic lambdas and lambda templates
Summary
Questions
Further reading
Chapter 3: Variadic Templates
Understanding the need for variadic templates
Variadic function templates
Parameter packs
Understanding parameter packs expansion
Variadic class templates
Fold expressions
Variadic alias templates
Variadic variable templates
Summary
Questions
Further reading
Part 2: Advanced Template Features
Chapter 4: Advanced Template Concepts
Understanding name binding and dependent names
Two-phase name lookup
Dependent type names
Dependent template names
Current instantiation
Exploring template recursion
Function template argument deduction
Class template argument deduction
Forwarding references
The decltype specifier
The std::declval type operator
Understanding friendship in templates
Summary
Questions
Further readings
Chapter 5: Type Traits and Conditional Compilation
Understanding and defining type traits
Exploring SFINAE and its purpose
Enabling SFINAE with the enable_if type trait
Using constexpr if
Exploring the standard type traits
Querying the type category
Querying type properties
Querying supported operations
Querying type relationships
Modifying cv-specifiers, references, pointers, or a sign
Miscellaneous transformations
Seeing real-world examples of using type traits
Implementing a copy algorithm
Building a homogenous variadic function template
Summary
Questions
Further reading
Chapter 6: Concepts and Constraints
Understanding the need for concepts
Defining concepts
Exploring requires expressions
Simple requirements
Type requirements
Compound requirements
Nested requirements
Composing constraints
Learning about the ordering of templates with constraints
Constraining non-template member functions
Constraining class templates
Constraining variable templates and template aliases
Learning more ways to specify constraints
Using concepts to constrain auto parameters
Exploring the standard concepts library
Summary
Questions
Further reading
Part 3: Applied Templates
Chapter 7: Patterns and Idioms
Dynamic versus static polymorphism
The Curiously Recurring Template Pattern
Limiting the object count with CRTP
Adding functionality with CRTP
Implementing the composite design pattern
The CRTP in the standard library
Mixins
Type erasure
Tag dispatching
Alternatives to tag dispatching
Expression templates
Using ranges as an alternative to expression templates
Typelists
Using typelists
Implementing operations on typelists
Summary
Questions
Further reading
Chapter 8: Ranges and Algorithms
Understanding the design of containers, iterators, and algorithms
Creating a custom container and iterator
Implementing a circular buffer container
Implementing an iterator type for the circular buffer container
Writing a custom general-purpose algorithm
Summary
Questions
Chapter 9: The Ranges Library
Advancing from abstract ranges to the ranges library
Understanding range concepts and views
Exploring more examples
Understanding the constrained algorithms
Writing your own range adaptor
Summary
Questions
Further reading
Appendix
Closing Notes
Assignment Answers
Chapter 1, Introduction to Templates
Chapter 2, Template Fundamentals
Chapter 3, Variadic Templates
Chapter 4, Advanced Template Concepts
Chapter 5, Type Traits and Conditional Compilation
Chapter 6, Concepts and Constraints
Chapter 7, Patterns and Idioms
Chapter 8, Ranges and Algorithms
Chapter 9, The Ranges Library
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share Your Thoughts