在这种非常特殊的情况下使用 goto ......替代方案?

Use of goto in this very specific case... alternatives?

我对在 C++ 代码中可能使用 goto 有疑问:我知道应尽可能避免使用 goto,但在这种非常特殊的情况下,我有找到避免使用多个嵌套 if-else and/or 附加二进制标志的良好替代方案的难度很小...... 代码如下(只报告相关部分):

// ... do initializations, variable declarations, etc...

    while(some_flag) {
        some_flag=false;

        if(some_other_condition) {
            // ... do few operations (20 lines of code)

            return_flag=foo(input_args); // Function that can find an error, returning false
            if(!return_flag) {
                // Print error
                break; // jump out of the main while loop
            }

            // ... do other more complex operations
        }

        index=0;
        while(index<=SOME_VALUE) {
            // ... do few operations (about 10 lines of code)
            return_flag=foo(input_args); // Function that can find an error, returning false
            if(!return_flag) {
                goto end_here; // <- 'goto' statement
            }

            // ... do other more complex operations (including some if-else and the possibility to set some_flag to true or leave it to false
            // ... get a "value" to be compared with a saved one in order to decide whether to continue looping or not
            if(value<savedValue) {
                // Do other operations (about 20 lines of code)
                some_flag=true;
            }
            // ... handle 'index'
            it++; // Increse number of iterations
        }

        // ... when going out from the while loop, some other operations must be done, at the moment no matter the value of some_flag

        return_flag=foo(input_args);
        if(!return_flag) {
            goto end_here; // <- 'goto' statement
        }

        // ... other operations here
        // ... get a "value" to be compared with a saved one in order to decide whether to continue looping or not
        if(value<savedValue) {
            // Do other operations (about 20 lines of code)
            some_flag=true;
        }

        // Additional termination constraint
        if(it>MAX_ITERATIONS) {
            some_flag=false;
        }

        end_here:
        // The code after end_here checks for some_flag, and executes some operations that must always be done,
        // no matter if we arrive here due to 'goto' or due to normal execution.
    }
}

// ...

每一次foo()returnsfalse,不要再执行任何操作,代码应该尽快执行最后的操作。另外一个要求就是这段代码,主要是while(index<=SOME_VALUE)里面的部分要运行尽可能的快,尽量有好的整体性能。

正在使用 'try/catch' 块,其中 try{} 包含大量代码(而实际上,函数 foo() 只能在调用时产生错误,即在两个不同的点仅代码)一个可能的选择?在这种情况下使用不同的 'try/catch' 块更好吗? 还有其他更好的选择吗?

非常感谢!

一种方法是使用另一个 dummy 循环和 break 像这样

 int state = FAIL_STATE;

 do {
     if(!operation()) {
         break;
     }

     if(!other_operation()) {
         break;
     }
     // ...
     state = OK_STATE;
 } while(false);

 // check for state here and do necessary cleanups

这样您就可以事先避免代码中的深层嵌套。

三个明显的选择:

  1. 坚持goto

  2. 将清除代码与某些 RAII class 的析构函数相关联。 (您可以将其写为 std::unique_ptr 作为 lambda 的删除。)

  3. 将您的函数重命名为 foo_internal,并将其更改为 return。然后在调用 foo_internal

  4. 的新 foo 函数中编写清理

所以:

return_t foo(Args...) {
    const auto result = foo_internal(Args..);
    // cleanup
    return result;
}

总的来说,您的函数看起来太长了,需要分解成更小的部分。

是C++!对非本地跳转使用异常:

try {
    if(some_result() < threshold) throw false;
}
catch(bool) {
    handleErrors();
}
// Here follows mandatory cleanup for both sucsesses and failures