Thread overview
struct destructors
Mar 07, 2008
Janice Caron
Mar 07, 2008
John C
Mar 07, 2008
Janice Caron
Mar 07, 2008
John C
Mar 07, 2008
Janice Caron
Mar 07, 2008
Bill Baxter
Mar 07, 2008
Robert Fraser
March 07, 2008
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?
March 07, 2008
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
March 07, 2008
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?
March 07, 2008
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.
March 07, 2008
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.
March 07, 2008
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
March 07, 2008
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.