returning struct, destructor

Eugene Wissner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 21 21:11:11 PST 2016


On Wednesday, 21 December 2016 at 17:49:22 UTC, kinke wrote:
> Basic stuff such as this is appropriately tested. The named 
> return value optimization is enforced by D (incl. unoptimized 
> builds), so behavior doesn't change by this optimization. It's 
> you who changed the behavior by removing the if. Due to the 
> `if`, the compiler doesn't know whether it should construct `a` 
> or `b` directly into the memory (sret pointee) provided by the 
> caller. When omitting the `if`, it's clear that `b` is returned 
> in all cases, so the compiles constructs `a` on the local stack 
> (and destructs it before exiting the function), but emplaces 
> `b` into `*sret` (i.e., the caller's stack) and can thus elide 
> 1x postblit + 1x dtor.

Thanks a lot. It makes sense. It seemed just weired that a 
conditional return value causes such a change. But I begin to 
understand the background.


More information about the Digitalmars-d-learn mailing list