Scope of temporaries as function arguments

Maxim Fomin maxim at maxim-fomin.ru
Fri Jun 28 07:26:02 PDT 2013


On Friday, 28 June 2013 at 08:08:17 UTC, monarch_dodra wrote:
>
> Just in case it wasn't clear from the original explanation, 
> this is a bug, it *should* be perfectly safe to pass as many 
> temps as you want, and expect the right amount of destructor 
> called in case of a throw.

Original explanation lacks the word "bug" deliberately because 
this is not a bug (in a sense that dmd generates wrong code), but 
a language design problem. How could you do this:

struct S
{
    int i = 1;
}

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

void main()
{
    S s;
    foo(s);
}

Currently there are two dtors, one which gets S(2) at the end of 
foo and second at the end of main, which gets S(1). If you move 
dtor from callee to caller, it would get S(1) object (struct is 
passed by value), but it doesn't make sense to destruct S(1) 
where you have S(2). One possible solution is to pass by pointer 
in low level, which would probably increase magnitude of problems.


More information about the Digitalmars-d-learn mailing list