constexpr if is another nice little feature in C++17, which significantly simplifies the way we code, especially in the context of templates. In this article, I will briefly talk about this feature and look at some examples.
constexpr if is a variant of the standard if statement we have had all along. The primary difference is that the condition part in constexpr if must be a compile-time evaluable expression unlike the standard if, where the condition is evaluated at run-time. Because of this special requirement, the compiler guarantees that the code it generates for the constexpr if will just be the sub-part of the if statement that is reachable. Take a look at the following example.
As you can see, the function checkCondition() in the above example is a constexpr function, in that its return value is known at compile-time. (In this article, we are not going into the details of constexpr in general. If time permits, I will cover that in a separate future article.)
So, what is the code emitted by the compiler for the constexpr if statement? The figure below shows the assembly code generated by Visual Studio 2017.
You can see that the false part of the if statement alone has been considered. The reason is, the compiler figured out that the condition is false (always) and hence the true part of the if statement can never get a chance to execute!
Just to compare with the standard if statement, take a look at the following code:
Here is the assembly code generated for this code:
Because the logic for checking the condition is executed at run-time (as opposed to compile-time) in this case, the compiler has to emit code for both the true and false parts of the if statement. It cannot assume that only one of the two will always execute.
So, compared to the regular if statement, constexpr if results in more compact code. The limitation, obviously, is that the condition must be a compile-time knowable expression. For example, the following code will not compile because the condition cannot be evaluated at compile time.
Here is another important thing to remember. Even though we might know that one part of the constexpr if statement will not be compiled, we cannot have invalid C++ statements in that part. This will be clear from the following example.
In this respect, it is different from #if or #ifdef. Check this out:
constexpr if is very useful in the case of templates. It can simplify things considerably. Here is an example of compile-time evaluation of a function (obviously, not suitable for serious context!):
If the above function is to be re-written without constexpr if (pre C++17), then it would look like this (I have changed the function name for convenience):
It is clear that substantial saving in coding has been achieved by the use of constexpr if. Not only that, the logic is much clearer!
You can find many more interesting examples if you dig into the template library. But hopefully, the above gives you an idea.
OK, that is it for today. Have a great weekend!
Recent Comments