[C++17] Reduce Compile Time with constexpr-if
Pattern
if constexpr(condition){
/* do something */
} else ifconstexpr(condition){
/* do something */
}
Description
constexpr-if
looks very similar to if-else
. The difference is that constexpr-if
's condition is evaluated during compile time, therefore the condition must be const. When the condition is true, the false statement is abandoned and never compiled, and vice versa. This is useful not only for your compile time reduction, but also the code will looks more intuitive.
Explanation
constexpr
is featured since C++11. constexpr
is which has much more constantity than const
. The main difference is constexpr
variable is initialized in compile time, while const
is in runtime. When you define constexpr
function, it implys the function is inline.
This thing is very hard to understand for me. If there is any error in my learning, let me know.