0

To reduce indentation in my code I want to skip a if statement if a condition is met. Let this be the following code:

if (foo()) {
    if (bar()) if_continue;        // continue the code and dont execute buz()

    buz();
}
...¹

The normal continue does not work. And I also cant use return because code at [¹] should still be executed.

I also want to avoid a solution like this, because this would result in unwanted indentation:

if (foo()) {
    if (!bar()) {
        ...
    }
}

Is this possible in C++?

Samuel
  • 388
  • 5
  • 15

1 Answers1

0

You can replace the true-branch statement by a call to a lambda function from which you return, if you want something like that:

  if (foo()) [](){
      if (bar()) {
        return;
      }
      buz();
    }();

But personally I find this

if (foo()) {
    if (!bar()) {
        ...
    }
}

a lot more readable.

Rulle
  • 4,496
  • 1
  • 15
  • 21
  • Arguably the lambda should be wrapped in `{}` or at least started on a separate lines yielding no benefit for intendation.. – fabian Nov 27 '22 at 11:24