C++26: Trivial infinite loops are no longer undefined behaviour
Thread
Unofficial Hacker News client; not affiliated with Y Combinator.
C++26: Trivial infinite loops are no longer undefined behaviour
Unofficial Hacker News client; not affiliated with Y Combinator.
account42 · · focus · HN ↗
echoangle · · focus · HN ↗
> What I found is that this is common in embedded and kernel code as a halt-on-error pattern. When a fatal error occurs and there’s no operating system to exit to, you simply stop:
account42 · · focus · HN ↗
mdspan · · focus · HN ↗
rcxdude · · focus · HN ↗
echoangle · · focus · HN ↗
account42 · · focus · HN ↗
echoangle · · focus · HN ↗
pdonis · · focus · HN ↗
sigbottle · · focus · HN ↗
gpderetta · · focus · HN ↗
weinzierl · · focus · HN ↗
kibwen · · focus · HN ↗
Interestingly, this all suggest that, in C-like languages, the more natural implementation of an infinite loop should not be `while(true)` nor `for(;;)`, but rather `do {} while(true)`, because do-while are also guaranteed to enter their body (and note that Rust doesn't feature do-while loops).
tialaramex · · focus · HN ↗
weinzierl · · focus · HN ↗
tialaramex · · focus · HN ↗
This answers the question Matt Godbolt had which caused him to create what would become Compiler Explorer, is a fancy modern for-each loop able to deliver the same perf as my 1970s loop? In Rust the answer is necessarily "Yes" because by the time the backend sees your program they're the same thing.
The reason Matt wanted to know is that obviously a for-each loop often has better ergonomics, so if they mean the same thing we should prefer our team to write this - but if they're slower that's a tough question, should we trade performance for clarity? The "Yes" answer that Matt found for C++ and which is baked into Rust means you don't need to make that trade decision, write whatever is easier to understand and maintain.
account42 · · focus · HN ↗
sumtechguy · · focus · HN ↗
That seems to be a very broad statement. For example in a system where interrupts mostly control things this sort of 'do not close the program' could be useful.
A guy I worked with had one I never would think of because I do not work in that field.
But yeah a warning would probably be useful.
not_the_fda · · focus · HN ↗
mdspan · · focus · HN ↗
cppcppcpp · · focus · HN ↗
tialaramex · · focus · HN ↗
anticensor · · focus · HN ↗