returning struct, destructor
evilrat via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 21 06:31:31 PST 2016
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 with named return value optimisation.
looks like it is really optimized away. an "improved" test case,
even with "if" block removed or changed to true now it always the
same number of operations
==========================================
import std.stdio;
import std.conv : to;
struct A {
int num;
this(int n)
{
num = n;
}
this(const ref A other)
{
num = -1; // special case
writeln("copy of " ~ to!string(other.num));
}
~this() {
writeln("Destruct" ~ to!string(num));
}
}
A myFunc() {
auto a = A(1), b = A(2);
if (false) {
return A(a);
}
return A(b);
}
void main() {
myFunc();
}
More information about the Digitalmars-d-learn
mailing list