[Issue 9238] Support rvalue references
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Dec 30 00:17:47 PST 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9238
--- Comment #9 from Jonathan M Davis <jmdavisProg at gmx.com> 2012-12-30 00:17:43 PST ---
The current proposal is to leave auto ref for templates exactly as it is but to
make auto ref work with non-templated functions differently. I don't know how
const& is implemented in C++, so I don't know how close the proposal is to
that, though the use case would be similar. The proposal for non-templated
functions would be that
auto foo(auto ref T param) {...}
would become
auto foo(ref T param) {...}
and that if it were called with an rvalue, a local variable would be declared
to hold that rvalue so that it could be passed to the function by ref, and that
variable would leave scope as soon as the statement with the function call
completed. So,
foo(bar());
would become something like
auto _temp = bar();
foo(_temp);
//_temp leaves scope and is destroyed here
That way, auto ref would work with non-templated functions. But it was _not_
proposed that templated functions would change at all.
As for scope and auto ref / in ref, ref alone has the problem. You can do
something like
auto ref bar(ref int i)
{
return bar;
}
auto ref foo()
{
int i;
return bar(i);
}
and you've now escaped a reference. The fact that auto ref could take an rvalue
has zero effect on that. ref is plenty. So, unless you're proposing that ref in
general use scope, I don't think that requiring that scope be used with auto
ref / in ref fixes much.
Also, I think that requiring that const be used is a big problem. const in D is
far more restrictive than it is in C++, so making it so that our counterpart to
C++'s const& has to use const is far too restrictive. auto ref with templates
works without const just fine. You run the risk of mutating the lvalue inside
the function, because there's no protection against it, but if you want to
prevent that you can just use const, and plenty of code _can't_ use const. So,
allowing auto ref to work without const is valuable, and I think that the
non-templated solution should do the same.
auto ref should basically be saying that the programmer wants unnecessary
copies to be avoided and doesn't care about protecting against lvalues being
mutated, whereas auto ref const says that they want to avoid unnecessary copies
and are willing to put up with the extra restrictions of const to get the
guarantee that lvalues won't be mutated.
in ref goes against that goal.
I think that we should either use auto ref for non-templated functions as I've
described (without touching how templated functions work at all) or that we
should come up with a new keyword to indicate the new thing that we want (even
if it starts with @ rather than being an actual keyword). Overloading in
further is a bad idea IMHO, and I think that requiring either scope or const is
a bad idea. Certainly, if we need scope in this situation, then we need scope
for _all_ situations where ref is used, not just this.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list