3 ms·
> no, `const` means you can't modify the data through that `const` variable (excepting shenanigans), not that it's immutable. But that's the whole point, isn't
by fivea 5y ago
> no, `const` means you can't modify the data through that `const` variable (excepting shenanigans), not that it's immutable.
But that's the whole point, isn't it? I mean the selling point of these lifetime annotations is to allow developers to specify that data within a thread cannot be modified by the code running in that thread.
Isn't that exactly what const does?
- flqn 5y agoNo, const can be added to objects at any declaration or callsite, meaning there can be many const- and non-const references to the same object within the same scope/thread/program/address space/execution context. Const doesn't solve aliasing.
- fivea 5y agoI think you are discussing different things, and in the process missing the whole point. It's one thing for an object to be immutable throughout is life cycle. That's immaterial to this discussion. It's an entirely different thing that the same object cannot be changed within specific contexts. If you want to ensure that a thread has read access to an object and it cannot be changed accidentally then passing a const reference to that object already ensures that. That's pretty much the whole point of const.
- flqn 5y agoI think you are missing that const is insufficient for safe concurrency; it requires programmer discipline to ensure there are no shared mutable references. Const also has no bearing on lifetimes. Constexpr/consteval do, but those objects always have static lifetime so it's sort of irrelevant.