如果为false,const debug变量被优化掉了吗? [英] const debug variable optimized away if false?

查看:96
本文介绍了如果为false,const debug变量被优化掉了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级中有一个

static const bool debug = false;

变量。调试输出就像这样,很简单:

if(debug)cerr<< debug message;


如果

调试变量为false,是否保证这个if / then子句总是被优化掉?


谢谢!

Markus

I have a
static const bool debug = false;
variable in my class. Debug output goes like this, trivially:
if(debug) cerr << "debug message";

Is it guaranteed that this if/then clause is always optimized away if the
debug variable is false?

Thanks!
Markus

推荐答案

" Markus Dehmann" <毫安************ @ gmail.com>写了...
"Markus Dehmann" <ma************@gmail.com> wrote...
我的班级中有一个
静态const bool debug = false;
变量。调试输出如下所示:
if(debug)cerr<< debug message;

如果
调试变量为false,是否保证这个if / then子句总是被优化掉?
I have a
static const bool debug = false;
variable in my class. Debug output goes like this, trivially:
if(debug) cerr << "debug message";

Is it guaranteed that this if/then clause is always optimized away if the
debug variable is false?




不。在优化方面没有任何保证。除非

你自己以某种方式删除它,它可能仍然存在。


更通用的保证方法是使用一个宏来使用
$如果定义了NDEBUG,则b $ b扩展为空:


#ifndef NDEBUG

#define PRINTDEBUGMESSAGE(a)cerr<< (a)

#else

#define PRINTDEBUGMESSAGE(a)

#endif


现在,当你在你的代码中使用


PRINTDEBUGMESSAGE(debug message);


时,如果NDEBUG是一个空语句,它将是一个空语句定义(通常

所以在发布版本中)。


类似的方法可以一次输出多个项目。


V



Nope. Nothing is guaranteed when it comes to optimisation. Unless
you remove it yourself somehow, it may still be there.

The more generically guaranteed approach is to use a macro that would
expand to nothing if NDEBUG is defined:

#ifndef NDEBUG
#define PRINTDEBUGMESSAGE(a) cerr << (a)
#else
#define PRINTDEBUGMESSAGE(a)
#endif

Now, when you use

PRINTDEBUGMESSAGE("debug message");

in your code, it will be an empty statement if NDEBUG is defined (usually
so in release builds).

Similar approach can be taken to output more than one item at a time.

V


Nope。


例如,如果您将调试信息编译到您的

可执行文件,许多编译器都不会进行优化,因为很多优化(例如内联)与调试器有关。


如果你想成为100%肯定它不存在,我很确定你

必须使用预处理器。我会做这样的事情:


#ifdef DEBUG

#define DEBUG_PRINT(string)cerr<< (字符串)

#elseif

#else

#define DEBUG_PRINT(字符串)

#endif


(可能那里有一些微妙的错误。我不知道

是否可以放入;或者不在宏中。)


那么你只需要做DEBUG_PRINT(调试消息);

也就是说,如果你用优化编译,编译器几乎肯定会是
这样做。


您可以随时查看装配输出并看得太确定。


(有些事情你可以做些增加那里没有什么机会,但我不认为这是值得的。)

Nope.

For instance, if you compile debugging information into your
executable, many compilers will not optimize because a lot of
optimizations (e.g. inlining) screw with the debugger.

If you want to be 100% positive it''s not there, I''m pretty sure you
have to use the preprocessor. I would do something like this:

#ifdef DEBUG
#define DEBUG_PRINT(string) cerr << (string)
#elseif
#else
#define DEBUG_PRINT(string)
#endif

(Probably there are a couple subtle errors there. I don''t know whether
to put the ; in the macro or not either.)

Then you just do DEBUG_PRINT("debug message");
That said, if you compile with optimizations the compiler almost
certainly will do it.

You could always look at the assembly output and see too be sure.

(There are things you could do to increase the chance nothing would be
there, but I don''t think it''s worth it.)


大多数商业级编译器将优化它。但标准

并不能保证它b / b $ b检查生成的程序集,看看它实际发生在你的
编译器上。此外,大多数编译器默认情况下不执行优化

a DEBUG build


Raj

most commercial grade compilers will optimize it away. But the standard
doesn''t guarantee it

Check the assembly generated and see it actually happens for your
compiler. Also most compilers dont perform optimizations by default for
a DEBUG build

Raj


这篇关于如果为false,const debug变量被优化掉了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆