为什么错误的语句仍然执行?
我有这段代码...
void drawMap(void)
{
if (false)
return;
for(auto iter = this->m_layers.begin(); iter != m_layers.end(); ++iter)
{
if ((*iter)->get() == NULL)
continue;
PN::draw((*iter)->get(), b2Vec2(0,0), true, 0);
}
}
如果我没有记错的话,它永远不会执行...但是它确实...并且当我更改
if (false)
return;
为
if (false)
return;
else
return;
它时,它现在根本不执行,但是第一个语句怎么可能不是假的呢? 吃头痛药
PS 我这样做只是因为我在调试时发现我的代码在不应该的情况下绘制到了屏幕上。
I have this code...
void drawMap(void)
{
if (false)
return;
for(auto iter = this->m_layers.begin(); iter != m_layers.end(); ++iter)
{
if ((*iter)->get() == NULL)
continue;
PN::draw((*iter)->get(), b2Vec2(0,0), true, 0);
}
}
If I'm not mistaken it should NEVER execute...but it does...and when I change
if (false)
return;
to
if (false)
return;
else
return;
it doesn't execute at all now, but how can that first statement NOT be false? grabs headache pills
P.S. I only did this 'cause I was debugging and noticed my code was drawing to the screen when it wasn't supposed to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
if (false)
将永远执行其主体...因为条件的值永远不会为真。因此,在您给出的代码中,drawMap
的其余部分将始终执行,因为它永远在开始时不会返回。考虑
if (x == 5)
- 仅当表达式x == 5
为 true 时才会执行。现在用false
替换x == 5
...如果您想要一个
if
语句总是执行,你想要的是。
if (false)
will never execute its body... because the value of the condition is never true. So in the code you've given, the remainder ofdrawMap
will always execute because it will never return at the start.Consider
if (x == 5)
- that will only execute if the expressionx == 5
is true. Now substitutefalse
forx == 5
...If you want an
if
statement which will always execute, you wantinstead.
把我算作那些实际上没有足够好地阅读问题的人群,或者如果问题如此简单的话,他们无法相信OP不理解问题的人:)
约翰·斯基特的答案当然是现货on :)
两个想法:
如果您在调试器中,当使用优化编译时,行可能会出现被执行、无序、根本不执行或出现在意外的行。这是因为某些机器指令将“归因于”不同的源代码行。不进行优化即可编译,以消除混乱的根源。这只是令人困惑,因为优化应该(!除非编译器错误)不会改变有效行为
这可能是你得到了一个你无法信任的邪恶的#define for false。通过仅通过预处理器运行代码来排除这种情况。
g++ -E
会做到这一点。 MSVC++ 有一个“保留预处理”源的选项Count me in with the crowd that didn't actually read the problem well enough, or couldn't believe that the OP didn't understand the problem if it were so simple :)
John Skeet's answer, of course, was spot on :)
Two thoughts:
If you're in a debugger, lines can appear to be executed, out of order, not at all or at unexpected lines when compiled with optimizations. This is because some machine instructions will get 'attributed' to different source lines. Compile without optimization to eliminate the source of confusion. It is confusing only, as optimizations should (! barring compiler bugs) not alter effective behaviour
It could be that you're getting an evil #define for false that you cannot trust. Rule this out by running the code through preprocessor only.
g++ -E
will do that. MSVC++ has an option to 'keep preprocessed' source类似于
,因此永远不会执行下一条语句(或块)。
根据您的情况考虑我发表的以下评论:
is analagous to
and will therefore never execute the next statement (or block).
In your context consider the following comments I made:
我已经看到了 if(false) 的用法,在类似 switch / case 的结构中,如下所示:
I have seen the usage of this if(false), in a switch / case like construction like this: