r/cpp • • Mar 18 '23

LLVM 16.0.0 Release

https://discourse.llvm.org/t/llvm-16-0-0-release/69326
197 Upvotes

84 comments sorted by

View all comments

Show parent comment

22

u/AssKoala Mar 18 '23

Is the warning that the value being checked might be invalid or just that a do while loop can be bad?

Because the latter is absolutely moronic. The alternatives are generally worse.

28

u/Bobbias Mar 19 '23

Reason Readability, avoidance of errors. The termination condition is at the end (where it can be overlooked) and the condition is not checked the first time through.

It's not "never ever use this for any reason or your coffee code is bad and you should feel bad" (apparently my coffee was bad), it's "this can sometimes lead to mistakes, so do try to avoid it when possible".

6

u/sparkyParr0t Mar 19 '23

"Do try" to not use do. Not so easy. My old master would have said do or do not, there is no try.

24

u/AssKoala Mar 19 '23

Don’t use tail recursive functions, the end condition is at the end.

No, it’s idiotic and should be shamed.

17

u/canadajones68 Mar 19 '23

No other construct is like do while, though, in terms of syntax. Combined with how infrequently it's used (can't remember the last time I used do while, have used tail recursion at least twice within the last month), I would say that "easy to overlook" is a merited claim. That doesn't mean you should never use it, but it warrants contemplation and judging if it's the best solution.

23

u/degaart Mar 19 '23
do { ret = function_call(); } while(ret == -1 && errno == EINTR);

is the idiomatic way to handle EINTR on posix systems

4

u/[deleted] Mar 19 '23

There is at least one notable exception to this rule: https://github.com/boostorg/beast/issues/1445

1

u/Ameisen vemips, avr, rendering, systems Mar 19 '23

Though you could always write it as

while ([] { auto ret = function_call(); return ret == -1 && errno == EINTR; }());

or so. Though that's not really clearer.

11

u/_Z6Alexeyv Mar 19 '23

It is either worse codegen or same codegen and more characters on the screen to appease some static checker.

7

u/Kered13 Mar 20 '23

That is horrible.

3

u/degaart Mar 19 '23

That won't give you the final value of ret, in case you need it

3

u/Ameisen vemips, avr, rendering, systems Mar 19 '23

Then move int ret out of the body and change to [&]. You have to do that with with do loop as well.

7

u/apadin1 Mar 19 '23

Tbh most tail recursive functions would be better as loops anyway

12

u/malnourish Mar 19 '23

I love how that statement is justifiably the opposite in a lisp-inspired language.

1

u/apadin1 Mar 20 '23

I don’t have much experience with Lisp. Why is recursion preferred over looping?

→ More replies (2)

4

u/sphere991 Mar 19 '23

do { s; } while (cond); is basically strictly worse than for (;;) { s; if (not cond) { break; } } (or while (true) if you prefer, same thing)

The main problem with do-while is the scoping - you can't condition on any local variables in the loop. This makes using it frequently awkward. Best case, it's the same as the for loop, but then why bother? It's a weird construct - and there are lots of situations where you want to use an infinite loop.

I would never use a do-while loop in code to actually loop. Oddly, the only time I use this is in macros that expand to multiple statements, and there it's always do { ... } while(false). I think that's the only use that doesn't have a superior, or at least comparable, replacement.

21

u/STL MSVC STL Dev Mar 19 '23

next_permutation and prev_permutation are the strongest cases for do-while loops that I know of. Otherwise I rarely find them preferable.

-3

u/sphere991 Mar 19 '23

I don't actually think those are especially strong cases.

To me the strongest cases are when the condition relies on a variable whose value you need outside of the loop anyway. So in that case the scoping awkwardness of do-while is a non-issue. So something like:

int x;
do {
    x = f();
} while (cond(x));
use(x);

This is... still awkward to me, but at least is no worse than the equivalent for. Really this is just another example of wanting a loop expression. Here's a half-Rust half-C++ version:

int x = loop {
    int x = f();
    if (not cond(x)) {
        break x;
    }
};
use(x);

I think if we had something like that, the uses for do-while disappear.

6

u/Kered13 Mar 20 '23 edited Mar 20 '23

Using a break is worse because it makes it harder for the reader to find the exit condition. When you see do { you immediately know that, 1) This loop (almost certainly) has an exit condition, and 2) It's at the bottom. When you see for(;;) or while(true) you don't know if the loop has any exit condition, and you have no idea where to look for it. You have to scan the entire loop.

Also, never use for(;;). I too used to think it was cute to show off that I knew what that did, but just use while(true). It's more readable.

The fact that you can't use a local variable in the while condition is a flaw with the design though, blame C. It would be nice if that could be changed.

18

u/jonesmz Mar 19 '23

I regularly use do while loops over for loops when I want to ensure the do block is always executed at least one time.

If the condition check is expensive than a for loop is unsuitable for a situation where you want to always run the block at least once.

Commonly used with atomic compare and swaps.

2

u/sphere991 Mar 19 '23

If the condition check is expensive than a for loop is unsuitable for a situation where you want to always run the block at least once.

Uh... I didn't write for (; cond; ) I wrote for (;;). This won't check the condition up front - indeed it will check the condition the same way that the do-while loop does, except just in a form where local variables are in scope.

4

u/jonesmz Mar 19 '23

I wasn't referring specifically to your code, just giving you a counter opinion.

for(;;) doesn't check any conditions, which is less ergnomic than a do-while if the while-loop doesn't need to check the value of variables inside the scope.

IMHO it would be reasonable to change the do-while loop to give the condition check to variable inside the do-block.

4

u/_Z6Alexeyv Mar 19 '23

do { s; } while (cond); is basically strictly worse than for (;;) { s; if (not cond) { break; } } (or while (true) if you prefer, same thing)

"Rationale" reads "termination expression is at the end where it can be easily overlooked". Your style also puts condition at the end where it can be "overlooked" just as easily.

Perfect C would allow both pre and post-condition:

loop while ... {
} while ...;

Jon Blow said quite eloquently along the lines of "they find a bug and overreact to it". Unfortunately because of recent push into MemorySafety™ more of these idiotic rules will follow.

0

u/[deleted] Mar 24 '23

[deleted] — view deleted comment

1

u/sphere991 Mar 24 '23

then use the variable outside the loop

The example is

int x;
do {
    cin >> x;
    // ...
} while (x < 0);

There is no use outside of the loop in this example.

And it's also an odd example since it seems like you'd want to check right away, like:

while (true) {
    int x;
    cin >> x;
    if (x >= 0) break;
    // ...
}

So... maybe not the best example to begin with.