--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/nqrkga%24hgr%241%40blaine.gmane.org.
This is about exiting loops easily with just an optional number telling how many loops exit.
"break" already has a relative meaning, it is relative to current loop. This just extends to more than one, no need of labels otherwise we already have "goto".
On Thursday, September 8, 2016 at 2:39:49 PM UTC+2, Klaim - Joël Lamotte wrote:On 8 September 2016 at 14:30, <amg...@gmail.com> wrote:This is about exiting loops easily with just an optional number telling how many loops exit.
I remember that this option was discussed a lot because it cause the break to have a relative meaning, which does not work well with code that change.The discussion converged to a preference to a way to name the section of the code (either label or an alternativ) which is associated to the break.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5fbdd991-3b91-4ff1-ae2f-434dfa19534f%40isocpp.org.
On 8 September 2016 at 14:55, <amg...@gmail.com> wrote:"break" already has a relative meaning, it is relative to current loop. This just extends to more than one, no need of labels otherwise we already have "goto".If you use numbers to express how many scopes to break from, then it's a relative break. If you name the scope to exit from, it's not relative at all.
On Thursday, September 8, 2016 at 2:39:49 PM UTC+2, Klaim - Joël Lamotte wrote:On 8 September 2016 at 14:30, <amg...@gmail.com> wrote:This is about exiting loops easily with just an optional number telling how many loops exit.
I remember that this option was discussed a lot because it cause the break to have a relative meaning, which does not work well with code that change.The discussion converged to a preference to a way to name the section of the code (either label or an alternativ) which is associated to the break.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
One problem with this proposal is that it promotes bad code. Sean
Parent has the good advice to avoid raw loops. To provide facilities
that support multiple nested loops is a step in the wrong direction.
In the rare case where you can not split your nested loop into two or
more functions just use the goto statement which will also serve as a
reminder that this is an area of code where you have to be extra
careful if you change anything.
Problem is these cases are not so rare. Everything that is composed of more than 1 dimension, like images, databases, matrixes, etc, requieres 2 or more loops to apply most of algorithms to them ( usually everything composed of row and columns )
for example: to search for an element and abort when the condition is found. There is a lot of algorithms than needs to deal with mutliple dimensional objects, so this situation is a lot more frequent than expected.
Anyway this not about replacing goto, anyone can keep using it, though similar problems you describes applies to goto too:
If algorithm is modified or adjusted it may requiere the label to be moved, otherwise algorithm will fail aswell.
If I do break( 2 ) i know exactly how many loops I exit. If I do a goto, can be anywhere in the method, even in previous lines, so finding where label is located can be even harder.
You probably didn't realize that c++ break is already relative, it exits from the current loop level. Where it exits? relative to the size of the loop where you put the break. If the loop would have 1 thousand lines, it would jump at the end to that code length.
'break' behaviour is very useful, so this break(n) extension is not different.
And let's be clear and practical, how many nested loops do we have? 1, 2, ... 3? other than that we split in other method, so we are not going to use break( 17 ), aswell we can place one thousand goto's in a method but nobody does it.
break(n) is useful, clear, requieres no extra conditional check or define extra label like goto, and it is easy to implement by compiler. It can be translated with an internal goto, but transparent and safe, aswell it does with the usual jumps in conditionals "if/else", "for", "do / while", etc
And the most important, it is optional, you can use it or not.
Finding a `goto` label is done via a simple string search. A good IDE can give it to you by simply right-clicking and picking a menu-item, or with a keyboard shortcut. Finding where `break 2` goes require finding the two nearest loops, which may be off-screen.
2. The use of this feature encourages the writing of low-quality code.
Oh, and there's another thing that makes your idea problematic. It doesn't combine well with the proposal for a "for/else" mechanism. What would happen if you use `break(2)` and the inner loop has an `else` clause (or whatever we want to call it)? By limiting `break` to just one loop, we ensure that such a proposal still makes sense.
Finding a `goto` label is done via a simple string search. A good IDE can give it to you by simply right-clicking and picking a menu-item, or with a keyboard shortcut. Finding where `break 2` goes require finding the two nearest loops, which may be off-screen.
Well, this is going to a too much more complicated issue that it started. If you see my initial example, it is very simple, you do not need a 3 page code to make use of it. What I mean is, if the code containing the inner loops is complex enough you should split that code into another method containing the loops, and this way you solve both: the break will be replaced by a return, and the complexity of the method will decrease by splling it.
But, as my example, if code is small enough due to inner code loops are simple, you want a clean and fast exit. Splitting the method is discarded in this case, so you only can use the goto or adding extra conditions.
Oh, and there's another thing that makes your idea problematic. It doesn't combine well with the proposal for a "for/else" mechanism. What would happen if you use `break(2)` and the inner loop has an `else` clause (or whatever we want to call it)? By limiting `break` to just one loop, we ensure that such a proposal still makes sense.
I didn't know that proposal, but looks already incompatible by using goto inside isn't? My proposal isn't incompatible with anything because internally it can be translated to a goto by the compiler.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/2fc6d34d-84bc-408f-8051-10ef2f08d595%40isocpp.org.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAFk2RUbXO4nirwFzSwsMHeLk6w4KOk36Qo1VO6hYDfLGnuMJeQ%40mail.gmail.com.
Given my endless arguing in the order-of-evaluation threads, I have really come to hate the "promotes bad code" arguments. The proof that break Label; is good is that Ada has it. And by the way, have none of the "bad code" arguers ever had to exit a for loop from inside a switch statement?
Hi,
Note that this is essentially solved by the idea of 'capturing monads in lambdas'.
for (auto&& person : people)
{
auto break_twice = [&break]{ break; }
for (auto&& phone : person.phones)
{
if (isLandline(phone))
{
std::cout << person.name << " " << phone
<< " still uses landline, do not switch it off!"
<< std::endl;
break_twice();
}
}
}
for (auto&& person : people)
{
for (auto&& phone : person.phones)
{
if (isLandline(phone))
{
std::cout << person.name << " " << phone
<< " still uses landline, do not switch it off!"
<< std::endl;
goto break_twice;
}
}
} break_twice:
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/765ea407-3ab0-4cc9-8bf2-e59c168a7bd0%40isocpp.org.
for(;;)
{
switch(...)
{
case ...:
break(2); // exit the for loop
}
}
for(;;)
{
switch(...)
{
case ...:
goto out_of_for; // exit the for loop
}
}
As a side note, what about a "block" statement? I'd find this useful. It's basically a more concise mechanism than do {} while (false);
E.g.
block
{
DoSomethingUseful();
if (x == 0)
break;
DoSomethingElse();
if (y == 0)
break;
DoAnotherThing();
}
{
DoSomethingUseful();
if (x == 0)
break;
DoSomethingElse();
if (y == 0)
break;
DoAnotherThing();
}
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f7b4533f-62c2-46c3-9a2b-82da0075dd6c%40isocpp.org.
Break statements need to be within a loop or switch, right?
Break statements need to be within a loop or switch, right?
Yes, but you are trying to propose new feature. Aren't you?
I see what you are saying now... indeed the "block" statement is redundant, and such a feature would not introduce any breaking changes (pun intended).
while ( cond1 )
{
while ( cond2 )
{
if ( cond3 )
{
break, break;
}
else if ( cond4)
{
break, continue;
}
}
// code reached only if !cond2
}
while ( cond1 )
{
while ( cond2 )
{
if ( cond3 )
{
break, break;
}
else if ( cond4)
{
break, continue;
}
}
// code reached only if !cond2
}Why is it good:* We use the comma operator in its intuitive manner
* It doesn't make it comfortable to create many embedded loops* Allows to continue as the last operation (nice bonus)
I don't think that this is going to work.
Consider the following code, which is valid and obvious in every version
of C++.
while (more_stuff_to_do())
{
if (tired_of_doing_stuff())
{
if (boss_is_not_here())
break; // Where should control flow be transferred?
std::cout << "Okay, I'll keep going...\n";
}
}
And even if you invent a (presumably not very intuitive) rule for how
this case would be handled, you would still break *lots* of existing
code like this.
exit from the current block or loop (which body may not be a block, just one expression statement).
while (true)
{
std::unique_ptr<thing> mything{};
// Nested scope is used because of the RAII lock.
{
std::lock_guard lg{mymutex};
if (queue.empty())
break; // Intention is to break out of the while loop.
mything = queue.pop();
}
// Work with my thing...
}
while ( there are ppl wants to complicate things ){while( they want to define labels and write more than necessary ){break2; // make your life simpler}}
With regards to your solution 1 in the original OP of using a function and then calling return. You say it is elegant but slow. Because of the time you need to take to create a function? If that is such an issue why not just do an immediately invoked lambda expression which was one of Jason Turner's key points at boostcon:
[&](){
while ( cond1 )
{
while ( cond2 )
{
if ( cond3 )
{
}return;
}
}
}();
Really though, if you're going to have such complexity within your program, with it to be iterating through several loops, it would be better for you to separate it out into its own method or function function and give it a meaningful name. I've seen many talks from the likes of Herb Sutter, Sean Parent and Bjarne Stroustrup on the subject of reducing the complexity of your code and making it intuitive to read by dividing up the problem and giving each area of the code its own name. For these reasons, while I do see why you want break(n), solution 1 is probably the most acceptable way to handle this issue. Regardless of the extra time incurred in creating the new method/function. And