In this recipe, we will learn how to use a new feature in C++17 called constexpr if. This recipe is important because it will teach you how to create if statements that are evaluated at runtime. Specifically, what this means is that the branch logic is picked at compile time and not at runtime. This allows you to change the behavior of a function at compile time without sacrificing performance, something that, in the past, could only be done with macros, which is not useful in template programming, as we will show.
Using if constexpr
Getting ready
Before beginning, please ensure that all of the technical requirements are met, including installing Ubuntu 18.04 or higher and running the following in a Terminal window:
> sudo...