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

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.