Why is dtor called for lazy parameter?

Andrey Zherikov andrey.zherikov at gmail.com
Fri Sep 18 12:26:44 UTC 2020

On Friday, 18 September 2020 at 10:54:41 UTC, ikod wrote:
> On Friday, 18 September 2020 at 10:43:47 UTC, Andrey Zherikov 
> wrote:
>> I have this code:
>> ==========
>> class S
> ...
>> auto create()
>> {
>>     writeln("-> ",__PRETTY_FUNCTION__);
>>     scope(exit) writeln("<- ",__PRETTY_FUNCTION__);
>>     return scoped!S(1);
>> }
> If you replace this line with "return new S(1)" it should work 
> as you expect.

This works here, but doesn't meet my intention in main(). I want 
something to be called automatically as a final step when 
"create().do_lazy().do_something();" is done and I see two ways 
to do this: either use struct as return type (this unfortunately 
forces copy struct value for every function call) or use 
scoped!(class) - so dtor is called right after the expression.
In case of "new S(1)" dtor is called after exit from main().

More information about the Digitalmars-d-learn mailing list