rvalue references

Jonathan M Davis jmdavisProg at gmx.com
Thu Apr 25 15:36:19 PDT 2013


On Wednesday, April 24, 2013 08:54:09 Zach the Mystic wrote:
> On Wednesday, 24 April 2013 at 02:56:42 UTC, Jonathan M Davis
> 
> wrote:
> > So, probably the best route at this point is to come up with a
> > new attribute
> > which is replace with ref in the function definition and
> 
> When you say "replace with ref", does that mean 'ref' won't
> appear in the common case?
> 
> ref T fc(scope int a) {}
> 
> The problem would be that 'scope' would behave differently,
> implying 'ref' with a value type, from with a reference type, in
> which it shouldn't imply 'ref'. I don't know if it makes sense to
> automatically promote the meaning of 'scope' to include 'ref' for
> value types while leaving it alone for reference types. This was
> objected to by others, but if it were provably harmless to allow,
> it would be an appearance improvement.

You misunderstand me (possibly because I didn't proofread what I wrote). What 
I mean is that the way that auto ref should work with non-templated functions 
is that

auto foo(auto ref T param) {...}

becomes

auto foo(ref T param) {...}

underneath the hood. Then when you pass an rvalue to it - e.g. foo(T(5)) - 
that gets translated to something like

T __temp = T(5);
foo(__temp);

Then auto ref works with both lvalues and rvalues with only one function 
definition, and ref is unchanged in how it works (it still only accepts 
lvalues).

The problem is that that's not how auto ref works with templated functions, 
and we need the way that it currently works with templated functions (as it 
improves D's forwarding capabilities), but we also really should have the 
ability to do auto ref with templated functions like I just described for non-
templated ones, since the templated approach results in a combinatorial 
explosion of template instantiations as you add more auto ref parameters. And 
obviously auto ref can't mean both for, so if we're going to have both, one of 
the two ends up needing a new attribute. So, either

1. auto ref on templates continues as-is (and is never implemented for non-
templated functions), and @newattr does what I just described for both 
templated and non-templated functions, or

2. auto ref is changed to mean what I just described, and @newattr is used to 
do the forwarding trick that auto ref is often currently used for with 
templated functions.

Nowhere in here am I suggesting that ref itself be treated differently, and 
nothing I'm describing really has anything to do with scope.

- Jonathan M Davis


More information about the Digitalmars-d mailing list