本文介绍了在C或C良好的goto的例子++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个线程,我们来看一下C或C的转到的良好用途的例子++。它是由an回答这人投了,因为他们以为我在开玩笑。

In this thread, we look at examples of good uses of goto in C or C++. It's inspired by an answer which people voted up because they thought I was joking.

摘要(标签由原来的改变,使意图更加清晰):

Summary (label changed from original to make intent even clearer):

infinite_loop:

    // code goes here

goto infinite_loop;

为什么它比其它的方法更:

Why it's better than the alternatives:


  • 这是具体的。 转到
    语言结构导致的
    无条件分支。备择方案
    依赖于使用结构
    支持有条件的分支,
    与退化总是真
    状态。

  • 标签文档的意图
    无需额外的注释。

  • 读者不必扫描
    其间code早期取值
    (尽管它仍然可以为
    无原则的黑客模拟
    继续与早期转到)。

  • It's specific. goto is thelanguage construct which causes anunconditional branch. Alternativesdepend on using structuressupporting conditional branches,with a degenerate always-truecondition.
  • The label documents the intentwithout extra comments.
  • The reader doesn't have to scan theintervening code for early breaks(although it's still possible for anunprincipled hacker to simulatecontinue with an early goto).

规则:


  • pretend该gotophobes没有
    赢得。它的理解,上述
    不能实时code,因为可以使用
    它违背了既定的成语。

  • 假设我们都听过的
    后藤认为是有害的,并且知道
    即跳转可以用来写
    意大利面条code。

  • 如果您用一个例子不以为然,
    批评它的技术优势
    独('因为人们不喜欢
    转到不是技术上的原因)。

让我们看看我们是否能谈谈这就像大人。

Let's see if we can talk about this like grown ups.

修改

此问题似乎现在已经完成。这产生了一些高品质的答案。谢谢大家,
 尤其是那些谁拿了我的小环的例子重视。大多数怀疑论者担心
 由于缺乏块范围。作为@quinmars在评论中指出的那样,你可以随时把周围的括号
循环体。我顺便指出为(;;),而(真)不给你的牙套
免费或者(和忽略他们可能会导致棘手的错误)。无论如何,我不会浪费更多
在这一点小事你的脑力 - 我可以用无害的习惯为(;;),而(真)(一样好,如果我想继续我的工作)。

This question seems finished now. It generated some high quality answers. Thanks to everyone, especially those who took my little loop example seriously. Most skeptics were concerned by the lack of block scope. As @quinmars pointed out in a comment, you can always put braces around theloop body. I note in passing that for(;;) and while(true) don't give you the bracesfor free either (and omitting them can cause vexing bugs). Anyway, I won't waste any moreof your brain power on this trifle - I can live with the harmless and idiomatic for(;;) and while(true) (just as well if I want to keep my job).

考虑其他的反应,我看到许多人认为转到的东西你总是
在另一种方式改写。当然你也可以通过引入循环避免转到
 一个额外的标志,嵌套的如果 S,或任何堆栈,但为什么不考虑是否转到
也许是这个职位的最佳工具?换句话说,有多少丑陋的人prepared忍受避免使用内置的语言特性,达到预期的目的?我的看法是,
甚至把一个标志是太高的代价。我喜欢我的变量重新present东西
问题或解决方案领域。 独资,以避免转到'不剪。

Considering the other responses, I see that many people view goto as something you alwayshave to rewrite in another way. Of course you can avoid a goto by introducing a loop, an extra flag, a stack of nested ifs, or whatever, but why not consider whether goto isperhaps the best tool for the job? Put another way, how much ugliness are people prepared to endure to avoid using a built-in language feature for its intended purpose? My take is thateven adding a flag is too high a price to pay. I like my variables to represent things inthe problem or solution domains. 'Solely to avoid a goto' doesn't cut it.

我会接受这给了模式C分支到清理块的第一个答案。 IMO,这使得一个转到所有公布答案最强情况下,肯定
如果你通过一个仇敌要经过的扭曲衡量它,以避免它。

I'll accept the first answer which gave the C pattern for branching to a cleanup block. IMO, this makes the strongest case for a goto of all the posted answers, certainlyif you measure it by the contortions a hater has to go through to avoid it.

推荐答案

下面有一招我听说过的人们使用。我从来没有在野外看到它虽然。而且它仅适用于C,因为C ++有RAII这更习惯用法做。

Heres one trick I've heard of people using. I've never seen it in the wild though. And it only applies to C because C++ has RAII to do this more idiomatically.

void foo()
{
    if (!doA())
        goto exit;
    if (!doB())
        goto cleanupA;
    if (!doC())
        goto cleanupB;

    /* everything has succeeded */
    return;

cleanupB:
    undoB();
cleanupA:
    undoA();
exit:
    return;
}

这篇关于在C或C良好的goto的例子++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:55
查看更多