Which one is more appropriate for compile-time configurations (such as debug/release), preprocessor directives, or if constexpr
?
#define DBG
#if DBG
// some code
#endif
// --------------------------------- or
inline constexpr bool DEBUG { true };
if constexpr ( DEBUG )
{
// some code
}
Note that, if if constexpr
is not part of a template, then the other parts of the if
(such as the else
s) are still compiled and checked for validity.
From cppreference:
If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated .
Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive: