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.
wahern · · focus · HN ↗
That's the epitome of the hidden code downside that Linus and many others dislike about C++. For constructors and destructors it's somewhat unavoidable and not so random, though Rust does better at limiting the blast radius of non-local code, at least in the drop case.
If they didn't want to adopt the C11 rule, the C++ committee should've explored a rule that required the compiler to emit a diagnostic or error for trivial loops (whether as defined by C11 or otherwise), requiring the programmer to explicitly insert ::yield or similar. No hidden code, and less opportunity for the compiler to do surprising things.
The C committee has been rigorously enumerating UB cases in the standard and addressing each case in turn, often by requiring a diagnostic, error, or by turning it into implemention defined behavior. But inserting code like that would be unthinkable.
kevin_thibedeau · · focus · HN ↗
Empty infinite loops are also commonplace in embedded C once main is done with init and within exception handlers. They don't care about anything beyond their narrow systems programming worldview.
Sniffnoy · · focus · HN ↗
Could you elaborate on this?
aw1621107 · · focus · HN ↗
> volatile external modifications are only truly meaningful for loads and stores. Other read-modify-write operations imply touching the volatile object more than once per byte because that’s fundamentally how hardware works. Even atomic instructions (remember: volatile isn’t atomic) need to read and write a memory location []. These RMW operations are therefore misleading and should be spelled out as separate read ; modify ; write, or use volatile atomic operations which we discuss below.
This was not received particularly well in the embedded community (e.g., [1]) due to said deprecation affecting compound bitwise operations on volatile variables, which are extremely widely used to interact with hardware registers. This pushback eventually resulted in C++23 un-deprecating compound bitwise operators on volatile variables [2].
[0]: <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1152r0.html" rel="nofollow">https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p11...
[1]: <a href="https://www.reddit.com/r/cpp/comments/jswz3z/compound_assignment_to_volatile_must_be/" rel="nofollow">https://www.reddit.com/r/cpp/comments/jswz3z/compound_assign...
[2]: <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2327r1.pdf" rel="nofollow">https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p23...
tialaramex · · focus · HN ↗
It still is a bad idea, but being warned would make them feel bad.