Compile-Time Programming
C++ has the ability to evaluate expressions at compile time, meaning that values are already calculated when the program executes. Even though metaprogramming has been possible since C++98, it was initially very complicated due to its complex template-based syntax. With the introduction of constexpr
, if constexpr
, and recently, C++ concepts, metaprogramming has become much more similar to writing regular code.
This chapter will give you a brief introduction to compile-time expression evaluations in C++ and how they can be used for optimization.
We will cover the following topics:
- Metaprogramming using C++ templates and how to write abbreviated function templates in C++20
- Inspecting and manipulating types at compile time using type traits
- Constant expressions that are evaluated by the compiler
- C++20 concepts and how to use them to add constraints to our template parameters
- Some real-world examples of metaprogramming...