Struct d'tors and destructive assignment of return vals

dsimcha dsimcha at yahoo.com
Tue May 26 18:44:51 PDT 2009


== Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
> On Tue, 26 May 2009 21:20:41 -0400, dsimcha <dsimcha at yahoo.com> wrote:
> > import std.stdio;
> >
> > struct RC {
> >     uint N;
> >
> >     this(this) {
> >         writeln("Postblit:  ", N);
> >     }
> >
> >     ~this() {
> >         writeln("D'tor:  ", N);
> >     }
> > }
> >
> > RC fun() {
> >     writeln("Doing stuff...");
> >     return RC(3);
> > }
> >
> >
> > void main() {
> >     RC foo = RC(1);
> >     writeln("Calling fun()...");
> >     foo = fun();
> >     writeln("Exiting...");
> > }
> >
> > Output:
> >
> > Calling fun()...
> > Doing stuff...
> > D'tor:  1
> > Exiting...
> > D'tor:  3
> >
> > Would it be feasible to require that, when a struct is being
> > destructively
> > assigned the return value of a function, the d'tor is called for the old
> > contents before the function that provides the return value is called
> > instead
> > of calling it after?
> What if fun throws an exception?
> -Steve

Argh good point, didn't think of that.  Then I guess you're just screwed.  In that
case, do you see any other way to get good COW semantics in this situation?



More information about the Digitalmars-d mailing list