Metaprogramming is a broad and complicated concept. There are several thick books talking about it, for example, “C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond”. I see it as a template programming tool that helps transfer some run-time work to compile time.
Below is a toy example of metaprogramming, a for-loop on compile time to print variable types. To see how it is interpreted on the compiler side, copy the code below to https://cppinsights.io/ and set the version to C++ 20 (It must be C++ 20 and after because the older versions use enable if instead of requires).
#include <tuple>
#include <memory>
#include <type_traits>
#include <iostream>
#include <iomanip>
template<typename TypeToPrint>
void printType(TypeToPrint&& t) {
std::cout << typeid(t).name() << std::endl;
}
template<typename... TypesToPrint>
void printType(TypesToPrint&&... ts) {
(printType(ts), ...);
}
// for loop base case
template<size_t I = 0, typename... TypesToPrint>
requires (I == sizeof...(TypesToPrint))
void printTypeNew(const std::tuple<TypesToPrint...>&) {}
// for loop reduction
template<size_t I = 0, typename... TypesToPrint>
requires (I < sizeof...(TypesToPrint))
void printTypeNew(const std::tuple<TypesToPrint...>& ts) {
printType(get<I>(ts));
printTypeNew<I + 1, TypesToPrint...>(ts);
}
template <typename... Types>
struct Tuple {
std::tuple<Types...> t;
void printAllTypes() const {
std::apply([](auto&&... ts){ printType(ts...); }, t);
}
void printAllTypesNew() const {
printTypeNew(t);
}
};
int main() {
Tuple<int*, std::unique_ptr<float>> T2;
T2.printAllTypes();
std::cout << "New way" << std::endl;
T2.printAllTypesNew();
}

![Rendered by QuickLaTeX.com \[\begin{aligned}f(t,T)&=\lim_{\delta\rightarrow 0}\frac{1}{\delta}\left(\frac{P(t,T)}{P(t,T+\delta)}-1\right) \\ & = -\frac{\partial \log P(t,T)}{\partial T}\end{aligned}.\]](https://sisitang0.com/wp-content/ql-cache/quicklatex.com-b5a0732f224c579d77f26db98449ca36_l3.png)
![Rendered by QuickLaTeX.com \[A_n(t) = \sum_{k=n+1}^{N+1} \alpha_{k-1}P(t,T_k).\]](https://sisitang0.com/wp-content/ql-cache/quicklatex.com-b2ec3de0caee6a648cf582e5f4fa3a7f_l3.png)
![Rendered by QuickLaTeX.com \[\begin{aligned}& PV_{\mbox{float}} \\ = & \sum_{k=n}^N L_k(t)\alpha_k P(t,T_{k+1}) \\ =& \sum_{k=n}^N \frac{1}{\alpha_k}\left(\frac{P(t,T_k)}{P(t,T_{k+1})}-1\right) \alpha_k P(t,T_{k+1}) \\ =& \sum_{k=n}^N \left(P(t,T_k) - P(t,T_{k+1})\right) \\ =& P(t,T_n) - P(t,T_{N+1}) \end{aligned}\]](https://sisitang0.com/wp-content/ql-cache/quicklatex.com-faa76195580877c1bc9e022c4ab6f116_l3.png)
![Rendered by QuickLaTeX.com \[\begin{aligned} g(t,x) := \mathbf{E} [&\int_t^Te^{-\int_t^TV(\tau,X_{\tau})\textnormal{d}\tau}f(r,X_r)\textnormal{d}r \\ &+e^{-\int_t^TV(\tau,X_{\tau})}h(X_T)|X_t = x ] \end{aligned}.\]](https://sisitang0.com/wp-content/ql-cache/quicklatex.com-ce949eee9441cd7cb71600b656d22116_l3.png)
![Rendered by QuickLaTeX.com \[\begin{aligned} \textnormal{d}g(t,X_t) & = g_t\textnormal{d}t + g_x \textnormal{d} x + \frac{1}{2}g_{xx} \textnormal{d} X \textnormal{d} X \\ & = [g_t+\beta g_x + \frac{1}{2}\gamma^2 g_{xx}] \textnormal{d} t + \gamma g_x \textnormal{d} W\end{aligned}.\]](https://sisitang0.com/wp-content/ql-cache/quicklatex.com-91595ea304137e12671716f319367ebf_l3.png)