Binding rvalues to const ref in D
Manu via Digitalmars-d
digitalmars-d at puremagic.com
Thu Oct 20 03:36:16 PDT 2016
On 20 October 2016 at 20:16, Ethan Watson via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:
> On Wednesday, 19 October 2016 at 21:19:03 UTC, Atila Neves wrote:
>
>> On Wednesday, 19 October 2016 at 15:58:23 UTC, Chris Wright wrote:
>>
>>> So it seems like the compiler could take care of this by only providing
>>> lvalue references but automatically creating those temporary variables for
>>> me. It's going to create an extra copy that might not be needed if there
>>> were a special rvalue reference type modifier, but why should I care? It's
>>> exactly as efficient as the code the compiler forces me to write.
>>>
>>> This is what Ethan Watson has suggested, too.
>>>
>>
>> Interesting. Also, I must have missed that suggestion.
>>
>
> It actually went a bit further than my suggestion, if I'm reading the
> summary correctly.
>
> For example, right now we go:
>
> Vector3 vSomeTempName = v1 + v2;
> someVectorFunc( vSomeTempName );
>
> This will keep the vSomeTempName entirely in scope and living on the stack
> for as long as that code block is active. A simplification step would be to
> store rvalues on the stack without needing to name them, and only
> destroying them once the block's scope goes out of scope.
>
> It still provides an easy escape from a C++ function though. For example:
>
> D code:
>
> return someVectorFunc( v1 + v2 );
>
> C++ code:
>
> const Vector3& someVectorFunc( const Vector3& someVector )
> {
> return someVector;
> }
>
> You'd still want to insert some sanity checking code in the code gen to
> throw an exception if the C++ function is returning a reference to the
> current stack and your D function is also returning by reference.
>
DIP25 introduced return ref to address this issue. Just annotate it
correctly?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20161020/a344cb6b/attachment.html>
More information about the Digitalmars-d
mailing list