The liabilities of binding rvalues to ref

Jonathan M Davis jmdavisProg at gmx.com
Thu May 9 12:26:22 PDT 2013


On Thursday, May 09, 2013 19:45:16 Peter Alexander wrote:
> It seems that 'auto ref' would be suitable, provided we can find
> a way for it to work with normal functions (in a sensible way,
> not like templates).

That's trivial enough. All you have to do is lower code like

auto foo(auto ref int i) {...}

foo(5);

to something like

auto foo(ref int i) {...}

auto __temp = 5;
foo(__temp);

And temporaries end up on the stack anyway, so you wouldn't really even have 
to lower it to quite like that, but that's what would be happening 
conceptually. It's also what would happen if plain ref accepted rvalues. It's 
just that we avoid certain classes of issues by having something distinct from 
plain ref for accepting rvalues by ref. The implementation itself is 
straightforward. If anything, I think that argument comes down primarily to 
two things:

1. Should plain ref accept rvlaues?

2. If plain ref shouldn't accept rvalues, then what attribute do we use to 
accept rvalues by ref?

And given that the whole point of adding auto ref to the language was to solve 
exactly this problem, I think that it makes perfect sense to just use auto 
ref.

- Jonathan M Davis


More information about the Digitalmars-d mailing list