Thread overview
pass by value && elide dtor + post-blit
Jun 20, 2015
Xiaoxi
Jun 20, 2015
Ali Çehreli
Jun 21, 2015
Xiaoxi
Jun 21, 2015
Namespace
June 20, 2015
When passing a struct by value:

Is there any way to trick the compiler to elide unnecessary post-blit & dtor pair?

Maybe using an union, somehow?

June 20, 2015
On 06/20/2015 02:09 PM, Xiaoxi wrote:
> When passing a struct by value:
>
> Is there any way to trick the compiler to elide unnecessary post-blit &
> dtor pair?
>
> Maybe using an union, somehow?
>

Can you show with an example please. I don't see either of those called for the following program:

import std.stdio;

struct S
{
    this(int)
    {
        writeln(__FUNCTION__);
    }

    this(this)
    {
        writeln(__FUNCTION__);
    }

    ~this()
    {
        writeln(__FUNCTION__);
    }
}

S foo()
{
    auto s = S(42);
    return s;
}

void main()
{
    writeln("before");
    auto s = foo();
    writeln("after");
}

The output:

before
deneme.S.this
after
deneme.S.~this

Ali

June 21, 2015
On Saturday, 20 June 2015 at 22:44:17 UTC, Ali Çehreli wrote:
> On 06/20/2015 02:09 PM, Xiaoxi wrote:
> The output:
>
> before
> deneme.S.this
> after
> deneme.S.~this
>
> Ali

Dear Ali,

thank you for helping! Problem happens when passing by value as in param.

DMD32 D Compiler v2.067.0
deneme.S.this
before
deneme.S.__postblit
deneme.S.__postblit
deneme.S.~this
deneme.S.~this
after
deneme.S.~this

import std.stdio;

struct S
{
    int val;
    this(int par)
    {
        val = par;
        writeln(__FUNCTION__);
    }

    this(this)
    {
        writeln(__FUNCTION__);
    }

    ~this()
    {
        writeln(__FUNCTION__);
    }
}

S foo(S s)
{
    s.val+=1;
    return s;
}

void main()
{
    auto s = S(42);
    writeln("before");
    foo(s);
    writeln("after");
}


June 21, 2015
> Dear Ali,
>
> thank you for helping! Problem happens when passing by value as in param.

Change 'foo' to this:
----
ref S foo(ref S s)
{
    s.val+=1;
    return s;
}
----