# Compile-time Programming in C++ Team Chocomel(Y.Tayeh and R.Davletova) chose Compile-time programming as the second topic. We will present the our research of this topic in several **"blog posts"**. ## Post 1: What is Compile-time programming? Before implementing our program, we had to research and study the following concepts: - **Compile-time:** is the period in which the code is converted to machine code. - **Run-time:** is the final period of a computer program's life cycle, in which the code is executed. - **Compile-time Programming:** is the program code that can be executed during the compile-time, rather than the normal procedure which is during run-time. ## Post 2: Choice of the topic We have decided to choose this topic for the following reasons: - Wanting to experience a new approach in programming. - Wanting to understand different and faster ways to compile a program. - Wanting to explore how does a compiler compiles the code. - Wanting to experience function templates a bit more and understand different uses of it - Fold packs seemed like an interesting concept. ## Post 3: How to do compile-time programming in C++ Compile-time programming in C++ can be done is several ways. We used following data types and instructions in our implementation: - constexpr (C++11) - may be evaluated at compile-time, but may be at runtime. - consteval (C++20) - must be evaluated at compile-time; always produce a compile-time expression and always visible only at compile-time. consteval can only be applied to the declaration of a function or function template - constinit (C++20)- asserts that a variable has static initialization (zero initialization or constant initialization). - templates - functions, classes, or variables written once for use with multiple types. - folds - expression is an instruction for the compiler to repeat the application of an operator over a variadic template pack. Compilation times and memory usage are expected to be way better with fold expressions than with recursive instantiations. ## Post 4: C++ implementation In the beginning of the assignment we wrote a couple of compile-time functions. however, we struggled with finding the best concept of the program to use these functions. after few discussions we've decided to add the following elements to our program: - a compile-time class with compile-time functions. - a compile-time array, that gets filled on compile-time. - an old style compile-time functions and Folds. Our compile-time class consisted of a simple calculator that calculates sum, subtraction, multiplication and division. in addittion to a function to check if a number is a prime number and 2 functions to print the numbers in an array, all of these functions are compile-time functions that should be evaluated during compile-time. ## Post 5: The end result After completing our program, we wanted to test if compile-time functions were actually evaluated at compile time, so we tested the Fibo function with a benchmark to test how fast is it going to compile compared to a normal run-time Fibo function, and the result was that compile-time function was 1.2 times faster than the run-time function. ![](https://i.imgur.com/cHDGs09.png)