rvalue references

Manu turkeyman at gmail.com
Tue Apr 23 20:44:10 PDT 2013


On 24 April 2013 11:22, kenji hara <k.hara.pg at gmail.com> wrote:

> Temporaries which created on stack to pass an address to `scope ref`
> parameter would be destroyed after the "current statement/expression".
>
> T rvalue();
>
> void foo(scope ref T) { ... }
> foo(rvalue());   // here is "current statement/expression"
> // lowered to: { auto __tmp = rvalue(); foo(__tmp);  __tmp.~this(); };
>
> Note that the lifetime of taken rvalue is mostly same as non-ref parameter
> case.
> void bar(T arg) { ... arg.~this(); }
> bar(rvalue());
> // the rvalue argument is moved into the function 'bar', and destroyed
> inside function.
>

The important distinction is that with scope ref, a temps life can be
prolonged and cascaded via return values.
This is very important behaviour, I couldn't write an efficient&convenient
matrix library for instance without this, i would have to choose one or the
other.


> Kenji Hara
>
>
> 2013/4/24 Diggory <diggsey at googlemail.com>
>
>> On Wednesday, 24 April 2013 at 00:54:12 UTC, kenji hara wrote:
>>
>>> 2013/4/24 Manu <turkeyman at gmail.com>
>>>
>>>  "The r-value being passed is assigned to a stack allocated temporary,
>>>> which has a lifetime that is identical to any other local variable, ie,
>>>> the
>>>> lifetime of the function in which it appears."
>>>> There, I defined it.
>>>>
>>>>
>>> Good definition. If add more,
>>> "getting address of "scope" parameter would be disallowed, at least in
>>> @safe code, because it would be regarded as the escape of stack allocated
>>> temporary."
>>>
>>> Kenji Hara
>>>
>>
>> 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.
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130424/99913990/attachment.html>


More information about the Digitalmars-d mailing list