logoalt Hacker News

Sniffnoytoday at 2:46 AM1 replyview on HN

> They intentionally broke bitwise operators on volatiles because they wanted to be impose their atomic religion everywhere.

Could you elaborate on this?


Replies

aw1621107today at 4:12 AM

"broke" is arguably an overstatement. C++20 deprecated some (most?) operations on volatile variables [0] in part because they can misleadingly imply an atomic operation:

> 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]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p11...

[1]: https://www.reddit.com/r/cpp/comments/jswz3z/compound_assign...

[2]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p23...

show 1 reply