structs are now lvalues - what is with "auto ref"?
Jonathan M Davis
jmdavisProg at gmx.com
Sat Dec 29 11:56:48 PST 2012
On Saturday, December 29, 2012 19:43:36 Minas Mina wrote:
> So when it will be fixed I will be able to write:
>
> void foo(auto ref Vector3 v);
>
> and it will pass copies or references depending on the situation?
For templated functions, it currently generates either
void foo(ref Vector3 v);
or
void foo(Vector3 v);
depending on whether you pass it an lvalue or an rvalue. The situtation with
non-templated functions has not been sorted out yet, but what will probably
happen once it has been is that if you declare
void foo(auto ref Vector3 v);
then underneath the hood, you'll only get
void foo(ref Vector3 v);
but when it's passed an rvalue, it will get automatically assigned to a local
variable first and then that local variable will leave scope after the function
call. So,
foo(bar());
will turn into something like
auto _temp = bar();
foo(_temp);
In either case, the idea is that if you declare a parameter to be auto ref, it
will accept both rvalues and lvalues and do so without making unnecessary
copies, whereas ref will still specifically require an lvalue. So, you use ref
when you want to mutate the original, and you use auto ref when you don't care
whether it gets mutated or not but don't want a copy to be made if it doesn't
have to be (and you use auto ref const if you want to avoid the copy but
guarantee that it doesn't get mutated).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list