rvalue references

kinke via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 6 10:01:51 PDT 2015


On Wednesday, 3 June 2015 at 01:57:21 UTC, bitwise wrote:
> 'in' is currently useless because scope is not defined 
> properly, and const is too restrictive. Also, because of DIP25, 
> it's now even more useless. It seems a pretty sure bet that it 
> will either continue to be completely useless or be changed at 
> some point anyways, so why not now?
> ...
> So why not re-brand this awesomely concise and convenient 
> keyword to be the "non-template auto ref"?

I see the point in some sort of `auto ref`/`scope ref` parameters 
binding to rvalues in cases where one's not interested in a 
function's side effect applied to a non-escapable rvalue argument.

But what I need 99% of the time is some means to express my 
intention to pass an argument the most efficient way possible as 
the callee is only gonna read from it and won't let it escape. 
This is only relevant for non-primitive value types, i.e., 
structs. Like many others, I love the neat little `in` keyword, 
and would like for it to have exactly these semantics, for both 
templates and normal functions:

* the callee has read-only access to the parameter, i.e.,
   it is const;
* since it's declared as normal function parameter (`in T arg`),
   it cannot escape (`scope` or however we wanna call this);
* depending on type and hardware, value types are either passed
   by value (POD type whose size <= [2*]size_t.sizeof) or by
   reference (non-POD type and/or size > threshold);
   reference types (class instances) are always passed by ref
* if a value type is passed by reference, an rvalue argument is
   allowed to safely bind to the const, non-escapable reference.

This would be particularly useful for generic templates, e.g., 
containers, where primitive and small POD types T are ideally 
passed by value, and other Ts by reference.


More information about the Digitalmars-d mailing list