Struct lifetime wrt function return?
rsw0x via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun May 3 19:29:19 PDT 2015
I remember reading that guaranteed RVO was part of the D
standard, but I am completely unable to find anything on it in
the specification.
I'm also unable to find anything in it that explicitly states the
lifetime of returning a stack-local struct from a function.
However, it does state
>Destructors are called when an object goes out of scope.
So without guaranteed RVO I am quite confused.
I apologize because this code will likely be poorly formatted.
import std.stdio;
struct S{
~this(){
writeln("Goodbye!");
}
}
S foo(){
S s;
return s;
}
void main()
{
S s2 = foo();
}
This says "Goodbye!" exactly once, indicating(?) that S was
NRVO'd which means the scope of s went from foo to main.
However, is this a guarantee by the standard? Is an
implementation allowed to define foo such that it returns by copy
and calls a destructor on s, meaning "Goodbye!" would print out
twice?
More information about the Digitalmars-d-learn
mailing list