Date: Wed, 18 Dec 2024 11:49:53 +0100
I want to have break/continue statements with labels in C++. Now is the time.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm has been
accepted into C2y, with the syntax:
label: for (/* ... */) {
while (true) { break label; }
}
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3377.pdf proposes to
update this syntax to
for label (/* ... */) {
while (true) { break label; }
}
While N3377 may be a bit more syntactically sound (not reusing labels
that are otherwise only goto targets, I'm not a fan of that idea.
Putting "label:" before a loop has a lot of precedent in other
languages (e.g. Java). Assuming that N3377 gets rejected, the syntax
would also be a good fit for C++.
There is no good solution for breaking out of nested loops, or out of
a switch statement within a loop:
- You can put the whole thing into a function and rely on "return",
but that may decrease readability and add the cost of a whole new
separate function.
- You can use an IILE (immediately invoked lambda expression), but
that increases indentation level, makes a new scope, and overall adds
unwarranted complexity.
- You can use "goto after_loop;" with some label, but goto does not
work in constant expressions, is unconditionally banned from some
style guides, and overall won't be the future.
- You can use an extra bool to indicate whether the outer loop should
continue, but this adds more mutable state to your function and
doesn't express the idea of "just break out of both loops" clearly.
Every alternative sucks, and most general purpose languages provide
some way of breaking out of multiple loops. C++ is really behind and
should follow in C's footsteps, with whatever syntax C2y lands on.
Note that "break label" has been proposed most recently in
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3879.pdf
(was rejected), but this is because the author stupidly bunched the
popular "break label" idea together with unpopular, novel ideas, such
as "goto case-label" within switches, and the whole thing was thrown
out.
Since then, IIRC there have also been other proposals like "break N"
for breaking out of N nested loops, but all of those were rejected.
Anyhow, my questions for std-proposal are mainly:
- Is anyone already working on this?
- Is anyone interested in co-authoring?
I have no doubt in my mind that a small, simple, proposal which just
brings the syntax from C2y into C++ will pass, and we might even make
it into C++26 if we act swiftly.
Yours
Jan Schultke
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm has been
accepted into C2y, with the syntax:
label: for (/* ... */) {
while (true) { break label; }
}
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3377.pdf proposes to
update this syntax to
for label (/* ... */) {
while (true) { break label; }
}
While N3377 may be a bit more syntactically sound (not reusing labels
that are otherwise only goto targets, I'm not a fan of that idea.
Putting "label:" before a loop has a lot of precedent in other
languages (e.g. Java). Assuming that N3377 gets rejected, the syntax
would also be a good fit for C++.
There is no good solution for breaking out of nested loops, or out of
a switch statement within a loop:
- You can put the whole thing into a function and rely on "return",
but that may decrease readability and add the cost of a whole new
separate function.
- You can use an IILE (immediately invoked lambda expression), but
that increases indentation level, makes a new scope, and overall adds
unwarranted complexity.
- You can use "goto after_loop;" with some label, but goto does not
work in constant expressions, is unconditionally banned from some
style guides, and overall won't be the future.
- You can use an extra bool to indicate whether the outer loop should
continue, but this adds more mutable state to your function and
doesn't express the idea of "just break out of both loops" clearly.
Every alternative sucks, and most general purpose languages provide
some way of breaking out of multiple loops. C++ is really behind and
should follow in C's footsteps, with whatever syntax C2y lands on.
Note that "break label" has been proposed most recently in
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3879.pdf
(was rejected), but this is because the author stupidly bunched the
popular "break label" idea together with unpopular, novel ideas, such
as "goto case-label" within switches, and the whole thing was thrown
out.
Since then, IIRC there have also been other proposals like "break N"
for breaking out of N nested loops, but all of those were rejected.
Anyhow, my questions for std-proposal are mainly:
- Is anyone already working on this?
- Is anyone interested in co-authoring?
I have no doubt in my mind that a small, simple, proposal which just
brings the syntax from C2y into C++ will pass, and we might even make
it into C++26 if we act swiftly.
Yours
Jan Schultke
Received on 2024-12-18 10:50:05