const(X) member of Y
deadalnix
deadalnix at gmail.com
Wed Feb 6 21:38:32 PST 2013
On Wednesday, 6 February 2013 at 18:15:53 UTC, Dan wrote:
>
> Start with:
>
> struct X {
> char c[];
> }
>
> Assume you want value semantics - postblit provides this
> capability. It appears with 2.061 'this(this) const' is now
> supported. Previously, only 'this(this)' was recognized (i.e.
> called) when expected. To get value semantics a developer must
> choose between these two and for this case the goal is to
> ensure 'c = c.dup;' so value semantics are preserved.
>
> Case 1: Go with 'this(this) { c = c.dup; }'. This works just
> fine except for the case where you want a const(X) as a member
> of some other class. For example: struct Y { const(X) x; }.
>
> Case 2: Go with 'this(this) const { c = c.dup; }'. This is not
> possible because you can not change 'c' since the function is
> const. Maxim Fomin pointed out a workaround.
>
Both are bugs, as const constructor are supposed to be able to
set const variable once. See TDPL for reference on that.
> struct X {
> char c[];
> void _postblit_() { c = c.dup; }
> this(this) const {
> void delegate() dg = &_postblit_;
> dg();
> }
> }
>
This is also a bug as transitivity of const isn't respected.
Welcome in weirdland !
More information about the Digitalmars-d
mailing list