12 C++ conceptual art: Templates and concepts
This chapter covers
- Diving deep into the concept of template
- Understanding template specialization
- Exploring templates and the concept of metaprogramming
- Unraveling the C++20 concepts and constraints
In this chapter, we will explore the powerful features that allow you to write generic code and express powerful constraints on types: templates and concepts. Throughout this chapter, you will learn how to write generic code using templates.
You will learn that templates are not just limited to generic programming but open the door to a realm of advanced techniques and metaprogramming, where you can harness the full power of the C++ language to manipulate types, and perform computations at compile time. These techniques, such as template specialization, and template metaprogramming, provide us with the ability to craft highly optimized and specialized code – and in this chapter, we explore them all.
We then move and explore various techniques to effectively apply constraints using concepts – a new and highly anticipated addition to the C++ language, added in C++20. We will cover different ways to use concepts, including the requires clause, trailing requires clause, abbreviated function templates, and constrained template parameters, and learn how to use concepts with functions and class templates.