Conditional Compilation with if constexpr
(C++17)
(From Marius Bancila's Blog)
This is a feature of the preprocessor that selects whether to include or not a chunk of code in the final text file that will be passed to the compiler. Preprocessor conditional directives can check arithmetic expressions or whether a name is defined as a macro.
In the following example, a message is written to the standard output stream when the program is compiled using a debug configuration and the _DEBUG
macro is defined.
#define TRACE(x) std::cout << x << std::endl int main() { #ifdef _DEBUG TRACE("debug build"); #endif }
In C++17 this can be replaced with constexpr if as shown in the following example:
#include <string_view> inline void trace(std::string_view text) { std::cout << text << std::endl; } int main() { if constexpr(_DEBUG) trace("debug build"); }