Surprise destructor call...

kinke noone at nowhere.com
Wed Aug 14 14:28:06 UTC 2024


On Wednesday, 14 August 2024 at 13:56:08 UTC, Manu wrote:
> Well, the condition is that NRVO should elide the copy/move... 
> it should be
> constructed at the caller's scope in this case.
> Your example `Thing(null).field` is not the same thing, because 
> it's not
> NRVO at all.
>
> I guess this is a bug then?

When actually using NRVO (`auto r = Thing(null); return r;`), one 
even gets the error *twice*. So that's definitely a bug, the 
destruction is handled by the caller; just for the attributes 
check etc., there's no real dtor call in `fun()`.

What you have is a case for RVO; AFAIK, LDC and GDC implement 
that (for non-POD types at least), no idea about DMD. Meaning 
that the temporary in the return expression isn't destructed by 
`foo` either; it's emplaced directly into the caller-allocated 
return value, as the NRVO case.


More information about the Digitalmars-d mailing list