www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - struct destructors

reply "Janice Caron" <caron800 googlemail.com> writes:
I see that structs now have destructors, according to the changelog for D2.012.

How does that work? I wasn't even aware that they had /con/structors!
I've still been using static opCall for that (and hating doing so,
because it's such a kludge).

So do we get to use this() and ~this() now? Can we use new and delete
with structs? Can we use delete with structs on the stack?

Also, what about assignment and copy construction?

...and repeatedly returning a struct from a function (as in, e() calls
f(), which calls g(), which calls h(); h returns a struct, which
returns it to g, which returns it to f, which returns it to e). Will
those nested returns invoke a chain of copy constructors and
destructors? Or will it be optimised to just a single copy?

That's a lot of questions for one post. Sorry about that.

Oh - one more. What's a postblit?
Mar 07 2008
parent reply John C <johnch_atms hotmail.com> writes:
Janice Caron wrote:
 I see that structs now have destructors, according to the changelog for D2.012.
 
 How does that work? I wasn't even aware that they had /con/structors!
 I've still been using static opCall for that (and hating doing so,
 because it's such a kludge).
We don't, as yet. Walter's announcement says: "Yes, you read that right, struct destructors but no struct constructors yet."
 
 So do we get to use this() and ~this() now? Can we use new and delete
 with structs? Can we use delete with structs on the stack?
 
 Also, what about assignment and copy construction?
 
 ...and repeatedly returning a struct from a function (as in, e() calls
 f(), which calls g(), which calls h(); h returns a struct, which
 returns it to g, which returns it to f, which returns it to e). Will
 those nested returns invoke a chain of copy constructors and
 destructors? Or will it be optimised to just a single copy?
 
 That's a lot of questions for one post. Sorry about that.
 
 Oh - one more. What's a postblit?
http://www.digitalmars.com/d/2.0/struct.html
Mar 07 2008
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 07/03/2008, John C <johnch_atms hotmail.com> wrote:
  > Oh - one more. What's a postblit?

 http://www.digitalmars.com/d/2.0/struct.html
Gosh. But isn't the opAssign example wrong? It says: S* opAssign(ref const S s) { a = s.a; return this; } but I rather think it should be S* opAssign(ref const S s) { a[] = s.a[]; return this; } since you don't /want/ this to share the same workspace as s. Am I missing something, or is this a documentation bug?
Mar 07 2008
parent reply John C <johnch_atms hotmail.com> writes:
Janice Caron wrote:
 Gosh. But isn't the opAssign example wrong? It says:
 
     S* opAssign(ref const S s)
     {
         a = s.a;
         return this;
     }
 
 but I rather think it should be
 
     S* opAssign(ref const S s)
     {
         a[] = s.a[];
         return this;
     }
 
 since you don't /want/ this to share the same workspace as s. Am I
 missing something, or is this a documentation bug?
In the example, 'a' refers to an int. So no.
Mar 07 2008
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 07/03/2008, John C <johnch_atms hotmail.com> wrote:
 In the example, 'a' refers to an int. So no.
So it does. I was confusing it with the previous example. Thanks for the correction.
Mar 07 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Janice Caron wrote:
 On 07/03/2008, John C <johnch_atms hotmail.com> wrote:
 In the example, 'a' refers to an int. So no.
So it does. I was confusing it with the previous example. Thanks for the correction.
But this looks wrong """ Struct assignment t=s is defined to be semantically equivalent to: t = S.opAssign(s); """ Shouldn't that be t.opAssign(s)? --bb
Mar 07 2008
parent Robert Fraser <fraserofthenight gmail.com> writes:
Bill Baxter wrote:
 Janice Caron wrote:
 On 07/03/2008, John C <johnch_atms hotmail.com> wrote:
 In the example, 'a' refers to an int. So no.
So it does. I was confusing it with the previous example. Thanks for the correction.
But this looks wrong """ Struct assignment t=s is defined to be semantically equivalent to: t = S.opAssign(s); """ Shouldn't that be t.opAssign(s)? --bb
So it should.
Mar 07 2008