Scope of temporaries as function arguments

monarch_dodra monarchdodra at gmail.com
Fri Jun 28 08:17:11 PDT 2013


On Friday, 28 June 2013 at 15:12:01 UTC, monarch_dodra wrote:
> On Friday, 28 June 2013 at 14:26:04 UTC, Maxim Fomin wrote:
>> [...]
>
> I don't understand the problem... There *should* be two 
> destroyers... "main.s" is postblitted into "foo.s", and then 
> foo destroys "foo.s" at the end of its scope...
>
> Where is the problem here?

--------
import std.stdio;

struct S
{
    int i = 0;
     this(int i){this.i = i; writeln("constructing: ", i);}
     this(this){writeln("postbliting: ", i);}
     ~this(){writeln("destroying: ", i);}
}

void foo(S s)
{
    s.i = 2;
}

void main()
{
    S s = S(1);
    foo(s);
}
--------
constructing: 1
postbliting: 1
destroying: 2
destroying: 1
--------

Should I have expected a different behavior?


More information about the Digitalmars-d-learn mailing list