为什么错误的语句仍然执行?

发布于 2024-12-01 17:04:00 字数 596 浏览 3 评论 0原文

我有这段代码...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-12-08 17:04:00

if (false)永远执行其主体...因为条件的值永远不会为真。因此,在您给出的代码中,drawMap 的其余部分将始终执行,因为它永远在开始时不会返回。

考虑 if (x == 5) - 仅当表达式 x == 5 为 true 时才会执行。现在用 false 替换 x == 5...

如果您想要一个if语句总是执行,你想要的

if (true)

是。

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 of drawMap will always execute because it will never return at the start.

Consider if (x == 5) - that will only execute if the expression x == 5 is true. Now substitute false for x == 5...

If you want an if statement which will always execute, you want

if (true)

instead.

只是一片海 2024-12-08 17:04:00

把我算作那些实际上没有足够好地阅读问题的人群,或者如果问题如此简单的话,他们无法相信OP不理解问题的人:)

约翰·斯基特的答案当然是现货on :)

两个想法:

  1. 如果您在调试器中,当使用优化编译时,行可能会出现被执行、无序、根本不执行或出现在意外的行。这是因为某些机器指令将“归因于”不同的源代码行。不进行优化即可编译,以消除混乱的根源。这只是令人困惑,因为优化应该(!除非编译器错误)不会改变有效行为

  2. 这可能是你得到了一个你无法信任的邪恶的#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:

  1. 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

  2. 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

Blockquote

苍白女子 2024-12-08 17:04:00
if (false)

类似于

if (1 == 2)

,因此永远不会执行下一条语句(或块)。

根据您的情况考虑我发表的以下评论:

void drawMap(void)
{
    if (false) return; //Not gonna happen.

    //The following will always happen
    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)

is analagous to

if (1 == 2)

and will therefore never execute the next statement (or block).

In your context consider the following comments I made:

void drawMap(void)
{
    if (false) return; //Not gonna happen.

    //The following will always happen
    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);
    }
}
夜声 2024-12-08 17:04:00

我已经看到了 if(false) 的用法,在类似 switch / case 的结构中,如下所示:

int ret = doSomeThingFunction();
if (false) {}
else if (ret < 0 ) {

}
else if (ret == 0) {

}
else if (ret > 0) {

}

I have seen the usage of this if(false), in a switch / case like construction like this:

int ret = doSomeThingFunction();
if (false) {}
else if (ret < 0 ) {

}
else if (ret == 0) {

}
else if (ret > 0) {

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文