Issue with const

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 9 10:59:52 PDT 2012


On Monday, April 09, 2012 19:51:17 Jacob Carlborg wrote:
> On 2012-04-09 17:30, Steven Schveighoffer wrote:
> > // untested
> > inout(Foo) clone() inout {
> > return new inout(Foo)(data);
> > }
> > 
> > Note, you can't post-assign data, since inout is effectively const
> > inside an inout function.
> > 
> > -Steve
> 
> Ok, that works. But then I want to modify the clone:
> 
> void bar (const Foo foo)
> {
> auto c = foo.clone();
> c.data = ...
> }
> 
> But now "c" is const. I don't want "c" to be const, I just want "foo" to
> be const.

I don't see how that's possible with a void* member variable. clone can be 
const as long as you actually deep copy all of foo's member variables. If 
there are any that you don't deep copy, then the fact that clone is const 
forces the return value to be const because of transivity. And with a void* 
member variable, I don't see how you could deep copy it, so the void* member 
variable is stuck as const, making the return value stuck as const.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list