Inability to dup/~ for const arrays of class objects
Jakob Ovrum
jakobovrum at gmail.com
Wed May 29 05:23:02 PDT 2013
On Wednesday, 29 May 2013 at 09:40:08 UTC, monarch_dodra wrote:
> whereas D's means "This object's value WILL remain constant,
> until the end of time, and under no circumstance can it ever be
> modified. Oh. And so will everything it references".
That's NOT what D's const means! The data may change through
other, mutable references to it, but not through any const
references. That's why const data is not implicitly convertible
to immutable data (unless it has no mutable indirection, in which
case it would be a deep copy) - there could be mutable references
out there!
The difference between C++ and D const is that D's const is
transitive, and mutating it by casting away const can have
terrible consequences because const is designed to work with
immutable.
> The fact that *you* are promising to not change it is
> irrelevant to the concept of D's const. D only knows 2 states:
> objects that mutate, and objects that don't mutate.
No.
> *Once you've casted something to const, make sure that NOTHING
> will modify the object via a non-const handle.
This is *perfectly fine* unless the non-const handle was acquired
by casting away const or immutable.
More information about the Digitalmars-d
mailing list