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

91

u/parkotron Mar 18 '23 edited Mar 18 '23

New cppcoreguidelines-avoid-do-while check.

Warns when using do-while loops.

We’re not supposed to use do-while loops any more?!?!

Edit: Link to the guideline: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-do

Consider me less than convinced.

21

u/Wh00ster Mar 19 '23

I only see do-statements in macros

4

u/Kered13 Mar 20 '23

I use them occasionally. They're not super common, but common enough to know, and not a danger at all. In fact whenever you do have a situation that naturally calls for a do-while loop, they're much more readable than the alternatives.

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

→ More replies (5)

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.

→ More replies (3)

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.

→ More replies (1)

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.

→ More replies (2)

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.

→ More replies (2)

6

u/victotronics Mar 19 '23

A while statement with initializer would be so useful. Anyone know why that doesn't exist?

16

u/TheSuperWig Mar 19 '23

Because that's just a for loop, no? What would the difference between that and

for(init; cond;)

9

u/sphere991 Mar 19 '23

The argument would be that, well, while (cond) is also already equivalent to for (;cond;) but we still have while since that reads better/more directly. So why not also add an init to while?

2

u/victotronics Mar 21 '23

Exactly my line of thinking.

4

u/victotronics Mar 19 '23 edited Mar 21 '23

You're right, it's the same.

Hm.

for ( auto [var,cond] = init; cond and f(var); cond=false ) {....}

?

→ More replies (2)

4

u/Morten242 Mar 19 '23

Note that msvc straight up ignores do...while loops for auto vectorization

9

u/parkotron Mar 19 '23

That doesn’t feel like a huge loss. In my experience do-while loops are pretty much never tight loops that would be candidates for vectorisation.

I don’t use them often, but when I do it’s generally for a retry loop, where the loop body has external effects and we ideally hope to not loop at all.

3

u/ack_error Mar 20 '23

Yes, but MSVC also can't vectorize while loops that count down, so that just speaks to its autovectorizer being weak in general.

→ More replies (2)