/ C++FUN

Multi-Level Break in C++ via IIFE

I guess we all have been at this point.

for (auto i : ...)
    for (auto j : ...)
        if (condition(i, j))
        {
            break outer???
        }

You want to search something, and for one reason or another you end up with a nested loop. You find what you searched for and now want to break all the way to the outer loop.

If only we had multi-level breaks.

But we don’t.

So people introduce flags:

auto found = false;
for (auto i : ...)
{
    for (auto j : ...)
        if (condition(i, j))
        {
            found = true;
            break;
        }

    if (found)
        break;
}

Which introduces quite the clutter and can be error-prone if the loops contain more code and the second break is somehow missed or not executed.

Or people (*gasp*) introduce gotos:

for (auto i : ...)
    for (auto j : ...)
        if (condition(i, j))
        {
            goto next;
        }
next:;

But C is the enemy, isn’t it?

Okay, so we clearly need a multi-level break appropriate for modern C++.

I mean, we could propose new syntax… co_break, anyone?

Yeah, yeah, I get it. Has been done too many times already.

But behold! We already have shiny lambda expressions and immediately invoked function expressions (IIFEs). And they are more than adequate to solve our problem:

[&] {
    for (auto i : ...)
        for (auto j : ...)
            if (condition(i, j))
            {
                return;
            }
}();

No need to introduce additional flags, identifiers, labels. Just good old [&]{}(); and return.

This post is tagged as C++ and fun and is suited best as some mid-week entertainment in turbulent times.

(Title image from pixabay)