Transitive const sucks
Bruce Adams
tortoise_74 at yeah.who.co.uk
Wed Sep 12 12:11:29 PDT 2007
Janice Caron Wrote:
> > I suspect he meant "copy on write", but forget to tell us. :-)
>
> Here's an example of copy-on-write:
>
> MyString s = "cat";
> MyString t = s; /* Only a reference is copied. Now s and t both
> reference the same data */
> writefln(t); /* prints "cat" */
>
> /*drum roll*/
> t[0] = 'b'; /* Only now is a copy made. Now t is unique, with s=="cat"
> and t="bat" */
> writefln(s); /* prints "cat" */
> writefln(t); /* prints "bat" */
>
> Of course, to implement this - to actually create a class that did
> this, you'd need MyString to have some internal state (and therefore,
> either non-transitive const, or logical const).
If you are living in the wonderful world of unix. Copy on write automagically happens for you if you change a value, following a process fork, courtesy of the OS. That's not to say it not a useful technique outside of forking unix processes but the point is that it can happen without any kind of const thrown into the mix.
I suspect the restrict keyword interacts with this in interesting ways.
Fortunately I've never had to try it myself.
Bruce.
More information about the Digitalmars-d
mailing list