Back to basics (compile-time programming with templates)
In the previous chapter, we discussed what templates are. In this chapter, we will go deeper into templates and will discuss why it is important to compute programs at compile time. In C++, there are some ways to compute values at compile time. New features have been added to language standards to carry out these functions.
The C++ template system is Turing-complete, meaning it has the ability to compute anything that can be computed, which was discovered during the process of standardizing the language. The first example of this was software that computed prime numbers, even though it did not complete compilation; the list of prime numbers was a component of the compiler’s error message. In essence, code determines whether a given number is a prime number at compile time.
Let us see the following example:
#include <iostream>template <size_t n> class Foo { public: Foo(void ...