this() const

Jonathan M Davis jmdavisProg at gmx.com
Sun Apr 15 17:40:59 PDT 2012


On Sunday, April 15, 2012 21:20:23 sclytrack wrote:
> 	this( const size_t step) const
> 	{
> 		this.step = step;
> 	}
> 
> 
> Error: cannot modify const/immutable/inout expression this.step
> 
> 
> Is this the expected behavior? Thanks.

const and immutable postblit constructors don't really work right. Even if you 
get the constructor itself to work, other stuff stops working IIRC (it's been a 
while since I tried, so I don't remember all of the details). If you think 
about what a postblit is doing, a const or immutable postblit constructor 
doesn't work at all. It memcpy's the struct, and then it assigns the variables 
new values. Since they're const or immutable if the postblit is const or 
immutable, you can't assign to them. But of course, there are cases where you 
need a const or immutable postblit constructor, so this is a definite problem.

Supposedly, Walter and Andrei have a plan on how to fix this, but they have yet 
to share it with anyone, and it definitely hasn't been implemented yet.

In the interim, I'd be _very_ leery about using const or immutable postblit 
constructors. What you're doing really shouldn't compile, so if it does like 
Simon's post suggests, then I believe that that's a bug. And even if it _does_ 
work, other stuff tends to go wrong (with opAssign? -  I don't recall exactly 
what unfortunately). Maybe we should push Walter and Andrei on this again, 
since it's a been a while since it's come up.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list