Re: returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
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.

Re: returning struct, destructor

2016-12-21 Thread Ali Çehreli via Digitalmars-d-learn
On 12/21/2016 07:01 AM, Eugene Wissner wrote: > Isn't an optimization that changes the behavior bad? I had a crash in > the code where the destructor did something meaningfull, freed the > memory (the same pointer) twice. Sounds like you ended up with two objects that owned the same resource.

Re: returning struct, destructor

2016-12-21 Thread kinke via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 18:02:54 UTC, bauss wrote: It removes an unnecessary allocation for the returning copy of the struct, as the return value is never used. Hence why it's pointless that it would be compiled anyway. That's incorrect, it doesn't have anything to do with the

Re: returning struct, destructor

2016-12-21 Thread bauss via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 15:01:20 UTC, Eugene Wissner wrote: On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints

Re: returning struct, destructor

2016-12-21 Thread kinke via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 15:01:20 UTC, Eugene Wissner wrote: On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints

Re: returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints "Destruct" only 2 times - the behavior I'm expecting. Why? Possibly to do

Re: returning struct, destructor

2016-12-21 Thread evilrat via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints "Destruct" only 2 times - the behavior I'm expecting. Why? Possibly to do

Re: returning struct, destructor

2016-12-21 Thread John C via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints "Destruct" only 2 times - the behavior I'm expecting. Why? Possibly to do with named return value optimisation.

Re: returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 12:32:51 UTC, Nicholas Wilson wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: Consider we have a function that returns a struct. So for example: import std.stdio; struct A { ~this() { writeln("Destruct"); } } A

Re: returning struct, destructor

2016-12-21 Thread Nicholas Wilson via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: Consider we have a function that returns a struct. So for example: import std.stdio; struct A { ~this() { writeln("Destruct"); } } A myFunc() { auto a = A(), b = A(); if (false) { return a;

returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
Consider we have a function that returns a struct. So for example: import std.stdio; struct A { ~this() { writeln("Destruct"); } } A myFunc() { auto a = A(), b = A(); if (false) { return a; } return b; } void main() { myFunc(); } This prints 3