Why and when to use constexpr
In today's article I just want to briefly cover why and when you would use the keyword constexpr. Constexpr was introduced in C++11 and is used to tell the compiler: it may be possible to evaluate this expression at compile time. As an example: Here we have a recursive function which calculates the Fibonacci number at position n in the sequence. Now we need to ask ourselves, can this computation be carried out at compile time? Well, the answer is yes it can, if we know the value of n at compile time. Lets take the above example a bit further, lets say we want the Fibonacci number at position 6 in the sequence: Now, to properly demonstrate the power of compile time optimisations and the importance of making use of them as best we can in our code I'm going to use a tool I started using recently called compiler explorer . Compiler explorer let's us enter code snippets and view the assembly output generated by the compiler. I won't pretend to be an expert in assembly as...