Introduction to Template Metaprogramming

Learn about metaprogramming and creating templates in C++.

We will begin with an introduction to template metaprogramming.

Metaprogramming in c++

When writing regular C++ code, it is eventually transformed into machine code. Metaprogramming, however, allows us to write code that transforms itself into regular C++ code. In a more general sense, metaprogramming is a technique where we write code that transforms or generates some other code. By using metaprogramming, we can avoid duplicating code that only differs slightly based on the data types we use, or we can minimize runtime costs by precomputing values that can be known before the final program executes. Nothing stops us from generating C++ code by using other languages. We could, for example, do metaprogramming by using preprocessor macros extensively or writing a Python script that generates or modifies C++ files for us:

Get hands-on with 1400+ tech skills courses.