如何在运行时有效地排除代码块

Jal*_*isa 1 c++ preprocessor

为了在编译期间从模型中排除代码块,我将预处理器用作:

#ifdef setting1
do something
#endif //setting1
Run Code Online (Sandbox Code Playgroud)

有时我打算在编译过程中保留一段代码,但在运行时将其排除在外。if为此目的,有没有比声明更好的方法了?

bob*_*bah 5

为了保持它的“原始”,你只需更换#ifdef喜欢的东西


bool const theflag = false;

if constexpr (theflag) {
    dosomething
}
Run Code Online (Sandbox Code Playgroud)

这样,dosomething仍将对语法进行检查。

  • 并且const bool theFlag = false; (2认同)