Scope of temporaries as function arguments

monarch_dodra monarchdodra at gmail.com
Fri Jun 28 08:12:00 PDT 2013


On Friday, 28 June 2013 at 14:26:04 UTC, Maxim Fomin wrote:
> 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.

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?


More information about the Digitalmars-d-learn mailing list