Language feature: Compile-time if statement / if-constexpr



Before discussing about if-constexpr, we should discuss first about constexpr. It was introduced in C++11 to mark an expression as having a compile-time constant result. More about constexpr can be read here.


if-constexpr simply add if statements at compile time. This way, we can get rid of some difficult template pattern-matching and SFINAE situations.

Other than that, they work in perfect harmony with constexpr functions and variables, providing further support.

Before C++17, a code that is using enable_if and SFINAE would look like this:

With if-constexpr, we can change the two functions into one:

You may also like...

Leave a Reply