简体   繁体   English

C ++强制编译器选择退出某些代码

[英]c++ force compiler to opt out some piece of code

I have a piece of code: 我有一段代码:

// some code, which only do sanity check
expensive checks
// sanity check end

Now how do I tell the compiler to force it to opt out this piece? 现在,我如何告诉编译器强制其选择退出? Basically it means when I compile with -O2 or O3, I don't want it to be there... 基本上,这意味着当我使用-O2或O3进行编译时,我不希望它在那里。

Thanks! 谢谢!

You can accomplish this with a constant and a single if/def pair. 您可以使用一个常数和一个if / def对来完成此操作。 This allows the code to still be compiled and checked for errors but omitted during optimization. 这样,仍可以编译代码并检查错误,但在优化过程中将其省略。 This can prevent changes that might break the check code from going undetected. 这样可以防止可能会破坏校验码的更改无法被检测到。

#if defined(USE_EXPENSIVE_CHECKS) || defined(DEBUG)
#define USE_EXPENSIVE_CHECKS_VALUE true
#else
#define USE_EXPENSIVE_CHECKS_VALUE false
#endif

namespace {
 const bool useExpensiveChecks = USE_EXPENSIVE_CHECKS_VALUE;
};

void function()
{
    if(useExpensiveChecks == true)
    {
        // expensive checks
    }
}

Instead of relying on the compiler to optimize the code out, you could pass the compiler an additional symbol definition only when you want the code to run: 不必依赖编译器优化代码,而仅在希望代码运行时才可以向编译器传递附加的符号定义:

// some code, which only do sanity check
#ifdef my_symbol
expensive checks
#endif
// sanity check end

Using macros and conditionals in the preprocessor is really the only way to avoid code being generated by the compiler. 在预处理器中使用宏和条件实际上是避免编译器生成代码的唯一方法。

So, here's how I would do it: 因此,这是我的处理方式:

#ifdef NEED_EXPENSIVE_CHECKS
inline expensive_checking(params...)
{
   ... do expensive checking here ... 
}
#else
inline expensive_checking(params...)
{
}
#endif

Then just call: 然后只需致电:

some code
expensive_checking(some_parameters...)
some other code

An empty inlined function will result in "no code" in any decent, modern compiler. 空的内联函数将在任何体面的现代编译器中导致“无代码”。 Use -DNEED_EXPENSIVE_CHECKS in your debug build settings, and don't use that in release build. 在调试版本设置中使用-DNEED_EXPENSIVE_CHECKS ,不要在发行版本中使用它。

I have also been known to use a combination of macro and function, such as this: 我也知道结合使用宏和函数,例如:

#ifdef NEED_EXPENSIVE_CHECKS
#define EXPENSIVE_CHECKS(stuff...) expensive_checks(__FILE__, __LINE__, stuff...)
inline expensive_checks(const char *file, int line, stuff ...)
{
   if (some_checking)
   {
      cerr << "Error, some_checking failed at " << file << ":" << line << endl;
   }
}
#else
#define EXPENSIVE_CHECKS(stuff...)
#endif

Now, you get information on which file and what line when something fails, which can be very useful if the checks are made in many places (and you can use __function__ or __pretty_function__ to get the function name as well, if you wish). 现在,您可以获得有关什么文件和什么行失败的信息,如果在很多地方进行了检查,这将非常有用(如果需要,您也可以使用__function____pretty_function__来获取函数名称)。

Obviously, the assert() macro will essentially do what my macro solution does, except it usually doesn't provide the filename and line-number. 显然, assert()宏实际上会执行我的宏解决方案,除非它通常不提供文件名和行号。

Move your checks into a different function, then import cassert and write assert(expensive_check()) . 将您的支票移到其他函数中,然后导入cassert并写入assert(expensive_check()) When you want to disable the checks, use #define NDEBUG before the inclusion of cassert. 如果要禁用检查,请装入cassert 之前使用#define NDEBUG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM