[Issue 19793] no postblit is called if cast is used for structs

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 24 09:08:39 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=19793

--- Comment #5 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to RazvanN from comment #3)
> (In reply to Илья Ярошенко from comment #0)
> > struct S(T)
> > {
> >     size_t* counter;
> > 
> >     this(this)
> >     {
> >         if (counter)
> >             ++counter[0];
> >     }
> > 
> >     ~this()
> >     {
> >         if (counter)
> >             --counter[0];
> >     }
> >     
> >     static S create()
> >     {
> >         return S(new size_t(1));
> >     }
> > }
> > 
> > auto constOf(S!int a)
> > {
> >     return cast(S!(const int)) a;
> > }
> > 
> > void main()
> > {
> > 	auto s = S!int.create;
> >     assert(s.counter[0] == 1); // pass
> >     auto a = constOf(s);
> >     assert(s.counter[0] == 2); // fails
> > }
> 
> Up untile recently, casts were considered rvalues in dmd while postblits get
> called solely on lvalues. However this was recently changed in the specs [1]
> so that `cast()` and `cast(qualifier)` are lvalues. A PR was submitted to
> get the
> implementation in sync with the spec [2], so I suspect that that will also
> fix this issue.
> 
> [1] https://github.com/dlang/dlang.org/pull/2606
> [2] https://github.com/dlang/dmd/pull/9505

I didn't notice that the cast is done to a different template instantiation:
from S!int to S!(const int); does are different types that are not implicitly
convertible one to the other so the cast in this case will remain an rvalue.
My PR will not fix this and this is an invalid issue according to the spec.

Now that copy constructors are in the master branch, I suggest that you
stop using the postblit.

--


More information about the Digitalmars-d-bugs mailing list