C++17: constexpr if

Written by on December 17, 2017 in C++, Programming with 0 Comments

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.

Example1

Example1

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.

Generated Code

Generated Code

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:

Example2

Example2

Here is the assembly code generated for this code:

Generated Code

Generated 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.

Compile-time Check Needed

Compile-time Check Needed

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.

Example3

Example3

In this respect, it is different from #if or #ifdef. Check this out:

Preprocessor Example

Preprocessor Example

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!):

Template Example

Template Example

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):

Template Example Pre-C++17

Template Example (Pre-C++17)

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!

 

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published.

Top