From slices to perfect imitators: opByValue

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu May 8 18:09:51 PDT 2014


On Thu, 08 May 2014 18:10:28 +0200
Timon Gehr via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> On 05/08/2014 06:02 PM, monarch_dodra wrote:
> >
> > If you have const data referencing mutable data, then yes, you can
> > cast away all the const you want, but at that point, it kind of
> > makes the whole "const" thing moot.
>
> This is not guaranteed to work. I guess the only related thing that
> is safe to do is casting away const, but then not modifying the
> memory.

Exactly. It's effectively illegal to cast away const and then mutate the
object. The compiler lets you do it, because D is a systems language, but the
compiler is free to assume that the object wasn't modified, so unless you know
what you're doing and are very, very careful, you're risking subtle bugs.
Really, casting away const and then mutating the now-mutable object is not
something that you should ever be doing.

- Jonathan M Davis


More information about the Digitalmars-d mailing list