How it works...
The way constexpr if works is relatively simple: the condition in the if statement must be a compile-time expression that evaluates or can be convertible to a Boolean. If the condition is true, the body of the if statement is selected, which means it ends up into the translation unit for compilation. If the condition is false, the else branch, if any is defined, is evaluated. Return statements in discarded constexpr if branches to not contribute to the function return type deduction.
In the first example from the How to do it... section, the compute() function template has a clean signature. The body is also very simple; if the type that is substituted for the template parameter is an integral type, the compiler will select ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access