[Issue 8269] The 'with statement' does not observe temporary object lifetime

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Aug 9 04:18:42 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=8269

--- Comment #14 from yebblies <yebblies at gmail.com> ---
(In reply to Damian from comment #13)
> Why would the with statement be creating a temporary in the first place? It
> seems terribly inefficient, surely it can just alias the original object and
> be done with it?

It does when it can, but it can't when the with argument is an rvalue.

eg

Struct generateStructForMe() { ... }

with(generateStructForMe())
{
   auto x = member1 + member2;
}

You want this to expand to (and in lvalue cases it will)

auto withptr = &generateStructForMe();
auto x = withptr.member1 + withptr.member2;

But this can't work, because the struct has to be stored somewhere before you
can take its address.

The struct literals in these examples are also rvalues and work the same way. 
Class references and struct pointers do not have this problem an can be copied
freely.

--


More information about the Digitalmars-d-bugs mailing list