rvalue references

Diggory diggsey at googlemail.com
Wed Apr 24 03:27:23 PDT 2013


> r-values can NEVER be accepted by a ref argument, they don't 
> 'exist', we're
> talking about implicitly generated temporaries (which also 
> addresses the
> issues with receiving _explicit_ temp's).
They may be temporary but why should that mean they don't exist. 
(wow that's disturbingly close to a harry potter quote)

Currently temporaries exist as long as they are required to 
exist, and their lifetime never exceeds one statement.

> If I were to start arguing upon the basis of your proposal, I 
> would then
> start arguing that auto-ref in this context is pointless, and 
> automatic
> creation of a temp to hold any r-value should be the 
> universal/default
> behaviour.

I believe with the exception of literals (which are probably 
implemented as global immutable variables) temporaries are 
already whenever an rvalue is used...

>> Why does the temporary need to exist any longer than the 
>> current
>> statement? (the current lifetime of temporaries are the 
>> statement or
>> expression). Surely any longer is just wasting stack space.
> 
> scope ref a(scope ref int x) { return x; }
> void b(scope ref int x);
> 
> b(a(10));
> 
> The temp produced from '10' needs to last longer than a(), 
> because it can
> be returned to b(). Ie, the temp needs the life of any normal 
> local
> declared within that scope.
> 
> How do you describe 'wasting' stack space? Does D attempt to 
> recycle stack
> space from previous short-lived locals within a single function?

I said "Why does the temporary need to exist any longer than the 
current statement?". In your example the temporary does not exist 
longer than the current statement, (the current statement in this 
example being "b(a(10))" since that is the context in which the 
temporary would be created)

I would hope that D reuses the stack space it uses for 
temporaries, certainly any decent C++ compiler would...

> The important distinction is that with scope ref, a temps life 
> can be
> prolonged and cascaded via return values.
That's simply not true, even if you allow passing rvalues as 
normal "ref", as long as DIP25A is implemented. The longest a 
temp ever needs to exist is until the end of the statement, with 
or without rvalue references. This means there is no issue with 
conditionals.

Anyway, it seems in general that everyone thinks DIP25A should be 
implemented, or am I mistaken?


More information about the Digitalmars-d mailing list