auto ref and non-templated functions

Jonathan M Davis jmdavisProg at gmx.com
Mon Dec 24 09:40:00 PST 2012


This has probably been discussed before, so someone has probably already 
explained why this is a bad idea, but I can't remember why that would be, so 
I'm going to ask:

Why can't we simply make auto ref work with non-templated functions by making 
it automatically generate both the ref and non-ref versions? So, if I do

auto foo(auto ref S s) { /*do stuff*/ }

I automatically get something like

auto foo(S s) { foo(s); }
auto foo(ref S s) { /*do stuff*/ }

and

auto foo(auto const ref S s) { /* do stuff* / }

becomes

auto foo(const S s) { foo(s); }
auto foo(ref const S s) { /* do stuff */ }

What problems does this cause? Why haven't we just done this already?

And if that doesn't work, can we simply make it so that the compiler 
automatically creates a variable when you pass an rvalue to a non-templated 
auto ref function? So, with

auto foo(auto ref S s) { /*do stuff*/ }

you get

auto foo(ref S s) { /* do stuff*/ }

but when you call it with an rvalue like with

S bar() { ... }
foo(bar());

you get something like

auto _rValue = bar();
foo(_rValue};

where _rValue leaves scope after the call to foo. Are there any problems that 
anyone can see with that?

It just seems to me that it should be relatively easy to come up with a 
solution to make auto ref work with non-templated functions, and then we can 
solve the whole const ref issue and be done with it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list