Is it possible to jump from an unnamed scope?
void MyFunc() {
{
... // Code
if (!head_size) {
goto _common_error; // <- break and continue don't work here
}
... // Code
if (!tail_size) {
goto _common_error; // second time
}
... // Code
}
_common_error:
{
... // Code
}
}
My question is not whether this can be redesigned, but whether there is a trick in c++ that I don't know.
Is there a mechanism in c++ other than goto to jump out of an unnamed scope? break and continue do not work in scopes.
Update1: changed word namespace to scope
Yes, you need to use goto
to jump out of a scope.
break
can only be used to jump out of a loop or switch.
But you can use a (questionable) trick by using a dummy loop:
void MyFunc() {
do {
... // Code
if (!head_size) {
break;
}
... // Code
if (!tail_size) {
break;
}
... // Code
} while (false);
{
... // Error handling code
}
}