Writing const-correct code in D
Brad Roberts
braddr at puremagic.com
Wed Mar 8 18:59:38 PST 2006
On Thu, 9 Mar 2006, Kevin Bealer wrote:
> The same is true for struct, it gets copied in, which is fine for
> small structs. For larger structs, you might want to pass by "in *",
> i.e. use "in Foo *". You can modify this technique to use struct, for
> that see the last item in the numbered list at the end.
>
>
> For classes, the issue is that the pointer will not be modified with
> the "in" convention, but the values in the class may be.
>
> : this(const_Foo b)
> : {
> : x1 = b.x1.dup;
> : }
I must have missed something somewhere along the way.. when did copying
imply const? To me, something that's const can't be modified. That
doesn't mean just to the caller, but also to the callee. It's a mechanism
for saying "this object shouldn't be changed". Const by duplication
doesn't help with the last part and make it entirely probable that code
will at some point be change to modify parts of the passed in data with
the expectation that those changes actually occur on up through to the
caller.
Sorry, const by dup is in some ways even worse than not having const. I
see how it solves some usecases though, so it's not totally worse. :)
Later,
Brad
More information about the Digitalmars-d
mailing list