[C++17] Reduce Compile Time with constexpr-if

in #coding6 years ago (edited)

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.

Sort:  

This thing is very hard to understand for me. If there is any error in my learning, let me know.

Coin Marketplace

STEEM 0.21
TRX 0.13
JST 0.030
BTC 68152.98
ETH 3536.22
USDT 1.00
SBD 2.86