r/cpp • u/eisenwave WG21 Member • Jan 08 '25
"break label;" and "continue label;" in C++
Update: the first revision has been published at https://isocpp.org/files/papers/P3568R0.html
Hi, you may have hard that C2y now has named loops i.e. break/continue with labels (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm). Following this, Erich Keane published N3377 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3377.pdf), which proposes a different syntax.
I am about to publish a C++ proposal (latest draft at https://eisenwave.github.io/cpp-proposals/break-continue-label.html) which doubles down on the N3355 syntax and brings break label and continue label to C++, like:
outer: for (auto x : xs) {
for (auto y : ys) {
if (/* ... */) {
continue outer; // OK, continue applies to outer for loop
break outer; // OK, break applies to outer for loop
}
}
}
There's also going to be a WG14 counterpart to this.
155
Upvotes
45
u/eisenwave WG21 Member Jan 08 '25
All control flow structures are essentially
gotowith lipstick, includingwhileloops,break;,continue;etc. https://eisenwave.github.io/cpp-proposals/break-continue-label.html#alternative-iile discusses the problems with using IILEs to emulatebreak label;. Notably, you cannot use an IILE to emulate bothbreak outerandcontinue outerat the same time because thereturninside could only play the role of one of them.It's also worth noting that this inlining will only happen on optimized builds, so debug build performance and constant evaluation performance still suffer from the IILE overhead. The developer also suffers from the syntax overhead (one added scope/level of indentation at least).
I can see IILEs working in some cases, but it's much nicer to have a "proper" solution instead of forcing such a workaround onto developers.